Esempio n. 1
0
        private void ReceiveCallBack(IAsyncResult asyncResult)
        {
            UdpStateObject asyncState = asyncResult.AsyncState as UdpStateObject;

            try
            {
                int count = asyncState._socket.EndReceiveFrom(asyncResult, ref asyncState._endPoint);
                if (count > 0)
                {
                    byte[] dst = new byte[count];
                    Buffer.BlockCopy(base._buffer, 0, dst, 0, count);
                    this.HandleReceived(dst, asyncState._endPoint);
                    this.ReceiveNext();
                }
                else
                {
                    Logger.LogWarn("Read from network result in {0}", new object[] { count });
                    this.OnNetworkError(new NetworkException("cannot read from network."));
                }
            }
            catch (SocketException exception)
            {
                this.OnReceiveFailed(exception);
            }
        }
Esempio n. 2
0
        private void SendCallBack(IAsyncResult asyncResult)
        {
            UdpStateObject asyncState = asyncResult.AsyncState as UdpStateObject;

            try
            {
                asyncState._socket.EndSendTo(asyncResult);
            }
            catch (SocketException exception)
            {
                base.DelayError(exception);
            }
        }
Esempio n. 3
0
        protected void SendTo(ByteArray byteArray, System.Net.EndPoint endPoint)
        {
            UdpStateObject state = new UdpStateObject(base._socket, endPoint, null);

            try
            {
                if (base._sync)
                {
                    IAsyncResult asyncResult = base._socket.BeginSendTo(byteArray.Buffer, 0, byteArray.Length, SocketFlags.None, endPoint, null, state);
                    base.AddAsyncBlock(asyncResult, base._socket, 4);
                }
                else
                {
                    base._socket.BeginSendTo(byteArray.Buffer, 0, byteArray.Length, SocketFlags.None, endPoint, new AsyncCallback(this.SendCallBack), state);
                }
            }
            catch (SocketException exception)
            {
                Logger.LogError("Error in send : {0}", new object[] { exception.Message });
            }
        }
Esempio n. 4
0
        protected void ReceiveNext()
        {
            System.Net.EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
            UdpStateObject      state    = new UdpStateObject(base._socket, endPoint);

            try
            {
                if (base._sync)
                {
                    IAsyncResult asyncResult = base._socket.BeginReceiveFrom(base._buffer, 0, base._buffer.Length, SocketFlags.None, ref endPoint, null, state);
                    base.AddAsyncBlock(asyncResult, base._socket, 3);
                }
                else
                {
                    base._socket.BeginReceiveFrom(base._buffer, 0, base._buffer.Length, SocketFlags.None, ref endPoint, new AsyncCallback(this.ReceiveCallBack), state);
                }
            }
            catch (SocketException exception)
            {
                this.OnReceiveFailed(exception);
            }
        }