Ejemplo n.º 1
0
        private void HandleMatchGameMsg(InsightMessage _insightMsg)
        {
            var message = (MatchGameMsg)_insightMsg.message;

            Debug.Log("[ServerMatchMaker] - Received requesting match game");

            server.InternalSend(new JoinGameMsg {
                uniqueId     = message.uniqueId,
                gameUniqueId = GetFastestGame()
            }, _callbackMsg => {
                if (_insightMsg.callbackId != 0)
                {
                    var responseToSend = new InsightNetworkMessage(_callbackMsg)
                    {
                        callbackId = _insightMsg.callbackId
                    };

                    if (_insightMsg is InsightNetworkMessage netMsg)
                    {
                        server.NetworkReply(netMsg.connectionId, responseToSend);
                    }
                    else
                    {
                        server.InternalReply(responseToSend);
                    }
                }
            });
        }
Ejemplo n.º 2
0
        private void HandleChatMsg(InsightMessage _insightMsg)
        {
            if (_insightMsg is InsightNetworkMessage netMsg)
            {
                var message = (ChatMsg)_insightMsg.message;

                Debug.Log("[ChatServer] - Received Chat Message.");

                //Inject the username into the message
                message.username = authModule.registeredUsers.Find(_e => _e.connectionId == netMsg.connectionId)
                                   .username;

                if (gameModule != null)
                {
                    foreach (var playerConnId in gameModule.GetPlayersInGame(netMsg.connectionId))
                    {
                        server.NetworkSend(playerConnId, message);
                    }
                }
                else
                {
                    foreach (var user in authModule.registeredUsers)
                    {
                        server.NetworkSend(user.connectionId, message);
                    }
                }
            }
            else
            {
                Debug.Log("[ChatServer] - Rejected (Internal) Chat Message.");
            }
        }
Ejemplo n.º 3
0
        private void HandleRegisterSpawnerMsg(InsightMessage _insightMsg)
        {
            var message = (RegisterSpawnerMsg)_insightMsg.message;

            Debug.Log("[MasterSpawner] - Received process spawner registration");

            var connectionId   = _insightMsg is InsightNetworkMessage netMsg ? netMsg.connectionId : 0;
            var uniqueId       = Guid.NewGuid().ToString();
            var responseToSend = new InsightNetworkMessage(
                new RegisterSpawnerMsg {
                uniqueId = uniqueId
            })
            {
                callbackId = _insightMsg.callbackId,
                status     = CallbackStatus.Success
            };

            ReplyToSpawner(connectionId, responseToSend);

            registeredSpawners.Add(new SpawnerContainer {
                connectionId = connectionId,
                uniqueId     = uniqueId,
                maxThreads   = message.maxThreads
            });
        }
Ejemplo n.º 4
0
        private void HandleRegisterPlayerMsg(InsightMessage _insightMsg)
        {
            if (_insightMsg is InsightNetworkMessage netMsg)
            {
                // var message = (RegisterPlayerMsg) insightMsg.message;

                Debug.Log("[GameMasterManager] - Received player registration");

                var playerUniqueId = Guid.NewGuid().ToString();

                registeredPlayers.Add(new PlayerContainer {
                    connectionId = netMsg.connectionId,
                    uniqueId     = playerUniqueId
                });

                var responseToSend = new InsightNetworkMessage(
                    new RegisterPlayerMsg {
                    uniqueId = playerUniqueId
                })
                {
                    callbackId = _insightMsg.callbackId,
                    status     = CallbackStatus.Success
                };
                server.NetworkReply(netMsg.connectionId, responseToSend);
            }
            else
            {
                Debug.LogError("[GameMasterManager] - Rejected (Internal) player registration");
            }
        }
Ejemplo n.º 5
0
        private void HandleGameListMsg(InsightMessage _insightMsg)
        {
            // var message = (GameListMsg) insightMsg.message;

            Debug.Log("[GameMasterManager] - Received player requesting game list");

            var gamesListMsg = new GameListMsg();

            gamesListMsg.Load(registeredGameServers);

            var responseToSend = new InsightNetworkMessage(gamesListMsg)
            {
                callbackId = _insightMsg.callbackId,
                status     = CallbackStatus.Success
            };

            if (_insightMsg is InsightNetworkMessage netMsg)
            {
                server.NetworkReply(netMsg.connectionId, responseToSend);
            }
            else
            {
                server.InternalReply(responseToSend);
            }
        }
