Example #1
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();
                }
            }
        }