Exemple #1
0
        private void OnReceive(IAsyncResult result)
        {
            try
            {
                if (result.IsCompleted)
                {
                    int bytesRead = this.socket.EndReceive(result);

                    if (bytesRead > 0)
                    {
                        byte[] read = new byte[bytesRead];
                        Array.Copy(this.buffer, 0, read, 0, bytesRead);

                        this.readHandler(this, read);
                        SocketRead.Begin(this.socket, this.readHandler, this.errorHandler);
                    }
                    else
                    {
                        // Disconnect
                    }
                }
            }
            catch (Exception e)
            {
                if (this.errorHandler != null)
                {
                    this.errorHandler(this, e);
                }
            }
        }
Exemple #2
0
 private void OnClientConnect(IAsyncResult result)
 {
     try
     {
         var clientSocket = Socket.EndAccept(result);
         ResourcesManager.AddClient(new Client(clientSocket),
                                    ((IPEndPoint)clientSocket.RemoteEndPoint).Address.ToString());
         SocketRead.Begin(clientSocket, OnReceive, OnReceiveError);
         Console.WriteLine("[UCS]    Client connected (" + ((IPEndPoint)clientSocket.RemoteEndPoint).Address +
                           ":" +
                           ((IPEndPoint)clientSocket.RemoteEndPoint).Port + ")");
     }
     catch (Exception e)
     {
         Console.WriteLine("[UCS]    Exception when accepting incoming connection: " + e);
     }
     try
     {
         Socket.BeginAccept(OnClientConnect, Socket);
     }
     catch (Exception e)
     {
         Console.WriteLine("[UCS]    Exception when starting new accept process: " + e);
     }
 }
Exemple #3
0
 void OnClientConnect(System.IAsyncResult result)
 {
     try
     {
         Socket clientSocket = m_vServerSocket.EndAccept(result);
         Debugger.WriteLine("[M] Client connected (" + ((IPEndPoint)clientSocket.RemoteEndPoint).Address.ToString() + ":" + ((IPEndPoint)clientSocket.RemoteEndPoint).Port.ToString() + ")");
         ResourcesManager.AddClient(new Client(clientSocket));
         SocketRead.Begin(clientSocket, OnReceive, OnReceiveError);
     }
     catch (System.Exception e)
     {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine("Exception when accepting incoming connection: " + e);
         Console.ResetColor();
     }
     try
     {
         m_vServerSocket.BeginAccept(new System.AsyncCallback(OnClientConnect), m_vServerSocket);
     }
     catch (System.Exception e)
     {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine("Exception when starting new accept process: " + e);
         Console.ResetColor();
     }
 }
Exemple #4
0
        private void ProcessAccept(SocketAsyncEventArgs args)
        {
            if (args.SocketError != SocketError.Success)
            {
                StartAccept(); // start to accept as soon as possible
                ProcessBadAccept(args);
                return;
            }

            StartAccept(); // start to accept as soon as possible

            var connection     = args.AcceptSocket;
            var remoteEndPoint = (IPEndPoint)connection.RemoteEndPoint;

            ResourcesManager.AddClient(new Client(connection));
            SocketRead.Begin(connection, OnReceive, OnReceiveError);

            Console.WriteLine("Client connected ({0}:{1})", remoteEndPoint.Address, remoteEndPoint.Port);

            args.AcceptSocket = null;
            _acceptPool.Push(args);
        }
Exemple #5
0
 private void OnReceive(IAsyncResult result)
 {
     try
     {
         if (result.IsCompleted)
         {
             int num = this.Socket.EndReceive(result);
             if (num > 0)
             {
                 byte[] array = new byte[num];
                 Array.Copy(this.buffer, 0, array, 0, num);
                 this.readHandler(this, array);
                 SocketRead.Begin(this.Socket, this.readHandler, this.errorHandler);
             }
         }
     }
     catch (Exception exception)
     {
         if (this.errorHandler != null)
         {
             this.errorHandler(this, exception);
         }
     }
 }