Ejemplo n.º 6
0
        private void HandleLeaveGameMsg(InsightMessage _insightMsg)
        {
            var message = (LeaveGameMsg)_insightMsg.message;

            Debug.Log("[GameMasterManager] - Received player requesting leave the game ");

            playersInGame.Remove(message.uniqueId);
        }
Ejemplo n.º 7
0
        private void HandleChatMsg(InsightMessage _insightMsg)
        {
            Debug.Log("[ChatClient] - Receive chatting");

            var message = (ChatMsg)_insightMsg.message;

            ReceiveMessage(message);
        }
Ejemplo n.º 8
0
        private void HandleSpawnerStatusMsg(InsightMessage _insightMsg)
        {
            var message = (SpawnerStatusMsg)_insightMsg.message;

            Debug.Log("[MasterSpawner] - Received process spawner update");

            var spawner = registeredSpawners.Find(_e => _e.uniqueId == message.uniqueId);

            Assert.IsNotNull(spawner);
            spawner.currentThreads = message.currentThreads;
        }
Ejemplo n.º 9
0
        private void HandleJoinGameMsg(InsightMessage _insightMsg)
        {
            var message = (JoinGameMsg)_insightMsg.message;

            Debug.Log($"[GameMasterManager] - Received player requesting join the game : {message.gameUniqueId}");

            var game = registeredGameServers.Find(_e => _e.uniqueId == message.gameUniqueId);

            Assert.IsNotNull(game);

            if (game.currentPlayers < game.maxPlayers)
            {
                Assert.IsTrue(registeredPlayers.Exists(_e => _e.uniqueId == message.uniqueId));
                playersInGame.Add(message.uniqueId, message.gameUniqueId);

                var changeServerMsg = new ChangeServerMsg {
                    uniqueId       = game.uniqueId,
                    networkAddress = game.networkAddress,
                    networkPort    = game.networkPort
                };

                var responseToSend = new InsightNetworkMessage(changeServerMsg)
                {
                    callbackId = _insightMsg.callbackId,
                    status     = CallbackStatus.Success
                };

                if (_insightMsg is InsightNetworkMessage netMsg)
                {
                    server.NetworkReply(netMsg.connectionId, responseToSend);
                }
                else
                {
                    server.InternalReply(responseToSend);
                }
            }
            else
            {
                var responseToSend = new InsightNetworkMessage(new ChangeServerMsg())
                {
                    callbackId = _insightMsg.callbackId,
                    status     = CallbackStatus.Error
                };

                if (_insightMsg is InsightNetworkMessage netMsg)
                {
                    server.NetworkReply(netMsg.connectionId, responseToSend);
                }
                else
                {
                    server.InternalReply(responseToSend);
                }
            }
        }
Ejemplo n.º 10
0
 protected override void Resend(InsightMessage _insightMsg, CallbackHandler _callback)
 {
     if (_insightMsg is InsightNetworkMessage netMsg)
     {
         NetworkSend(netMsg, _callback);
     }
     else
     {
         InternalSend(_insightMsg, _callback);
     }
 }
Ejemplo n.º 11
0
        private void Reply(InsightMessage _insightMsg)
        {
            if (client)
            {
                client.NetworkReply((InsightNetworkMessage)_insightMsg);
                return;
            }

            if (server)
            {
                server.InternalReply(_insightMsg);
                return;
            }
            Debug.LogError("[ProcessSpawner] - Not initialized");
        }
Ejemplo n.º 12
0
        private void HandleGameStatusMsg(InsightMessage _insightMsg)
        {
            var message = (GameStatusMsg)_insightMsg.message;

            Debug.Log("[GameMasterManager] - Received game update");

            var game = registeredGameServers.Find(_e => _e.uniqueId == message.game.uniqueId);

            Assert.IsNotNull(game);
            game.Update(message.game);

            foreach (var playerTemp in registeredPlayers)
            {
                server.NetworkSend(playerTemp.connectionId, new GameListStatusMsg {
                    operation = GameListStatusMsg.Operation.Update,
                    game      = game
                });
            }
        }
