Example #1
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            exit = false;

            system = VSystem.GetInstance();

            if (!IsBindEvent)
            {
                var conversationService = (ConversationService)system.GetSystemService(ServiceType.ConversationService);
                conversationService.IPString   = cmbIP.Text;
                conversationService.Port       = int.Parse(txtPort.Text);
                conversationService.BufferSize = PacketHelper.PacketLength;
                // 服务启动成功事件处理函数
                conversationService.OnSuccessService += new EventHandler(conversationService_OnStartSuccessService);
                // 服务启动失败事件处理函数
                conversationService.OnFaultService += new EventHandler(conversationService_OnFaultService);
                // 客户端接收线路建立事件处理函数
                conversationService.OnCreateThread += new ThreadEventHandler(conversationService_OnCreateThread);
                // 客户端退出事件处理函数
                conversationService.OnLogout += new ClientEventHandler(conversationService_OnLogout);
                // 收到数据包处理函数
                conversationService.OnReceivePocketData += new ReceiveDataEventHandler(conversationService_OnReceivePocketData);
                // 运行状态消息处理函数
                conversationService.OnRunStateMessage += new CommonRunStateMessageEventHandler(conversationService_OnRunStateMessage);


                conversationService.OnReplyTimeout += new ClientEventHandler(conversationService_OnReplyTimeout);
                conversationService.OnReplyOnline  += new ClientEventHandler(conversationService_OnReplyOnline);
                IsBindEvent = true;
            }

            system.Run();
        }
Example #2
0
        private void CreateMultiClient()
        {
            var receveService = (ConversationService)system.GetSystemService(ServiceType.ConversationService);

            if (receveService == null)
            {
                return;
            }
            TcpClient Client = new TcpClient(receveService.IPString, receveService.Port);

            var readThread = new Thread(() =>
            {
                try
                {
                    Thread.CurrentThread.Name = "测试读" + new Random().Next(9999).ToString("D4");

                    TcpClient client = (TcpClient)Thread.GetData(Thread.GetNamedDataSlot(Thread.CurrentThread.ManagedThreadId.ToString()));

                    NetworkStream stream = client.GetStream();
                    //-----------------------------------------------------------------------------------------------------
                    //PacketHelper ap = new PacketHelper();
                    //-----------------------------------------------------------------------------------------------------
                    DisassembleBuffer db = new DisassembleBuffer();

                    //var mn = "0001";
                    var mn = ++mMN;
                    // 收到服务端的数据 目前先不实同
                    db.OnReceivePocketData += new ReceivePocketDataEventHandler((s, rd) =>
                    {
                        string error = null;
                        var items    = new Dictionary <object, string>();
                    });
                    // Buffer for reading data
                    Byte[] bytes = new Byte[3 * PacketHelper.PacketLength];

                    string data = null;

                    Byte[] first = PacketHelper.GetPacketClientNotifyToServerShutdown((byte)mn);

                    stream.Write(first, 0, first.Length);
                    // Loop to receive all the data sent by the client.
                    while (/*Thread.CurrentThread.ThreadState != System.Threading.ThreadState.AbortRequested &&*/ !exit)
                    {
                        int i = 0;
                        Console.WriteLine("线程:{0} {1}", Thread.CurrentThread.Name, "准备再次读...");
                        if ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            db.Processing(bytes, i);
                            // Translate data bytes to a ASCII string.
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

                            Console.WriteLine("Received: {0}", data);
                        }
                    }
                }
                catch (ThreadAbortException tae)
                {
                    //Console.WriteLine("线程:{0} {1}", Thread.CurrentThread.Name, "放弃");
                    //if (OnAlarmAsyncRunStateMessage != null) OnAlarmAsyncRunStateMessage.BeginInvoke(this, new RunStateMessageEventArgs(string.Format("线程放弃:{0} {1}", Thread.CurrentThread.Name, tae.InnerException == null ? tae.Message : tae.Message + " " + tae.InnerException.Message)));
                }
                catch (IOException ioe)
                {
                    if (this == null)
                    {
                        return;
                    }
                    this.Invoke(new Action(() =>
                    {
                        txtLog.Text += (string.Format("异常:{0}\r\n", ioe.InnerException == null ? ioe.Message + "\r\n" + ioe.StackTrace : ioe.Message + " " + ioe.InnerException.Message + "\r\n" + ioe.StackTrace));
                    }));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("异常:{0}", ex.InnerException == null ? ex.Message + "\r\n" + ex.StackTrace : ex.Message + " " + ex.InnerException.Message + "\r\n" + ex.StackTrace);
                    this.Invoke(new Action(() =>
                    {
                        txtLog.Text += (string.Format("异常:{0}\r\n", ex.InnerException == null ? ex.Message + "\r\n" + ex.StackTrace : ex.Message + " " + ex.InnerException.Message + "\r\n" + ex.StackTrace));
                    }));
                }
                finally
                {
                    //Console.WriteLine("线程:{0} {1}\addMN\n", Thread.CurrentThread.Name, "退出");
                    this.Invoke(new Action(() =>
                    {
                        txtLog.Text += (string.Format("线程退出:{0}\r\n", Thread.CurrentThread.Name));
                    }));
                }
                //Shutdown and end connection
                //client.Close();
                //if (OnAlarmAsyncRunStateMessage != null) OnAlarmAsyncRunStateMessage.BeginInvoke(this, new RunStateMessageEventArgs(string.Format("线程退出:{0}", Thread.CurrentThread.Name)));
            });


            readThread.Start();
            //receiveThread.Start(Client);



            tcpClientMgr.Add(Client);
            threadMgr.Add(readThread);
            //ReceiveThreadMgr.Add(sendThread);
        }