Ejemplo n.º 1
0
        /// <summary>
        /// Handler for the read event
        /// </summary>
        /// <param name="ar">Result information</param>
        private void ReadCallbackHandler(IAsyncResult ar)
        {
            try
            {
                // Read data from the client socket.
                int bytesRead = sock_.EndReceive(ar);
                if (bytesRead > 0)
                {
                    // Bump used size.
                    usedSize_ += bytesRead;

                    // Notify the handlers
                    ReadEventArgs readEv = new ReadEventArgs(this, data_, usedSize_, DefaultReceiveSize);
                    if (OnReceiveData != null)
                    {
                        try
                        {
                            OnReceiveData(this, readEv);
                        }
                        catch { }
                    }
                    if (readEv.NextReadSize > 0)
                    {
                        this.PostReceive(readEv.NextReadSize, !readEv.AppendToBuffer);
                    }
                }
                else // socket closed
                {
                    this.Close(true);
                    if (OnEndSession != null)
                    {
                        DisconnectEventArgs discEv = new DisconnectEventArgs(this, null);
                        try
                        {
                            OnEndSession(this, discEv);
                        }
                        catch { }
                    }
                }
            }
            catch (SocketException ex)
            {
                if (sock_.Connected == false && !isClosed_)
                {
                    this.Close(true);
                    if (OnEndSession != null)
                    {
                        DisconnectEventArgs discEv = new DisconnectEventArgs(this, null);
                        try
                        {
                            OnEndSession(this, discEv);
                        }
                        catch { }
                    }
                }

                else if (isClosed_ == false && OnFailure != null)
                {
                    ErrorEventArgs errEv = new ErrorEventArgs(this, ErrorEventArgs.SocketOperation.Receive, ex);
                    try
                    {
                        OnFailure(this, errEv);
                    }
                    catch { }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handler for the connect operation
        /// </summary>
        /// <param name="ar">Result information</param>
        private void AcceptCallbackHandler(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            listenInfo_.acceptingEvent_.Set();

            try
            {
                // Get the socket that handles the client request.
                Socket handler = sock_.EndAccept(ar);

                // Create the state object.
                SocketClient scNew = new SocketClient(handler);

                // Notify the client application
                ConnectEventArgs connEv = new ConnectEventArgs(scNew, listenInfo_.ReadSize);
                if (OnStartSession != null)
                {
                    try
                    {
                        OnStartSession(this, connEv);
                    }
                    catch { }
                }

                if (scNew.IsConnected)
                {
                    // If we are auto-wireup events then do so.
                    if (connEv.AutoConnect == true)
                    {
                        if (this.OnEndSession != null)
                        {
                            foreach (Delegate d in this.OnEndSession.GetInvocationList())
                            {
                                scNew.OnEndSession += (EventHandler <SocketEventArgs>)d;
                            }
                        }
                        if (this.OnFailure != null)
                        {
                            foreach (Delegate d in this.OnFailure.GetInvocationList())
                            {
                                scNew.OnFailure += (EventHandler <SocketEventArgs>)d;
                            }
                        }
                        if (this.OnReceiveData != null)
                        {
                            foreach (Delegate d in this.OnReceiveData.GetInvocationList())
                            {
                                scNew.OnReceiveData += (EventHandler <SocketEventArgs>)d;
                            }
                        }
                        if (this.OnSendDataComplete != null)
                        {
                            foreach (Delegate d in this.OnSendDataComplete.GetInvocationList())
                            {
                                scNew.OnSendDataComplete += (EventHandler <SocketEventArgs>)d;
                            }
                        }
                    }

                    if (connEv.NextReadSize > 0)
                    {
                        scNew.PostReceive(connEv.NextReadSize, true);
                    }
                }
            }
            catch (ObjectDisposedException)
            {
                // Ignore and exit.
            }
            catch (SocketException ex)
            {
                if (OnFailure != null)
                {
                    ErrorEventArgs errEv = new ErrorEventArgs(this, ErrorEventArgs.SocketOperation.Accept, ex);
                    try
                    {
                        OnFailure(this, errEv);
                    }
                    catch { }
                }
            }
        }