Ejemplo n.º 13
0
        private void HandleLoginMsg(InsightMessage _insightMsg)
        {
            if (_insightMsg is InsightNetworkMessage netMsg)
            {
                var message = (LoginMsg)_insightMsg.message;

                Debug.Log($"[ServerAuthentication] - Received login : {message.accountName} / {message.accountPassword}");

                if (Authenticated(message))                   //Login Sucessful
                {
                    var uniqueId = Guid.NewGuid().ToString();

                    registeredUsers.Add(new UserContainer {
                        connectionId = netMsg.connectionId,
                        uniqueId     = uniqueId,
                        username     = message.accountName
                    });

                    var responseToSend = new InsightNetworkMessage(new LoginMsg {
                        uniqueId = uniqueId
                    })
                    {
                        callbackId = _insightMsg.callbackId,
                        status     = CallbackStatus.Success
                    };
                    server.NetworkReply(netMsg.connectionId, responseToSend);
                }
                else                   //Login Failed
                {
                    var responseToSend = new InsightNetworkMessage(new LoginMsg())
                    {
                        callbackId = _insightMsg.callbackId,
                        status     = CallbackStatus.Error
                    };
                    server.NetworkReply(netMsg.connectionId, responseToSend);
                }
            }
            else
            {
                Debug.LogError("[ServerAuthentication] - Rejected (internal) login");
            }
        }
Ejemplo n.º 14
0
        //Instead of handling the msg here we will forward it to an available spawner.
        private void HandleSpawnRequestMsg(InsightMessage _insightMsg)
        {
            if (registeredSpawners.Count == 0)
            {
                Debug.LogError("[MasterSpawner] - No spawner regsitered to handle spawn request");
                return;
            }

            var message = (RequestSpawnStartToMasterMsg)_insightMsg.message;

            Debug.Log("[MasterSpawner] - Received requesting game creation");

            //Get all spawners that have atleast 1 slot free
            var freeSlotSpawners = registeredSpawners.FindAll(_e => _e.currentThreads < _e.maxThreads);

            //sort by least busy spawner first
            freeSlotSpawners = freeSlotSpawners.OrderBy(_e => _e.currentThreads).ToList();
            SendToSpawner(freeSlotSpawners[0].connectionId, message, _callbackMsg => {
                Debug.Log($"[MasterSpawner] - Game creation on child spawner : {_callbackMsg.status}");

                if (_insightMsg.callbackId != 0)
                {
                    var responseToSend = new InsightNetworkMessage(_callbackMsg)
                    {
                        callbackId = _insightMsg.callbackId
                    };

                    if (_insightMsg is InsightNetworkMessage netMsg)
                    {
                        server.NetworkReply(netMsg.connectionId, responseToSend);
                    }
                    else
                    {
                        server.InternalReply(responseToSend);
                    }
                }
            });
        }
Ejemplo n.º 15
0
        private void HandleRegisterGameMsg(InsightMessage _insightMsg)
        {
            if (_insightMsg is InsightNetworkMessage netMsg)
            {
                var message = (RegisterGameMsg)_insightMsg.message;

                Debug.Log("[GameMasterManager] - Received game registration");

                var game = new GameContainer {
                    connectionId = netMsg.connectionId,

                    uniqueId       = message.uniqueId,
                    networkAddress = message.networkAddress,
                    networkPort    = message.networkPort,
                    gameName       = message.gameName,
                    minPlayers     = message.minPlayers,
                    maxPlayers     = message.maxPlayers,
                    currentPlayers = message.currentPlayers,
                };

                registeredGameServers.Add(game);

                foreach (var playerTemp in registeredPlayers)
                {
                    server.NetworkSend(playerTemp.connectionId, new GameListStatusMsg {
                        operation = GameListStatusMsg.Operation.Add,
                        game      = game
                    });
                }

                LaunchGame(game.uniqueId);
            }
            else
            {
                Debug.LogError("[GameMasterManager] - Rejected (Internal) game registration");
            }
        }
Ejemplo n.º 16
0
        private void HandleKillSpawn(InsightMessage _insightMsg)
        {
            var message = (KillSpawnMsg)_insightMsg.message;

            spawnerProcesses.First(_e => _e.uniqueId == message.uniqueId).process.Kill();
        }
Ejemplo n.º 17
0
 public InsightNetworkMessage(InsightMessage _insightMsg) : base(_insightMsg)
 {
 }
