public static void DisconnectConnections()
 {
     foreach (ClientConnection connection in ClientConnectionManager.m_connections.Values)
     {
         TcpServerSocket.Disconnect(connection);
     }
 }
예제 #2
0
        public void ReceiveData()
        {
            if (!this.Destructed)
            {
                this.m_receiveBuffer.Write(this.m_receiveAsyncEventArgs.Buffer, this.m_receiveAsyncEventArgs.BytesTransferred);

                int length = this.m_receiveBuffer.Size();

                do
                {
                    int read = this.Messaging.OnReceive(this.m_receiveBuffer.GetBuffer(), length);

                    if (read <= 0)
                    {
                        if (read == -1)
                        {
                            TcpServerSocket.Disconnect(this);
                        }
                        break;
                    }

                    this.m_receiveBuffer.Remove(read);
                } while ((length = this.m_receiveBuffer.Size()) > 0);
            }
        }
예제 #3
0
 public void Send(byte[] buffer, int length)
 {
     if (!this.Destructed)
     {
         TcpServerSocket.Send(this, buffer, length);
     }
 }
        private static void OnReceive(object sender, SocketAsyncEventArgs args)
        {
            ClientConnection clientConnection = (ClientConnection)args.UserToken;

            if (clientConnection != null)
            {
                try
                {
                    do
                    {
                        if (args.SocketError != SocketError.Success || args.BytesTransferred <= 0)
                        {
                            TcpServerSocket.Disconnect(clientConnection);
                            break;
                        }

                        clientConnection.ReceiveData();
                    } while (!clientConnection.Destructed && !clientConnection.Socket.ReceiveAsync(args));
                }
                catch (SocketException)
                {
                    TcpServerSocket.Disconnect(clientConnection);
                }
                catch (Exception exception)
                {
                    Logging.Error("TcpServerSocket.onReceive - unhandled exception thrown : " + exception);
                    TcpServerSocket.Disconnect(clientConnection);
                }
            }
        }
        private static void OnSend(object sender, SocketAsyncEventArgs args)
        {
            if (args.SocketError != SocketError.Success)
            {
                TcpServerSocket.Disconnect((ClientConnection)args.UserToken);
            }

            args.Dispose();
        }
        private static void Update()
        {
            while (ClientConnectionManager.m_started)
            {
                foreach (ClientConnection connection in ClientConnectionManager.m_connections.Values)
                {
                    if (!connection.MessageManager.IsAlive())
                    {
                        TcpServerSocket.Disconnect(connection);
                        continue;
                    }
                }

                Thread.Sleep(1000);
            }
        }
        public static void Disconnect(ClientConnection clientConnection, int reason)
        {
            if (clientConnection.State == ClientConnectionState.LOGGED)
            {
                if (reason != 0)
                {
                    DisconnectedMessage disconnectedMessage = new DisconnectedMessage();
                    disconnectedMessage.SetReason(reason);
                    clientConnection.Messaging.Send(disconnectedMessage);
                }
                else
                {
                    TcpServerSocket.Disconnect(clientConnection);
                }
            }

            clientConnection.SetState(ClientConnectionState.DISCONNECTED);
        }
        public static void Send(ClientConnection clientConnection, byte[] buffer, int length)
        {
            SocketAsyncEventArgs sendAsyncEventArgs = new SocketAsyncEventArgs();

            sendAsyncEventArgs.SetBuffer(buffer, 0, length);
            sendAsyncEventArgs.UserToken  = clientConnection;
            sendAsyncEventArgs.Completed += TcpServerSocket.OnSend;

            try
            {
                if (!clientConnection.Socket.SendAsync(sendAsyncEventArgs))
                {
                    TcpServerSocket.OnSend(null, sendAsyncEventArgs);
                }
            }
            catch (Exception)
            {
                TcpServerSocket.OnSend(null, sendAsyncEventArgs);
            }
        }
        private void OnAccept(object sender, SocketAsyncEventArgs args)
        {
            if (args.SocketError == SocketError.Success)
            {
                Socket socket = args.AcceptSocket;
                SocketAsyncEventArgs receiveAsyncEventArgs = new SocketAsyncEventArgs();

                receiveAsyncEventArgs.SetBuffer(new byte[TcpServerSocket.RECEIVE_BUFFER_SIZE], 0, TcpServerSocket.RECEIVE_BUFFER_SIZE);
                receiveAsyncEventArgs.Completed += TcpServerSocket.OnReceive;

                ClientConnection clientConnection = ClientConnectionManager.Create(socket, receiveAsyncEventArgs);

                receiveAsyncEventArgs.UserToken = clientConnection;

                if (!socket.ReceiveAsync(receiveAsyncEventArgs))
                {
                    TcpServerSocket.OnReceive(null, receiveAsyncEventArgs);
                }
            }

            this.StartAccept(args);
        }