Beispiel #1
0
 public void createTcpListener(string sockID, IPEndPoint ipEndPoint)
 {
     if (listenerSocketMap.ContainsKey(ipEndPoint))
     {
         logger.Info(string.Format("already exist socket :{0}", ipEndPoint.ToString()));
     }
     else
     {
         TcpListener    listener    = new TcpListener(ipEndPoint);
         NetworkStream  ns          = null;
         TCPListenerSet listenerSet = new TCPListenerSet(listener, ns);
         listenerSocketMap.Add(ipEndPoint, listenerSet);
         logger.Info(string.Format("create success socket :{0}", ipEndPoint.ToString()));
         Task socketListener = new Task(() => this.listener(sockID, listenerSet));
         socketListener.Start();
     }
 }
Beispiel #2
0
        public void listener(string socketID, TCPListenerSet listenerSet)
        {
            TcpClient    client = null;
            StreamReader sr;
            string       getData = "";
            string       header  = "";

            try
            {
                listenerSet.listener.Start();
                while (true)
                {
                    socketLogger.Info(string.Format("socket id {0} listen... socket {1}", socketID, listenerSet.listener.LocalEndpoint));
                    client = listenerSet.listener.AcceptTcpClient();
                    socketLogger.Info(string.Format("socket id {0} accept socket {1}", socketID, client.Client.RemoteEndPoint));
                    listenerSet.ns = client.GetStream();
                    sr             = new StreamReader(client.GetStream());
                    while (client.Connected)
                    {
                        int size = 0;
                        if (socketID.Equals("OMS"))
                        {
                            size    = listenerSet.ns.Read(listenerSet.readBuffer, 0, listenerSet.readBuffer.Length);
                            getData = System.Text.Encoding.ASCII.GetString(listenerSet.readBuffer);
                        }
                        else
                        {
                            getData = sr.ReadLine();
                        }

                        if (getData != null)
                        {
                            header = getData.Substring(0, 4);
                            //lock (MainForm.lockObj)
                            {
                                switch (header)
                                {
                                case "0374":
                                    if (socketID.Equals("OMS"))
                                    {
                                        STRUCT_0374_REPLY msg_oms = new STRUCT_0374_REPLY(socketID, getData);
                                        Task FilledThread         = new Task(() => msgHandler.FilledMessageProc(msg_oms));
                                        Task positionCalcThread   = new Task(() => msgHandler.PositionCalcFilled(msg_oms));
                                        FilledThread.Start();
                                        positionCalcThread.Start();
                                    }
                                    else
                                    {
                                        STRUCT_0374 msg_ticket         = new STRUCT_0374(socketID, getData);
                                        Task        CancelCheckThread  = new Task(() => msgHandler.CancelCheck(msg_ticket));
                                        Task        positionCalcThread = new Task(() => msgHandler.PositionCalcTicket(msg_ticket));
                                        CancelCheckThread.Start();
                                        positionCalcThread.Start();
                                    }
                                    break;

                                case "0393":
                                    messageQueue_0393.Enqueue(new STRUCT_0393(socketID, getData));
                                    break;

                                case "0419":
                                    STRUCT_0419 msg_confirm   = new STRUCT_0419(socketID, getData);
                                    Task        ConfirmThread = new Task(() => msgHandler.ReceiveConfirm(msg_confirm));
                                    ConfirmThread.Start();
                                    break;

                                default:
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (listenerSet.ns != null)
                            {
                                listenerSet.ns.Close();
                            }
                            if (client != null)
                            {
                                client.Close();
                            }
                            if (client.GetStream() != null)
                            {
                                client.GetStream().Close();
                            }
                        }
                    }
                }
            }
            catch (Exception error)
            {
                logger.Error(error.ToString());
            }
            finally
            {
                if (listenerSet.ns != null)
                {
                    listenerSet.ns.Close();
                }
                if (client.Connected == true)
                {
                    if (client?.GetStream() != null)
                    {
                        client.GetStream().Close();
                    }
                }
                if (client != null)
                {
                    client.Close();
                }
            }
        }