Beispiel #1
0
        /// <summary>
        /// Logs the memory useage using GC.GetTotalMemory and returns the memory useage
        /// </summary>
        /// <param name="p">Process to use to calculate memory</param>
        public static void LogMemory(Process p)
        {
            long mem = +GC.GetTotalMemory(false);

            if (mem > maxMemoryUseage)
            {
                maxMemoryUseage = mem;
            }
            Globals_Server.logEvent("GC memory: " + mem);
        }
Beispiel #2
0
 /// <summary>
 /// Send an update to the client- used when the message to be sent requires additional information other than just a response code and some strings
 /// </summary>
 /// <param name="message">Message to be sent- can contain any number of details</param>
 public void Update(ProtoMessage message)
 {
     Contract.Requires(message != null);
     message.ActionType = Actions.Update;
     if (conn != null)
     {
         Globals_Server.logEvent("Update " + this.username + ": " + message.ResponseType.ToString());
         Console.WriteLine("Sending update " + message.ResponseType.ToString() + " to " + this.username);
         Server.SendViaProto(message, conn, alg);
     }
 }
Beispiel #3
0
        /// <summary>
        /// Initialise the server, and store some test users and clients.
        /// </summary>
        private void initialise()
        {
            LogInManager.StoreNewUser("helen", "potato");
            LogInManager.StoreNewUser("test", "tomato");
            LogInManager.StoreNewUser("simon", "farshas");
            NetPeerConfiguration config = new NetPeerConfiguration(app_identifier);

            //config.BroadcastAddress = NetUtility.Resolve("* your external IP here in IPv4 format *"); // external IP of a machine that the server might be running on
            config.LocalAddress       = NetUtility.Resolve(host_name);
            config.MaximumConnections = max_connections;
            config.Port = port;
            config.SetMessageTypeEnabled(NetIncomingMessageType.ConnectionApproval, true);
            config.SetMessageTypeEnabled(NetIncomingMessageType.ConnectionLatencyUpdated, true);
            config.PingInterval      = 10f;
            config.ConnectionTimeout = 100f;
            server   = new NetServer(config);
            ctSource = new CancellationTokenSource();
            server.Start();
            Globals_Server.server = server;
            Globals_Server.logEvent("Server started- host: " + config.BroadcastAddress + ", port: " + port + ", appID: " +
                                    app_identifier + ", max connections: " + max_connections);
            Client client = new Client("helen", "Char_158");

            Globals_Server.Clients.Add("helen", client);
            Client client2 = new Client("test", "Char_196");

            Globals_Server.Clients.Add("test", client2);
            Client client3 = new Client("simon", "Char_283");

            Globals_Server.Clients.Add("simon", client3);
            String        dir = Directory.GetCurrentDirectory();
            List <string> pcs = Globals_Game.pcKeys;                 // to potentially add a "create user function"

            pcs.Remove("Char_158");
            pcs.Remove("Char_195");
            pcs.Remove("Char_283");
            //dir = dir.Remove(dir.IndexOf("RepairHist_mmo"));
            String path;

            if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
            {
                path = Path.Combine(dir, "Certificates");
            }
            else
            {
                dir  = Directory.GetParent(dir).FullName;
                dir  = Directory.GetParent(dir).FullName;
                dir  = Directory.GetParent(dir).FullName;
                path = Path.Combine(dir, "Certificates");
            }
            Diplomacy.forgeAlliance("Char_283", "Char_196");
            Diplomacy.declareWar("Char_283", "Char_158");
            LogInManager.InitialiseCertificateAndRSA(path);
        }
