예제 #1
0
 /// <summary>
 /// 接收消息
 /// </summary>
 /// <param name="proxySocket"></param>
 /// <param name="message"></param>
 void proxy_MessageReceived(ZProxySocket proxySocket, ZMessage message)
 {
     if (MessageReceived != null)
     {
         MessageReceived(proxySocket, message);  //激发MessageReceived事件
     }
 }
예제 #2
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="message"></param>
 public void SendMessage(ZMessage message)
 {
     try
     {
         _socket.Send(message.RawData);  //
     }
     catch
     {
     }
 }
예제 #3
0
        /// <summary>
        /// 异步接收数据回调方法
        /// </summary>
        /// <param name="ar"></param>
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                int real_received = _socket.EndReceive(ar);
                if (real_received != 0)
                {
                    ZDataBuffer databuffer = GetDataBuffer();
                    byte[]      uncomplete = ar.AsyncState as byte[]; //上次未处理完的数据
                    if (uncomplete != null)
                    {
                        databuffer.WriteBytes(uncomplete, 0, uncomplete.Length); //将上次未处理完的数据重新写入缓冲区
                    }
                    databuffer.WriteBytes(_buffer_received, 0, real_received);   //将本次接收到的数据写入缓冲区

                    ZMessage message = null;
                    while ((message = databuffer.TryReadMessage()) != null)  //尝试从缓冲区中读取一条完整消息
                    {
                        if (MessageReceived != null)
                        {
                            MessageReceived.BeginInvoke(this, message, null, null);  //异步激发事件
                        }
                    }
                    _socket.BeginReceive(_buffer_received, 0, 1024, SocketFlags.None, new AsyncCallback(OnReceive), databuffer.UnCompelete);  //将未处理完的数据存放在state参数中,并开始下一次异步接收数据
                }
                else
                {
                    if (DisConnected != null)
                    {
                        DisConnected(this);
                    }
                }
            }
            catch
            {
                if (DisConnected != null)
                {
                    DisConnected(this);
                }
            }
        }