Esempio n. 1
0
        void UDPSendTo(byte[] data, int offset, int size)
        {
            UDPSocketStateObject state = new UDPSocketStateObject
            {
                obj    = this,
                socket = socket,
            };

            socket.BeginSendTo(data, offset, size, 0, remoteEnd, new AsyncCallback(WriteCallback), state);
        }
Esempio n. 2
0
        public void StartRead()
        {
            UDPSocketStateObject state = new UDPSocketStateObject
            {
                obj    = this,
                socket = socket,
                buf    = udpRcvBuf,
                remote = remoteEnd,
            };

            PostReadRequest(state);
        }
Esempio n. 3
0
        public static void WriteCallback(IAsyncResult ar)
        {
            UDPSocketStateObject state = (UDPSocketStateObject)ar.AsyncState;

            try
            {
                state.socket.EndSendTo(ar);
            }
            catch (SocketException ex)
            {
                state.obj.PushError(ex);
            }
        }
Esempio n. 4
0
        public static void RecvCallback(IAsyncResult ar)
        {
            UDPSocketStateObject state = (UDPSocketStateObject)ar.AsyncState;

            try
            {
                int bytesRead = state.socket.EndReceiveFrom(ar, ref state.remote);
                if (bytesRead <= 0)
                {
                    var ex = new EndOfStreamException("socket closed by peer");
                    state.obj.PushError(ex);
                    return;
                }
                var data = new byte[bytesRead];
                Buffer.BlockCopy(state.buf, 0, data, 0, bytesRead);
                state.obj.PushToRecvQueue(data);
                state.obj.PostReadRequest(state);
            }
            catch (SocketException ex)
            {
                state.obj.PushError(ex);
            }
        }
Esempio n. 5
0
 void PostReadRequest(UDPSocketStateObject state)
 {
     socket.BeginReceiveFrom(state.buf, 0, state.buf.Length, 0, ref remoteEnd,
                             new AsyncCallback(RecvCallback), state);
 }