/// <summary>
        /// Processes received packets.
        /// </summary>
        /// <param name="packet">Received packet.</param>
        void ProcessPacket(NetworkPacket packet)
        {
            // Decrypt data
            var data = packet.Data;

            if (data != null && data.Length > 0 && packet.PacketType != NetworkPacketType.PublicKey)
            {
                if (packet.PacketType != NetworkPacketType.ServerInfo)
                {
                    data = _decrypter.Decrypt(packet.Data);
                }
                if (data == null)
                {
                    Log.WriteLine(LogPriority.Error, "NetworkGameClient: Error! Could not decrypt message from server");
                    return;
                }
            }
            switch (packet.PacketType)
            {
            case NetworkPacketType.PublicKey:
                try
                {
                    var serverPubKey = Encoding.ASCII.GetString(packet.Data);
                    _encrypter = new CryptoHelper(serverPubKey);
                }
                catch (Exception ex)
                {
                    _encrypter = null;
                    Log.WriteLine(LogPriority.Error, "NetworkGameClient: Received key is invalid! " + ex.Message);
                }
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
#pragma warning disable CC0004   // Catch block cannot be empty
                finally { try { _encrypterWaiter?.Cancel(); } catch { /* Ignore*/ } }
#pragma warning restore CC0004   // Catch block cannot be empty
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
                break;

            case NetworkPacketType.ListGames:
                try
                {
                    var glistString = Encoding.ASCII.GetString(data);
                    var glist       = JsonConvert.DeserializeObject <List <GameInfo> >(glistString);
                    Log.WriteLine(LogPriority.Verbose, "NetworkGameClient: Received list of games on server. Game count: " + glist.Count);
                    GameListReceived?.Invoke(this, new GameListEventArgs(glist));
                }
                catch (Exception e)
                {
                    Log.WriteLine(LogPriority.Error, "NetworkGameClient: Received list of games from server could not be read. " + e.Message);
                }
                break;

            case NetworkPacketType.ClientLogin:
                try
                {
                    if (data.Length != 1 || data[0] > 4)
                    {
                        Log.WriteLine(LogPriority.Information, "NetworkGameClient: Received login confirmation could not be read: No data!");
                        break;
                    }
                    LoginCheckResult loginResult = (LoginCheckResult)(int)data[0];
                    if (loginResult == LoginCheckResult.LoginOK)
                    {
                        IsLoggedIn = true;
                        Log.WriteLine(LogPriority.Information, "NetworkGameClient: Login to server successful!");
                    }
                    else
                    {
                        IsLoggedIn = false;
                        var error = "NetworkGameClient: Login to server failed! ";
                        switch (loginResult)
                        {
                        case LoginCheckResult.InvalidUser:
                            error += "Invalid user name";
                            break;

                        case LoginCheckResult.InvalidPassword:
                            error += "Invalid password";
                            break;

                        case LoginCheckResult.DatabaseError:
                            error += "Database error";
                            break;

                        case LoginCheckResult.AuthorizationRequired:
                            error += "Authorization required";
                            break;

                        case LoginCheckResult.CreateAccountDenied:
                            error += "Create account denied";
                            break;

                        default:
                            error += "Unknown error";
                            break;
                        }
                        Log.WriteLine(LogPriority.Information, error);
                    }
                    LoggedIn?.Invoke(this, new LoggedInEventArgs(loginResult));
                }
                catch (Exception e)
                {
                    Log.WriteLine(LogPriority.Error, "NetworkGameClient: Received login confirmation could not be read." + e.Message);
                }
                break;

            case NetworkPacketType.CreateGame:
                try
                {
                    // Check if an UID was assigned
                    var ginfo = JsonConvert.DeserializeObject <GameInfo>(Encoding.ASCII.GetString(data));
                    if (ginfo != null)
                    {
                        if (ginfo.UID != 0)
                        {
                            GameCreated?.Invoke(this, new GameCreatedEventArgs(ginfo));
                        }
                    }
                    else
                    {
                        Log.WriteLine(LogPriority.Error, "NetworkGameClient: Received CreateGame confirmation could not be read.");
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine(LogPriority.Error, "NetworkGameClient: Received CreateGame confirmation could not be read." + e.Message);
                }
                break;

            case NetworkPacketType.JoinGame:
                try
                {
                    var jMsg = JsonConvert.DeserializeObject <JoinMessage>(Encoding.ASCII.GetString(data));
                    if (jMsg != null)
                    {
                        GameJoinRequested?.Invoke(this, new GameJoinRequestedEventArgs(jMsg));

                        // If joining and other side declined then set IsJoined to false
                        if (jMsg.Request == JoinRequestType.Decline)
                        {
                            IsJoined = false;
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine(LogPriority.Error, "NetworkGameClient: Received JoinGame confirmation could not be read." + e.Message);
                }
                break;

            case NetworkPacketType.ServerInfo:
                try
                {
                    // Server data is always unencrypted
                    var jMsg = JsonConvert.DeserializeObject <ServerInfo>(Encoding.ASCII.GetString(packet.Data));
                    if (jMsg != null)
                    {
                        ServerRequiresLogin = jMsg.RequiresLogin;
                        ServerInfoReceived?.Invoke(this, new ServerInfoEventArgs(jMsg));
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine(LogPriority.Error, "NetworkGameClient: Received ServerInfo could not be read." + e.Message);
                }
                break;

            case NetworkPacketType.ExitGame:
                try
                {
                    var eMsg = JsonConvert.DeserializeObject <ExitGame>(Encoding.ASCII.GetString(data));
                    if (eMsg != null)
                    {
                        if (eMsg.UID == UID)
                        {
                            IsJoined = false;
                            UID      = 0;
                            GameExitReceived?.Invoke(this, new ExitGameEventArgs(eMsg.Reason));
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine(LogPriority.Error, "NetworkGameClient: Received ExitGame message could not be read." + e.Message);
                }
                break;

            case NetworkPacketType.GameSync:
                try
                {
                    var eMsg = JsonConvert.DeserializeObject <GameSync>(Encoding.ASCII.GetString(data));
                    if (eMsg != null && eMsg.UID == UID)
                    {
                        GameSyncReceived?.Invoke(this, new GameSyncEventArgs(eMsg));
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine(LogPriority.Error, "NetworkGameClient: Received GameSync message could not be read." + e.Message);
                }
                break;

            case NetworkPacketType.GameCommand:
                try
                {
                    var eMsg = JsonConvert.DeserializeObject <GameCommand>(Encoding.ASCII.GetString(data));
                    if (eMsg != null)
                    {
                        GameCommandReceived?.Invoke(this, new GameCommandEventArgs(eMsg));
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine(LogPriority.Error, "NetworkGameClient: Received GameSync message could not be read." + e.Message);
                }
                break;

            default:
                Log.WriteLine(LogPriority.Error, "NetworkGameClient: Packet type " + packet.PacketType + " not recognized!");
                break;
            }
        }
Beispiel #2
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="connection">Connection to player.</param>
 /// <param name="uid">Unique ID.</param>
 /// <param name="serverCrypto">Decryptor for player data.</param>
 public NetworkPlayer(Socket connection, uint uid, CryptoHelper serverCrypto)
 {
     Connection   = connection;
     UID          = uid;
     ServerCrypto = serverCrypto;
 }
 /// <summary>Constructor.</summary>
 public NetworkGameClient()
 {
     _decrypter = new CryptoHelper();
 }
 /// <summary>
 /// Sends data over the provides connection and optionally encrypts it.
 /// </summary>
 /// <param name="message">Message to send.</param>
 /// <param name="packetType">Packet type.</param>
 /// <param name="connection">Connection to use.</param>
 /// <param name="encrypter">Optional encryptor.</param>
 /// <returns>True if send was successful.</returns>
 protected bool Send(string message, byte packetType, Socket connection, CryptoHelper encrypter = null)
 {
     return(Send(Encoding.ASCII.GetBytes(message), packetType, connection, encrypter));
 }