Example #1
0
        /// <summary>
        /// Manages the data received async callback
        /// </summary>
        /// <param name="result">Result of async operation</param>
        protected override void dataReceived(IAsyncResult result)
        {
            int ix;
            AsyncStateObject aso    = (AsyncStateObject)result.AsyncState;
            Socket           socket = aso.Socket;
            EndPoint         ep     = null;

            ix = clients.IndexOf(socket);
            if (ix < 0)
            {
                return;
            }
            try
            {
                ep = socket.RemoteEndPoint;
                if (socket.Connected)
                {
                    base.dataReceived(result);
                }
                else
                //if (!socket.Connected || !socket.Poll(1, SelectMode.SelectRead) || !socket.Poll(1, SelectMode.SelectWrite))
                {
                    clients.RemoveAt(ix);
                    if (ClientDisconnected != null)
                    {
                        ClientDisconnected(ep);
                    }
                }
            }
            catch
            {
                if (clients.Count > ix)
                {
                    clients.RemoveAt(ix);
                }
                if (ClientDisconnected != null)
                {
                    ClientDisconnected(ep);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Manages the data received async callback
        /// </summary>
        /// <param name="result">Result of async operation</param>
        protected virtual void dataReceived(IAsyncResult result)
        {
            int received;

            byte[]           cnnBuff = new byte[1];
            AsyncStateObject aso     = (AsyncStateObject)result.AsyncState;
            Socket           socket  = aso.Socket;

            try
            {
                received = socket.EndReceive(result);
            }

            //catch (SocketException ex)
            catch (SocketException)
            {
                //if (ex.ErrorCode == 10054)
                //	throw ex;
                BeginReceive(aso);
                return;
            }
            if (socket.Connected)
            {
                if ((received == 0) && (aso.Length == 0))
                {
                    throw new Exception("Disconnected");
                }
                aso.Flush(received);
                // There is no pending data
                if ((received < aso.BufferSize) || (socket.Available < 1))
                {
                    ParseReceivedData(socket, aso.DataReceived);
                    aso = new AsyncStateObject(socket, bufferSize);
                }

                BeginReceive(aso);
                //if (socket.Receive(cnnBuff, SocketFlags.Peek) == 0)
                //	throw new Exception("Disconnected");
            }
        }
Example #3
0
        /// <summary>
        /// Begins a (safe) receive operation with a socket.
        /// If the operation fails, it retries automatically while the socket is connected
        /// </summary>
        internal virtual bool BeginReceive(AsyncStateObject aso)
        {
            if ((aso == null) || (aso.Socket == null))
            {
                return(false);
            }

            //while (aso.Socket.Connected)
            if (aso.Socket.Connected)
            {
                try
                {
                    aso.Socket.BeginReceive(aso.Buffer, 0, bufferSize, SocketFlags.None, dlgDataReceived, aso);
                    return(true);
                }
                catch
                {
                    System.Threading.Thread.Sleep(0);
                    //continue;
                }
            }
            return(false);
        }
Example #4
0
        private void SocketAccepted(IAsyncResult result)
        {
            Socket           s;
            AsyncStateObject aso;

            try
            {
                s = listener.EndAcceptSocket(result);
            }
            catch { return; }

            if (s.Connected)
            {
                clients.Add(s);
                aso = new AsyncStateObject(s, bufferSize);
                BeginReceive(aso);
                if (ClientConnected != null)
                {
                    ClientConnected(s);
                }
            }
            listener.BeginAcceptSocket(dlgSocketAccepted, listener);
        }