Ejemplo n.º 18
0
        private void HandleRequestSpawnStart(InsightMessage _insightMsg)
        {
            var message = (RequestSpawnStartToSpawnerMsg)_insightMsg.message;

            Debug.Log("[ProcessSpawner] - Received requesting game creation");

            var successful = false;
            var thisPort   = GetPort();

            if (thisPort != -1)
            {
                //If a UniqueID was not provided add one for GameResitration
                if (string.IsNullOrEmpty(message.gameUniqueId))
                {
                    message.gameUniqueId = Guid.NewGuid().ToString();

                    Debug.LogWarning("[ProcessSpawner] - UniqueID was not provided for spawn. Generating: " +
                                     $"{message.gameUniqueId}");
                }

                var args = ArgsString() +
                           Space + ArgNames.UniqueId + Space + message.gameUniqueId +
                           Space + ArgNames.NetworkAddress + Space + spawnerNetworkAddress +
                           Space + ArgNames.NetworkPort + Space + (startingNetworkPort + thisPort) +
                           Space + ArgNames.GameName + Space + message.gameName +
                           Space + ArgNames.MinPlayers + Space + message.minPlayers +
                           Space + ArgNames.MaxPlayers + Space + message.maxPlayers;

                var processInfo = new ProcessStartInfo {
                    FileName        = System.IO.Path.Combine(processPath, processName),
                    Arguments       = args,
                    UseShellExecute = false
                };

                var process = Process.Start(processInfo);
                if (process != null)
                {
                    Debug.Log(
                        $"[ProcessSpawner] - Spawning : {process.StartInfo.FileName}; args= {process.StartInfo.Arguments}");
                    process.EnableRaisingEvents = true;
                    process.Exited += OnProcessExited;

                    Send(new SpawnerStatusMsg {
                        uniqueId       = uniqueId,
                        currentThreads = GetRunningProcessCount()
                    });

                    spawnerProcesses[thisPort] = new RunningProcessContainer {
                        process  = process,
                        uniqueId = message.gameUniqueId
                    };

                    successful = true;
                }
            }

            if (_insightMsg.callbackId != 0)
            {
                if (successful)
                {
                    var requestSpawnStartMsg = new RequestSpawnStartToSpawnerMsg {
                        gameUniqueId   = message.gameUniqueId,
                        networkAddress = spawnerNetworkAddress,
                        networkPort    = (ushort)(startingNetworkPort + thisPort),
                    };

                    var responseToSend = new InsightNetworkMessage(requestSpawnStartMsg)
                    {
                        callbackId = _insightMsg.callbackId,
                        status     = CallbackStatus.Success
                    };
                    Reply(responseToSend);
                }
                else
                {
                    var responseToSend = new InsightNetworkMessage(new RequestSpawnStartToSpawnerMsg())
                    {
                        callbackId = _insightMsg.callbackId,
                        status     = CallbackStatus.Error
                    };
                    Reply(responseToSend);
                }
            }
        }
Ejemplo n.º 19
0
        private void HandleCreateGameMsg(InsightMessage _insightMsg)
        {
            var message = (CreateGameMsg)_insightMsg.message;

            Debug.Log("[GameMasterManager] - Received player requesting game creation");

            var requestSpawnStartMsg = new RequestSpawnStartToMasterMsg {
                gameName   = message.gameName,
                minPlayers = message.minPlayers,
                maxPlayers = message.maxPlayers
            };

            server.InternalSend(requestSpawnStartMsg, _callbackMsg => {
                Debug.Log($"[GameMasterManager] - Received games create : {_callbackMsg.status}");

                Assert.AreNotEqual(CallbackStatus.Default, _callbackMsg.status);
                switch (_callbackMsg.status)
                {
                case CallbackStatus.Success: {
                    var responseReceived = (RequestSpawnStartMsg)_callbackMsg.message;

                    var playerConnId = 0;
                    if (_insightMsg is InsightNetworkMessage netMsg)
                    {
                        playerConnId = netMsg.connectionId;
                    }

                    gameLaunchers.Add(responseReceived.gameUniqueId, new GameLauncher {
                            playerConnId   = playerConnId,
                            playerUniqueId = message.uniqueId,
                            message        = new InsightNetworkMessage(new ChangeServerMsg {
                                uniqueId       = responseReceived.gameUniqueId,
                                networkAddress = responseReceived.networkAddress,
                                networkPort    = responseReceived.networkPort
                            })
                            {
                                callbackId = _insightMsg.callbackId,
                                status     = CallbackStatus.Success
                            }
                        });

                    break;
                }

                case CallbackStatus.Error: {
                    var responseToSend = new InsightNetworkMessage(new ChangeServerMsg())
                    {
                        callbackId = _insightMsg.callbackId,
                        status     = CallbackStatus.Error
                    };

                    if (_insightMsg is InsightNetworkMessage netMsg)
                    {
                        server.NetworkReply(netMsg.connectionId, responseToSend);
                    }
                    else
                    {
                        server.InternalReply(responseToSend);
                    }

                    break;
                }

                case CallbackStatus.Timeout:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            });
        }
Ejemplo n.º 20
0
 public InsightMessage(InsightMessage _insightMsg)
 {
     status  = _insightMsg.status;
     message = _insightMsg.message;
 }