Beispiel #4
0
        /// <summary>
        /// Updates the client
        /// </summary>
        /// <param name="message">The message code to send</param>
        /// <param name="fields">Additional information to add to the message</param>
        public void Update(DisplayMessages message, string[] fields = null)
        {
            ProtoMessage m = new ProtoMessage();

            m.ActionType    = Actions.Update;
            m.ResponseType  = message;
            m.MessageFields = fields;
            if (conn != null)
            {
                Globals_Server.logEvent("Update " + this.username + ": " + message.ToString());
                Console.WriteLine("Sending update " + message.ToString() + " to " + this.username);
                Server.SendViaProto(m, conn, alg);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Processes a client disconnecting from the server- removes the client as an observer, removes their connection and deletes their CryptoServiceProvider
        /// </summary>
        /// <param name="conn">Connection of the client who disconnected</param>
        private void Disconnect(NetConnection conn)
        {
            Contract.Requires(conn != null);
            lock (ServerLock)
            {
                if (clientConnections.ContainsKey(conn))
                {
                    Client client = clientConnections[conn];
                    Globals_Server.logEvent("Client " + client.username + " disconnects");
                    Globals_Game.RemoveObserver(client);
                    client.conn = null;
                    clientConnections.Remove(conn);

                    client.alg = null;
                    conn.Disconnect("Disconnect");
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Read a message, get the relevant reply and send to client
        /// </summary>
        /// <param name="m">Deserialised message from client</param>
        /// <param name="connection">Client's connecton</param>
        public void ProcessMessage(ProtoMessage m, NetConnection connection)
        {
            Contract.Requires(connection != null && m != null);
            Client client;

            clientConnections.TryGetValue(connection, out client);
            if (client == null)
            {
                NetOutgoingMessage errorMessage =
                    server.CreateMessage("There was a problem with the connection. Please try re-connecting");
                server.SendMessage(errorMessage, connection, NetDeliveryMethod.ReliableOrdered);
                string log = "Connection from peer " + connection.Peer.UniqueIdentifier +
                             " not found in client connections. Timestamp: " +
                             DateTime.Now.ToString(DateTimeFormatInfo.CurrentInfo);
                Globals_Server.logError(log);
                return;
            }
            var pc = client.myPlayerCharacter;

            if (pc == null || !pc.isAlive)
            {
                NetOutgoingMessage msg = server.CreateMessage("You have no valid PlayerCharacter!");
                server.SendMessage(msg, connection, NetDeliveryMethod.ReliableOrdered);
                server.FlushSendQueue();
            }
            else
            {
                ProtoMessage reply = Game.ActionController(m, client);
                // Set action type to ensure client knows which action invoked response
                if (reply == null)
                {
                    ProtoMessage invalid = new ProtoMessage(DisplayMessages.ErrorGenericMessageInvalid);
                    invalid.ActionType = Actions.Update;
                    reply = invalid;
                }
                else
                {
                    reply.ActionType = m.ActionType;
                }
                SendViaProto(reply, connection, client.alg);
                Globals_Server.logEvent("From " + clientConnections[connection] + ": request = " +
                                        m.ActionType.ToString() + ", reply = " + reply.ResponseType.ToString());
            }
        }
Beispiel #7
0
        public static void TestRun(bool encrypt = true)
        {
            Process currentProcess = Process.GetCurrentProcess();

            if (encrypt)
            {
                Globals_Server.logEvent("Running test with encryption");
            }
            else
            {
                Globals_Server.logEvent("Running test without encryption");
            }
            double LoginTime, RecruitTime, MoveTime, SpyTime;
            double start = DateTime.Now.TimeOfDay.TotalMilliseconds;

            byte[] encryptionKey = null;
            if (encrypt)
            {
                encryptionKey = LogInManager.GetRandomSalt(32);
            }
            client.LogInAndConnect(Username, Pass, encryptionKey);
            while (!client.IsConnectedAndLoggedIn())
            {
                Thread.Sleep(0);
            }
            LoginTime = DateTime.Now.TimeOfDay.TotalMilliseconds - start;
            client.RecruitTroops(OwnedArmy.armyID, 70, true);
            RecruitTime = ProcessNextAction(Actions.RecruitTroops, currentProcess);
            // Move to another fief
            client.Move(MyPlayerCharacter.charID, NotOwnedFief.id, null);
            MoveTime = ProcessNextAction(Actions.TravelTo, currentProcess);
            // Spy
            client.SpyOnFief(MyPlayerCharacter.charID, MyPlayerCharacter.location.id);
            SpyTime = ProcessNextAction(Actions.SpyFief, currentProcess);
            // Confirm spy
            Globals_Server.logEvent("Time taken to run test (ms): " + (DateTime.Now.TimeOfDay.TotalMilliseconds - start));
            Globals_Server.logEvent("LogIn time: " + LoginTime);
            Globals_Server.logEvent("Recruit time: " + (RecruitTime));
            Globals_Server.logEvent("Travel time: " + MoveTime);
            Globals_Server.logEvent("Spy time: " + SpyTime);
            Globals_Server.logEvent("Max memory measured: " + maxMemoryUseage);
        }
Beispiel #8
0
        public void Listen()
        {
            while (server.Status == NetPeerStatus.Running && !ctSource.Token.IsCancellationRequested)
            {
                NetIncomingMessage im;
                WaitHandle.WaitAny(new WaitHandle[] { server.MessageReceivedEvent, ctSource.Token.WaitHandle });
                while ((im = server.ReadMessage()) != null && !ctSource.Token.IsCancellationRequested)
                {
                    switch (im.MessageType)
                    {
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.ErrorMessage:
                    case NetIncomingMessageType.WarningMessage:
                        Globals_Server.logError("Recieved warning message: " + im.ReadString());
                        break;

                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.Data:
                    {
#if DEBUG
                        Console.WriteLine("SERVER: recieved data message");
#endif
                        if (!clientConnections.ContainsKey(im.SenderConnection))
                        {
                            //error
                            im.SenderConnection.Disconnect("Not recognised");
                            return;
                        }
                        Client c = clientConnections[im.SenderConnection];
                        if (c.alg != null)
                        {
                            im.Decrypt(c.alg);
                        }
                        ProtoMessage m = null;
                        //global::ProtoMessage.ProtoMessage y = null;
                        using (MemoryStream ms = new MemoryStream(im.Data))
                        {
                            try
                            {
                                //y = Serializer.DeserializeWithLengthPrefix<global::ProtoMessage.ProtoMessage>(ms,
                                //PrefixStyle.Fixed32);
                                m = Serializer.DeserializeWithLengthPrefix <ProtoMessage>(ms, PrefixStyle.Fixed32);
                            }
                            catch (Exception e)
                            {
                                NetOutgoingMessage errorMessage = server.CreateMessage(
                                    "Failed to deserialise message. The message may be incorrect, or the decryption may have failed.");
                                if (c.alg != null)
                                {
                                    errorMessage.Encrypt(c.alg);
                                }
                                server.SendMessage(errorMessage, im.SenderConnection,
                                                   NetDeliveryMethod.ReliableOrdered);
                                Globals_Server.logError("Failed to deserialize message for client: " + c.username);
                            }
                        }
                        if (m == null /*&& y == null*/)
                        {
                            string error = "Recieved null message from " + im.SenderEndPoint.ToString();
                            if (clientConnections.ContainsKey(im.SenderConnection))
                            {
                                error += ", recognised client " + clientConnections[im.SenderConnection];
                            }
                            else
                            {
                                error += ", unrecognised client (possible ping)";
                            }
                            error += ". Data: " + im.ReadString();
                            Globals_Server.logError(error);
                            break;
                        }

                        if (m.ActionType == Actions.LogIn)
                        {
                            ProtoLogIn login = m as ProtoLogIn;
                            if (login == null)
                            {
                                im.SenderConnection.Disconnect("Not login");
                                return;
                            }
                            lock (ServerLock)
                            {
                                if (LogInManager.VerifyUser(c.username, login.userSalt))
                                {
                                    if (LogInManager.ProcessLogIn(login, c))
                                    {
                                        string log = c.username + " logs in from " + im.SenderEndPoint.ToString();
                                        Globals_Server.logEvent(log);
                                    }
                                }
                                else
                                {
                                    ProtoMessage reply = new ProtoMessage
                                    {
                                        ActionType   = Actions.LogIn,
                                        ResponseType = DisplayMessages.LogInFail
                                    };
                                    im.SenderConnection.Disconnect("Authentication Fail");
                                }
                            }
                        }
                        // temp for testing, should validate connection first
                        else if (clientConnections.ContainsKey(im.SenderConnection))
                        {
                            if (Globals_Game.IsObserver(c))
                            {
                                ProcessMessage(m, im.SenderConnection);
                                ProtoClient clientDetails = new ProtoClient(c);
                                clientDetails.ActionType = Actions.Update;
                                SendViaProto(clientDetails, im.SenderConnection, c.alg);
                            }
                            else
                            {
                                im.SenderConnection.Disconnect("Not logged in- Disconnecting");
                            }
                        }

                        /*//IF Y ACTION
                         *  if (y.ActionType == global::ProtoMessage.Actions.LogIn)
                         *  {
                         *      global::ProtoMessage.Client forCheck = new global::ProtoMessage.Client(c.username, c.myPlayerCharacter.playerID);
                         *      global::ProtoMessage.ProtoLogIn login = y as global::ProtoMessage.ProtoLogIn;
                         *      if (login == null)
                         *      {
                         *          im.SenderConnection.Disconnect("Not login");
                         *          return;
                         *      }
                         *      lock (ServerLock)
                         *      {
                         *          if (LogInManager.VerifyUser(c.username, login.userSalt))
                         *          {
                         *              if (LogInManager.ProcessLogIn(login, forCheck, true))
                         *              {
                         *                  string log = c.username + " logs in from " + im.SenderEndPoint.ToString();
                         *                  Globals_Server.logEvent(log);
                         *              }
                         *          }
                         *          else
                         *          {
                         *              ProtoMessage reply = new ProtoMessage
                         *              {
                         *                  ActionType = Actions.LogIn,
                         *                  ResponseType = DisplayMessages.LogInFail
                         *              };
                         *              im.SenderConnection.Disconnect("Authentication Fail");
                         *          }
                         *      }
                         *  }
                         *  // temp for testing, should validate connection first
                         *  else if (clientConnections.ContainsKey(im.SenderConnection))
                         *  {
                         *      if (Globals_Game.IsObserver(c))
                         *      {
                         *          ProcessMessage(y, im.SenderConnection);
                         *          ProtoClient clientDetails = new ProtoClient(c);
                         *          clientDetails.ActionType = Actions.Update;
                         *          SendViaProto(clientDetails, im.SenderConnection, c.alg);
                         *      }
                         *      else
                         *      {
                         *          im.SenderConnection.Disconnect("Not logged in- Disconnecting");
                         *      }
                         *  }*/
                    }
                    break;

                    case NetIncomingMessageType.StatusChanged:
                        byte stat = im.ReadByte();
                        NetConnectionStatus status = NetConnectionStatus.None;
                        if (Enum.IsDefined(typeof(NetConnectionStatus), Convert.ToInt32(stat)))
                        {
                            status = (NetConnectionStatus)stat;
                        }
                        else
                        {
                            Globals_Server.logError("Failure to parse byte " + stat + " to NetConnectionStatus for endpoint " + im.ReadIPEndPoint());
                        }
                        if (status == NetConnectionStatus.Disconnected)
                        {
                            if (clientConnections.ContainsKey(im.SenderConnection))
                            {
                                Disconnect(im.SenderConnection);
                            }
                        }
                        break;

                    case NetIncomingMessageType.ConnectionApproval:
                    {
                        string senderID = im.ReadString();
                        string text     = im.ReadString();
                        Client client;
                        Globals_Server.Clients.TryGetValue(senderID, out client);
                        if (client != null)
                        {
                            ProtoLogIn logIn;
                            if (!LogInManager.AcceptConnection(client, text, out logIn))
                            {
                                im.SenderConnection.Deny();
                            }
                            else
                            {
                                NetOutgoingMessage msg = server.CreateMessage();
                                MemoryStream       ms  = new MemoryStream();
                                // Include X509 certificate as bytes for client to validate
                                Serializer.SerializeWithLengthPrefix <ProtoLogIn>(ms, logIn, PrefixStyle.Fixed32);
                                msg.Write(ms.GetBuffer());
                                clientConnections.Add(im.SenderConnection, client);
                                client.conn = im.SenderConnection;
                                im.SenderConnection.Approve(msg);
                                server.FlushSendQueue();
                                Globals_Server.logEvent("Accepted connection from " + client.username);
                            }
                        }
                        else
                        {
                            im.SenderConnection.Deny("unrecognised");
                        }
                    }

                    break;

                    case NetIncomingMessageType.ConnectionLatencyUpdated:
                        break;

                    default:
                        Globals_Server.logError("Received unrecognised incoming message type: " + im.MessageType);
                        break;
                    }
                    server.Recycle(im);
                }
            }
#if DEBUG
            Globals_Server.logEvent("Server listening thread exits");
#endif
        }
Beispiel #9
0
            public void read()
            {
                while (client.Status == NetPeerStatus.Running && !ctSource.Token.IsCancellationRequested)
                {
                    WaitHandle.WaitAny(new WaitHandle[] { client.MessageReceivedEvent, ctSource.Token.WaitHandle });
                    NetIncomingMessage im;
                    while ((im = client.ReadMessage()) != null && !ctSource.IsCancellationRequested)
                    {
                        switch (im.MessageType)
                        {
                        case NetIncomingMessageType.DebugMessage:
                        case NetIncomingMessageType.ErrorMessage:
                        case NetIncomingMessageType.WarningMessage:
                        case NetIncomingMessageType.VerboseDebugMessage:
                        case NetIncomingMessageType.Data:
                            try
                            {
                                if (alg != null)
                                {
                                    im.Decrypt(alg);
                                }
                                MemoryStream ms = new MemoryStream(im.Data);
                                ProtoMessage m  = null;
                                try
                                {
                                    m = Serializer.DeserializeWithLengthPrefix <ProtoMessage>(ms, PrefixStyle.Fixed32);
                                }
                                catch (Exception e)
                                {
                                    // Attempt to read string and add to message queue
                                    string s = im.ReadString();
                                    if (!string.IsNullOrWhiteSpace(s))
                                    {
                                        Console.WriteLine("CLIENT: Got message: " + s);

                                        tClient.stringMessageQueue.Enqueue(s);
                                    }
                                }
                                if (m != null)
                                {
                                    Console.WriteLine("CLIENT: Got ProtoMessage with ActionType: " + m.ActionType + " and response type: " + m.ResponseType);
                                    if (m.ResponseType == DisplayMessages.LogInSuccess)
                                    {
                                        loggedIn = true;
                                        tClient.protobufMessageQueue.Enqueue(m);
                                    }
                                    else
                                    {
                                        if (m.ActionType == Actions.Update)
                                        {
                                            // Don't do anything at the moment for updates
                                        }
                                        else
                                        {
                                            tClient.protobufMessageQueue.Enqueue(m);
                                            if (m.ActionType == Actions.LogIn && m.ResponseType == DisplayMessages.None)
                                            {
                                                byte[] key = null;
                                                if (ValidateCertificateAndCreateKey(m as ProtoLogIn, out key))
                                                {
                                                    ComputeAndSendHashAndKey(m as ProtoLogIn, key);
                                                }
                                            }
                                            else
                                            {
                                                // Attempt to read string and add to message queue
                                                string s = im.ReadString();
                                                if (!string.IsNullOrWhiteSpace(s))
                                                {
                                                    tClient.stringMessageQueue.Enqueue(s);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Globals_Server.logError("Error in reading data: " + e.GetType() + " :" + e.Message + "; Stack Trace: " + e.StackTrace);
                            }
                            break;

                        case NetIncomingMessageType.StatusChanged:

                            NetConnectionStatus status = (NetConnectionStatus)im.ReadByte();
                            Console.WriteLine("CLIENT: Status changed to " + status.ToString());
                            //MemoryStream ms2 = new MemoryStream(im.SenderConnection.RemoteHailMessage.Data);
                            if (status == NetConnectionStatus.Connected)
                            {
                                if (im.SenderConnection.RemoteHailMessage != null)
                                {
                                    try
                                    {
                                        MemoryStream ms2 = new MemoryStream(im.SenderConnection.RemoteHailMessage.Data);
                                        ProtoMessage m   = Serializer.DeserializeWithLengthPrefix <ProtoMessage>(ms2, PrefixStyle.Fixed32);
                                        if (m != null)
                                        {
                                            tClient.protobufMessageQueue.Enqueue(m);
                                            if (m.ActionType == Actions.LogIn && m.ResponseType == DisplayMessages.None)
                                            {
                                                byte[] key = null;
                                                if (ValidateCertificateAndCreateKey(m as ProtoLogIn, out key))
                                                {
                                                    if (autoLogIn)
                                                    {
                                                        ComputeAndSendHashAndKey(m as ProtoLogIn, key);
                                                    }
                                                }
                                                else
                                                {
                                                    Console.WriteLine("Certificate validation failed: Server may be untrusted");
                                                    client.Disconnect("Invalid Certificate");
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                    }
                                }
                                break;
                            }
                            else if (status == NetConnectionStatus.Disconnected)
                            {
                                string reason = im.ReadString();
                                if (!string.IsNullOrEmpty(reason))
                                {
                                    tClient.stringMessageQueue.Enqueue(reason);
                                }
                            }
                            if (im.SenderConnection.RemoteHailMessage != null && (NetConnectionStatus)im.ReadByte() == NetConnectionStatus.Connected)
                            {
                            }
                            break;

                        case NetIncomingMessageType.ConnectionLatencyUpdated:
                            break;

                        default:
                            break;
                        }
                        client.Recycle(im);
                    }
                }
#if DEBUG
                Globals_Server.logEvent("Client listening thread ends");
#endif
            }