Exemplo n.º 1
0
        private void ReadAsyncBytes(IAsyncResult result)
        {
            ServerConnectionStateObject clientState = (ServerConnectionStateObject)result.AsyncState;

            try
            {
                Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Reading data from socket", ErrorSeverity.ERROR_INFO);
                int bytecount = clientState.workSocket.EndReceive(result);

                if (bytecount > 0)
                {
                    if (bytecount == 5)
                    {
                        try
                        {
                            string eofCheck = Encoding.ASCII.GetString(clientState.byteBuffer);
                            if (eofCheck.IndexOf("<EOF>") != -1)
                            {
                                byte[] extractedRecievedData = new byte[clientState.requestBufferPosition];
                                Array.Copy(clientState.requestBuffer, extractedRecievedData, clientState.requestBufferPosition);

                                NetworkObject recievedObject = JsonConvert.DeserializeObject <NetworkObject>(Encoding.ASCII.GetString(extractedRecievedData));
                                DecodeOperation(recievedObject, clientState);
                                return;
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Malformed or incomplete data, object conversion error:\n" + e.Message + "\n" + e.StackTrace, ErrorSeverity.ERROR_WARNING);

                            if (Configuration.SpamProtection.SPAM_PROTECTION_ENABLED)
                            {
                                AntispamProtection.MarkForMaloformedData(clientState.endpointIP);
                            }

                            ShutdownAndCloseSocket(clientState);
                            return;
                        }
                    }

                    Array.ConstrainedCopy(clientState.byteBuffer, 0, clientState.requestBuffer, clientState.requestBufferPosition, bytecount);
                    Array.Clear(clientState.byteBuffer, 0, Configuration.ServerVariables.BUFFER_SIZE);
                    clientState.requestBufferPosition += bytecount;
                    Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Recieved " + bytecount + " bytes (" + clientState.requestBufferPosition + " total bytes stored in instance)", ErrorSeverity.ERROR_INFO);
                    Debug.Logging.dbgMessageByteArray <byte>(clientState.requestBuffer);
                }

                clientState.timeoutTimer.Change(Configuration.ServerVariables.CLIENT_CONNECTION_TIMEOUT, Timeout.Infinite);
                clientState.workSocket.BeginReceive(clientState.byteBuffer, 0, Configuration.ServerVariables.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReadAsyncBytes), clientState);
            }
            catch (Exception e)
            {
                Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Something went wrong reading from socket:\n" + e.Message + "\n" + e.StackTrace, ErrorSeverity.ERROR_WARNING);
                ShutdownAndCloseSocket(clientState);
                return;
            }
        }
Exemplo n.º 2
0
 public void ReadAsyncDelayed(IAsyncResult result, ServerConnectionStateObject clientState)
 {
     try
     {
         clientState.timeoutTimer.Change(Configuration.ServerVariables.CLIENT_CONNECTION_TIMEOUT, Timeout.Infinite);
         clientState.workSocket.BeginReceive(clientState.byteBuffer, 0, Configuration.ServerVariables.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReadAsyncBytes), clientState);
     }
     catch (Exception e)
     {
         Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Socket read failure:\n" + e.Message + "\n" + e.StackTrace, ErrorSeverity.ERROR_WARNING);
         ShutdownAndCloseSocket(clientState);
         return;
     }
 }
Exemplo n.º 3
0
        public static void ClientResponseStatus(IAsyncResult result)
        {
            ServerConnectionStateObject clientState = (ServerConnectionStateObject)result.AsyncState;

            try
            {
                int responseSize = clientState.workSocket.EndSend(result);
                Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Sent data to client: " + responseSize + " Bytes.", ErrorSeverity.ERROR_INFO);
                ShutdownAndCloseSocket(clientState);
            }
            catch (Exception e)
            {
                Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Unable to send client data:\n" + e.Message + "\n" + e.StackTrace, ErrorSeverity.ERROR_WARNING);
                ShutdownAndCloseSocket(clientState);
                return;
            }
        }
Exemplo n.º 4
0
        public static void RespondToClient(ServerConnectionStateObject clientState, NetworkObject obj)
        {
            try
            {
                Console.WriteLine("Sending: " + JsonConvert.SerializeObject(obj).ToString());
                byte[] networkObjectBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(obj));

                clientState.workSocket.BeginSend(networkObjectBytes, 0, networkObjectBytes.Length, 0,
                                                 new AsyncCallback(ClientResponseStatus), clientState);
            }
            catch (Exception e)
            {
                Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Unable to send client data:\n" + e.Message + "\n" + e.StackTrace, ErrorSeverity.ERROR_WARNING);
                ShutdownAndCloseSocket(clientState);
                return;
            }
        }
