Esempio n. 1
0
        /// <summary>
        /// 接收数据回调方法
        /// </summary>
        /// <param name="ar">回调参数</param>
        private void OnReceive(IAsyncResult ar)
        {
            TCPEndPoint end = ar.AsyncState as TCPEndPoint;

            try
            {
                int real_recv = end.Socket.EndReceive(ar);
                end.MStream.Write(end.Buffer, 0, real_recv);  //写入消息缓冲区
                //尝试读取一条完整消息
                ZMessage msg;
                while (end.MStream.ReadMessage(out msg))
                {
                    if ((Msg)msg.head == Msg.Default)  //心跳包 跳过
                    {
                        foreach (KeyValuePair <int, int> p in _pulse_time)
                        {
                            if (p.Key == end.UID)
                            {
                                _pulse_time[p.Key] = _pulse; //重置
                                break;
                            }
                        }
                        continue;
                    }
                    //处理消息
                    if (TCPMessageReceived != null)
                    {
                        TCPMessageReceivedEventArgs args = new TCPMessageReceivedEventArgs();
                        args.Msg  = (Msg)msg.head;
                        args.Time = DateTime.Now;
                        args.End  = end;
                        args.Data = msg.content;
                        TCPMessageReceived.BeginInvoke(_server_id, args, null, null);  //激发事件,通知事件注册者处理消息
                    }
                }
                end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end);  //开始下一次数据接收
            }
            catch
            {
                if (TCPClientDisConnected != null)  //通知客户端断开
                {
                    TCPClientDisConnectedEventArgs args = new TCPClientDisConnectedEventArgs();
                    args.End  = end;
                    args.Time = DateTime.Now;
                    TCPClientDisConnected(_server_id, args);
                }
                int tmp;
                _pulse_time.TryRemove(end.UID, out tmp);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 接收数据回调方法
        /// </summary>
        /// <param name="ar"></param>
        private void OnReceive(IAsyncResult ar)
        {
            TCPEndPoint end = ar.AsyncState as TCPEndPoint;

            try
            {
                int real_recv = end.Socket.EndReceive(ar);
                end.MStream.Write(end.Buffer, 0, real_recv); //写入消息缓冲区
                //尝试读取一条完整消息
                ZMessage msg;
                while (end.MStream.ReadMessage(out msg))
                {
                    //处理消息
                    if (TCPMessageReceived != null)
                    {
                        TCPMessageReceivedEventArgs args = new TCPMessageReceivedEventArgs();
                        args.Msg  = (Msg)msg.head;
                        args.Time = DateTime.Now;
                        args.End  = end;
                        args.Data = msg.content;
                        TCPMessageReceived.BeginInvoke(_client_id, args, null, null);  //激发事件,通知事件注册者处理消息
                    }
                }
                end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end);
            }
            catch
            {
                if (TCPClientDisConnected != null)  //通知客户端断开
                {
                    TCPClientDisConnectedEventArgs args = new TCPClientDisConnectedEventArgs();
                    args.End  = end;
                    args.Time = DateTime.Now;
                    TCPClientDisConnected(_client_id, args);
                }
            }
        }
 /// <summary>
 /// 接收数据回调方法
 /// </summary>
 /// <param name="ar"></param>
 private void OnReceive(IAsyncResult ar)
 {
     TCPEndPoint end = ar.AsyncState as TCPEndPoint;
     try
     {
         int real_recv = end.Socket.EndReceive(ar);
         end.MStream.Write(end.Buffer, 0, real_recv); //写入消息缓冲区
         //尝试读取一条完整消息
         ZMessage msg;
         while (end.MStream.ReadMessage(out msg))
         {
             //处理消息
             if (TCPMessageReceived != null)
             {
                 TCPMessageReceivedEventArgs args = new TCPMessageReceivedEventArgs();
                 args.Msg = (Msg)msg.head;
                 args.Time = DateTime.Now;
                 args.End = end;
                 args.Data = msg.content;
                 TCPMessageReceived.BeginInvoke(_client_id, args, null, null);  //激发事件,通知事件注册者处理消息
             }
         }
         end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end);
     }
     catch
     {
         if (TCPClientDisConnected != null)  //通知客户端断开
         {
             TCPClientDisConnectedEventArgs args = new TCPClientDisConnectedEventArgs();
             args.End = end;
             args.Time = DateTime.Now;
             TCPClientDisConnected(_client_id, args);
         }
     }
 }
        /// <summary>
        /// 用户断线
        /// </summary>
        /// <param name="csID"></param>
        /// <param name="args"></param>
        void manager_TCPClientDisConnected(string csID, TCPClientDisConnectedEventArgs args)
        {
            this.Invoke((Action)delegate()
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells[0].Value.ToString() == args.End.UID.ToString())
                    {
                        dataGridView1.Rows.Remove(row);
                        break;
                    }
                }
                textBox1.AppendText(args.Time.ToLongTimeString() + " 终端唯一标识为[" + args.End.UID + "]的用户下线\n");

            });
        }
 /// <summary>
 /// 接收数据回调方法
 /// </summary>
 /// <param name="ar">回调参数</param>
 private void OnReceive(IAsyncResult ar)
 {
     TCPEndPoint end = ar.AsyncState as TCPEndPoint;
     try
     {
         int real_recv = end.Socket.EndReceive(ar);
         end.MStream.Write(end.Buffer, 0, real_recv);  //写入消息缓冲区
         //尝试读取一条完整消息
         ZMessage msg;
         while (end.MStream.ReadMessage(out msg))
         {
             if ((Msg)msg.head == Msg.Default)  //心跳包 跳过
             {
                 foreach (KeyValuePair<int, int> p in _pulse_time)
                 {
                     if (p.Key == end.UID)
                     {
                         _pulse_time[p.Key] = _pulse; //重置
                         break;
                     }
                 }
                 continue;
             }
             //处理消息
             if (TCPMessageReceived != null)
             {
                 TCPMessageReceivedEventArgs args = new TCPMessageReceivedEventArgs();
                 args.Msg = (Msg)msg.head;
                 args.Time = DateTime.Now;
                 args.End = end;
                 args.Data = msg.content;
                 TCPMessageReceived.BeginInvoke(_server_id, args, null, null);  //激发事件,通知事件注册者处理消息
             }
         }
         end.Socket.BeginReceive(end.Buffer, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), end);  //开始下一次数据接收
     }
     catch
     {
         if (TCPClientDisConnected != null)  //通知客户端断开
         {
             TCPClientDisConnectedEventArgs args = new TCPClientDisConnectedEventArgs();
             args.End = end;
             args.Time = DateTime.Now;
             TCPClientDisConnected(_server_id, args);
         }
         int tmp;
         _pulse_time.TryRemove(end.UID,out tmp);
     }
 }