示例#1
0
 public void BeginReceive(ISocketObject listener = null)
 {
     if (InternalSocket.Connected)
     {
         InternalSocket.BeginReceive(_ReceiveBuffer, 0, _ReceiveBuffer.Length, SocketFlags.None, ReceiveCallback, listener ?? this);
     }
 }
示例#2
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            var owner = ar.AsyncState as SocketObject;

            var count = 0;

            try
            {
                count = InternalSocket.EndReceive(ar);
            }
            catch (Exception)
            {
                if (owner.SocketDisconnected != null)
                {
                    owner.SocketDisconnected.Invoke(this);
                }

                Dispose();
                return;
            }

            if (count == 0)
            {
                if (owner.SocketDisconnected != null)
                {
                    owner.SocketDisconnected.Invoke(this);
                }

                Dispose();
                return;
            }

            if (owner.ReceivedData != null)
            {
                owner.ReceivedData.Invoke(this, _ReceiveBuffer, 0, count);
            }

            if (InternalSocket.Connected)
            {
                InternalSocket.BeginReceive(_ReceiveBuffer, 0, _ReceiveBuffer.Length, SocketFlags.None, ReceiveCallback, ar.AsyncState);
            }
        }