Exemplo n.º 5
0
        private void HandleIncomingConnection(IAsyncResult ar)
        {
            try
            {
                threadFinished.Set();

                Socket handler = ((Socket)ar.AsyncState).EndAccept(ar);

                ServerConnectionStateObject clientState = new ServerConnectionStateObject()
                {
                    workSocket = handler
                };

                IPEndPoint remoteIP = (IPEndPoint)handler.RemoteEndPoint;
                clientState.endpointIP   = remoteIP.Address.ToString();
                clientState.endpointPort = remoteIP.Port.ToString();

                if (Configuration.SpamProtection.SPAM_PROTECTION_ENABLED)
                {
                    if (!AntispamProtection.CheckUser(clientState.endpointIP))
                    {
                        return;
                    }
                }


                Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Handling incoming connection request", ErrorSeverity.ERROR_INFO);

                if (ServerManager.ClientCanConnect())
                {
                    ServerManager.ConnectClient();
                    ReadAsyncDelayed(ar, clientState);
                }
                else
                {
                    Debug.Logging.errlog(Utils.connectionInfo(clientState) + "CPU full: offloading incoming request to queue", ErrorSeverity.ERROR_WARNING);
                    ServerManager.queuedClients.Enqueue(new DelayedQueueConnection(ar, clientState));
                }
            }
            catch (Exception e)
            {
                Debug.Logging.errlog("Failed to connect client:\n" + e.Message + "\n" + e.StackTrace, ErrorSeverity.ERROR_WARNING);
            }
        }
Exemplo n.º 6
0
        private void DecodeOperation(NetworkObject recievedNetworkObject, ServerConnectionStateObject clientState)
        {
            try
            {
                switch (recievedNetworkObject.requestType)
                {
                case NetObjectType.CLIENT_REQUEST_SERVER_LIST:
                    _opDef.HandleServerListRequest(clientState, recievedNetworkObject);
                    break;

                case NetObjectType.CLIENT_SERVER_MODIFY_REGISTERED_SERVER:
                    _opDef.HandleModifyExistingServerRequest(clientState, recievedNetworkObject);
                    break;

                case NetObjectType.CLIENT_SERVER_REGISTER_SERVER:
                    _opDef.HandleRegisterNewServer(clientState, recievedNetworkObject);
                    break;

                case NetObjectType.CLIENT_SERVER_RESPONSE_GENERIC:
                    _opDef.HandleRespondToClient(clientState, recievedNetworkObject);
                    break;

                case NetObjectType.CLIENT_SERVER_UNREGISTER_SERVER:
                    _opDef.HandleUnregisterServerRequest(clientState, recievedNetworkObject);
                    break;

                case NetObjectType.SERVER_SEND_MATCHMAKE:
                    _opDef.HandleMatchmakingRequest(clientState, recievedNetworkObject);
                    break;
                }
            }
            catch (Exception e)
            {
                Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Something went wrong when reading data!\n" + e.Message + "\n" + e.StackTrace, ErrorSeverity.ERROR_WARNING);
                ShutdownAndCloseSocket(clientState);
                return;
            }
        }
Exemplo n.º 7
0
 public static void ShutdownAndCloseSocket(ServerConnectionStateObject clientState)
 {
     try
     {
         if (clientState.workSocket.Connected && clientState.disconnectCounted == false)
         {
             clientState.workSocket.Shutdown(SocketShutdown.Both);
             clientState.workSocket.Close();
             ServerManager.DiconnectClient();
             clientState.disconnectCounted = true;
         }
         else if (clientState.disconnectCounted == false)
         {
             ServerManager.DiconnectClient();
             clientState.disconnectCounted = true;
         }
     }
     catch (Exception e)
     {
         Debug.Logging.errlog(Utils.connectionInfo(clientState) + "Failed to shutdown client:\n" + e.Message + "\n" + e.StackTrace, ErrorSeverity.ERROR_WARNING);
         return;
     }
 }
 public DelayedQueueConnection(IAsyncResult ar, ServerConnectionStateObject clientState)
 {
     this.ar          = ar;
     this.clientState = clientState;
 }