Exemple #1
0
        // This method is invoked when an asynchronous receive operation completes.
        // If the remote host closed the connection, then the socket is closed.
        // If data was received then the data is echoed back to the client.
        //
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            TCPConnection token = null;

            try
            {
                token = e.UserToken as TCPConnection;
                int numBytes = e.BytesTransferred;

                if (numBytes > 0 && e.SocketError == SocketError.Success)
                {
                    token.OnReceive(e.Buffer, numBytes);
                    token.StartReceive();
                }
                else
                {
                    //Console.WriteLine(" - Disconnecting client (" + token.TcpEndpoint + "), received bytes=" + numBytes);

                    if (token != null)
                    {
                        token.Disconnect();
                    }
                    return;
                }
            }
            catch (ObjectDisposedException)
            {
                if (token != null)
                {
                    token.Disconnect();
                }
            }
            catch (SocketException ex)
            {
                if (token != null)
                {
                    Console.WriteLine(string.Format("{0}  {1}", token.TcpEndpoint, ex.Message));

                    token.Disconnect();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("OnReceiveHandler error: {0}", ex);

                if (token != null)
                {
                    token.Disconnect();
                }
            }
        }
Exemple #2
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
//             Interlocked.Increment(ref m_numConnectedSockets);
//             Console.WriteLine("Client connection accepted. There are {0} clients connected to the server",
//                 m_numConnectedSockets);

            // create a new connection object for this incoming connection

            Socket sock = null;

            try
            {
                if (m_listen == null)
                {
                    return;
                }

                sock = e.AcceptSocket;

//                sock.SendBufferSize = m_iocp.BufferSize;
//                sock.ReceiveBufferSize = m_iocp.BufferSize;
//                sock.NoDelay = Constants.UseNoDelay;

                TCPConnection baseSocket = null;

                try
                {
                    string ip = sock.Connected ? sock.RemoteEndPoint.ToString() : "socket disconnected";
                    //Globals.Log.Debug("{0} - Incoming connection from {1}", m_config.Name, ip);

                    baseSocket     = new TCPConnection(m_iocp, sock, m_networkHandler, m_config);
                    baseSocket.Tag = new ConnectionTag();

//                     lock (_clients)
//                         _clients.Add(baseSocket);

                    baseSocket.OnConnect();
                    baseSocket.StartReceive();
                }
                catch (SocketException)
                {
                    //Globals.Log.Error("BaseServer SocketException");
                    if (baseSocket != null)
                    {
                        baseSocket.Disconnect();
                    }
                }
                catch (Exception ex)
                {
                    //Globals.Log.Error("Client creation", ex);

                    if (baseSocket != null)
                    {
                        baseSocket.Disconnect();
                    }
                }
            }
            catch
            {
                //Globals.Log.Error("AcceptCallback: Catch");

                if (sock != null) // don't leave the socket open on exception
                {
                    try
                    {
                        sock.Close();
                    }
                    catch
                    {
                    }
                }
            }
            finally
            {
                if (m_listen != null)
                {
//                    e.AcceptSocket = null;
                    StartAccept(e);
                }
            }
        }
Exemple #3
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            //             Interlocked.Increment(ref m_numConnectedSockets);
            //             Console.WriteLine("Client connection accepted. There are {0} clients connected to the server",
            //                 m_numConnectedSockets);

            // create a new connection object for this incoming connection

            Socket sock = null;

            try
            {
                if (m_listen == null)
                    return;

                sock = e.AcceptSocket;

            //                sock.SendBufferSize = m_iocp.BufferSize;
            //                sock.ReceiveBufferSize = m_iocp.BufferSize;
            //                sock.NoDelay = Constants.UseNoDelay;

                TCPConnection baseSocket = null;

                try
                {
                    string ip = sock.Connected ? sock.RemoteEndPoint.ToString() : "socket disconnected";
                    //Globals.Log.Debug("{0} - Incoming connection from {1}", m_config.Name, ip);

                    baseSocket = new TCPConnection(m_iocp, sock, m_networkHandler, m_config);
                    baseSocket.Tag = new ConnectionTag();

            //                     lock (_clients)
            //                         _clients.Add(baseSocket);

                    baseSocket.OnConnect();
                    baseSocket.StartReceive();
                }
                catch (SocketException)
                {
                    //Globals.Log.Error("BaseServer SocketException");
                    if (baseSocket != null)
                        baseSocket.Disconnect();
                }
                catch (Exception ex)
                {
                    //Globals.Log.Error("Client creation", ex);

                    if (baseSocket != null)
                        baseSocket.Disconnect();
                }
            }
            catch
            {
                //Globals.Log.Error("AcceptCallback: Catch");

                if (sock != null) // don't leave the socket open on exception
                {
                    try
                    {
                        sock.Close();
                    }
                    catch
                    {
                    }
                }
            }
            finally
            {
                if (m_listen != null)
                {
            //                    e.AcceptSocket = null;
                    StartAccept(e);
                }
            }
        }