コード例 #1
0
        // ===========================================================================
        // ========================= DOD CALLBACK FUNCTIONS =========================
        // ===========================================================================

        void cbClientHandler(NetworkMessage netMsg)
        {
            switch (netMsg.msgType)
            {
            case (short)Message.ID.UserLogin:
            {
                Message.UserLogin msg = netMsg.ReadMessage <Message.UserLogin>();
                myID = msg.playerID;
                ClientSendMessage(Message.ID.UserLogin,
                                  new Message.UserLogin(myID, networkingInfo.playerName),
                                  Channels.reliable);
            }
            break;

            case (short)Message.ID.KickReason:
            {
                Message.KickReason msg = netMsg.ReadMessage <Message.KickReason>();
                common.printConsole(tag, "Kicked from server. Reason: " + msg.reason, true);
            }
            break;

            case (short)Message.ID.ConsoleBroadcast:
            {
                Message.ConsoleBroadcast msg = netMsg.ReadMessage <Message.ConsoleBroadcast>();
                Player p;
                if (common.existPlayerByID(connectedPlayers, msg.playerID, out p))
                {
                    common.printConsole(NetworkingCommon.CHATTAG, p.name + ": " + msg.broadcast, true);
                }
                else if (msg.playerID == NetworkingCommon.SERVERID)
                {
                    common.printConsole(NetworkingCommon.CHATTAG, "SERVER" + " ~ " + msg.broadcast, true);
                }
            }
            break;

            case (short)Message.ID.NameChange:
            {
                Message.NameChange msg = netMsg.ReadMessage <Message.NameChange>();
                Player             p;
                if (common.existPlayerByID(connectedPlayers, msg.playerID, out p))
                {
                    if (msg.playerID == myID)                             // Its me, check if it failed or not
                    {
                        if (msg.failed)                                   // Was not successful
                        {
                            common.printConsole(NetworkingCommon.NOTAG, "Name change failed. " + msg.newName + " is already occupied.", true);
                        }
                        else
                        {
                            common.printConsole(NetworkingCommon.NOTAG, "Your new name is " + msg.newName, true);
                            p.name = msg.newName;
                        }
                    }
                    else                             // Someone else changed their name
                    {
                        if (!msg.failed)
                        {
                            common.printConsole(NetworkingCommon.NOTAG, printPlayerName(p) + " is now known as " + msg.newName, true);
                            p.name = msg.newName;
                        }
                    }
                }
            }
            break;

            case (short)Message.ID.PlayerCon:
            {
                Message.PlayerCon msg = netMsg.ReadMessage <Message.PlayerCon>();
                Player            p;
                connectedPlayers.Add(p = new Player(msg.playerID, msg.name));
                if (msg.playerID != myID)
                {
                    common.printConsole(NetworkingCommon.NOTAG, "Player connected: " + printPlayerName(p), true);
                }
                else
                {
                    common.printConsole(NetworkingCommon.NOTAG, "Connected to " + printServerAdress() + " with ID: " + myID, true);
                }
            }
            break;

            case (short)Message.ID.PlayerDisc:
            {
                Message.PlayerDisc msg = netMsg.ReadMessage <Message.PlayerDisc>();
                Player             p;
                if (common.existPlayerByID(connectedPlayers, msg.playerID, out p))
                {
                    common.printConsole(NetworkingCommon.NOTAG, printPlayerName(p) + " disconnected. Reason: " + msg.reason, true);
                    connectedPlayers.Remove(p);
                }
            }
            break;

            case (short)Message.ID.PlayerList:
            {
                Message.PlayerList msg = netMsg.ReadMessage <Message.PlayerList>();
                connectedPlayers = msg.GetArrayAsList();
            }
            break;

            case (short)Message.ID.HeartBeat:
            {
                // Do nothing, since this is not interresting for us
            }
            break;

            default:
                common.printConsole(tag, "Unknown message id received: " + netMsg.msgType, true);
                break;
            }
        }
コード例 #2
0
ファイル: Server.cs プロジェクト: Johannesolof/DoDGame
        // ===========================================================================
        // ========================= CALLBACK FUNCTIONS =========================
        // ===========================================================================
        // SERVER SIDE DOD MESSAGE CALLBACK
        void cbServerHandler(NetworkMessage netMsg)
        {
            if ( !whiteList.ContainsKey(netMsg.conn.address) )
            {
                // User is not yet white listed, or should not be. Just kick and forget about him
                common.printConsole (tag, "Kicked chatty not white-listed peer: " + netMsg.conn.address + "(" + netMsg.msgType + ")", true);
                netMsg.ReadMessage<Message.NameChange>();
                netMsg.conn.Disconnect();
                return;
            }

            switch(netMsg.msgType)
            {
            case (short)Message.ID.UserLogin:  // Last part of user connecting to the server. Check if name is free
                {
                    Message.UserLogin msg = netMsg.ReadMessage<Message.UserLogin>();
                    if ( common.existPlayerByName(connectedPlayers, msg.name) )
                    {
                        ServerSendMessage(Message.ID.KickReason,
                            new Message.KickReason("Name already taken!"),
                            Channels.reliable, netMsg.conn);
                        netMsg.conn.Disconnect();
                    }
                    else // New user connected!
                    {
                        ServerPlayer sp = new ServerPlayer(msg.playerID, msg.name, netMsg.conn);
                        connectedPlayers.Add(sp);

                        common.printConsole (tag, "Player connected: " + printPlayerName(sp) + " from " + netMsg.conn.address, true);

                        ServerSendMessage(Message.ID.PlayerCon,
                            new Message.PlayerCon(sp), Channels.reliable, connectedPlayers); // Inform everyone of the new player

                        ServerSendMessage(Message.ID.PlayerList,
                            new Message.PlayerList(connectedPlayers),
                            Channels.reliable, netMsg.conn); // Let the new player know the current state of the server
                    }
                }
                break;
            case (short)Message.ID.KickReason:
                {
                    Message.KickReason msg = netMsg.ReadMessage<Message.KickReason>();
                    common.printConsole (tag, "Tried to kick server. Reason: " + msg.reason, true);
                }
                break;

            case (short)Message.ID.ConsoleBroadcast:
                {
                    Message.ConsoleBroadcast msg = netMsg.ReadMessage<Message.ConsoleBroadcast>();
                    ServerSendMessage(Message.ID.ConsoleBroadcast, msg, Channels.reliable, connectedPlayers);
                }
                break;

            case (short)Message.ID.NameChange:
                {
                    Message.NameChange msg = netMsg.ReadMessage<Message.NameChange>();
                    ServerPlayer sp;
                    if ( common.existPlayerByID(connectedPlayers, msg.playerID, out sp) )
                    {
                        if ( common.existPlayerByName(connectedPlayers, msg.newName ) ) // Name is already occupied
                        {
                            ServerSendMessage(Message.ID.NameChange, new Message.NameChange(sp.id, msg.newName, true),
                                Channels.reliable, sp.connection);
                            common.printConsole (tag, printPlayerName(sp) + "'s name change failed, to: " + msg.newName,
                                true);
                        }
                        else // Acknowledge the name change
                        {
                            ServerSendMessage(Message.ID.NameChange, msg, Channels.reliable, connectedPlayers);
                            common.printConsole (tag, printPlayerName(sp) + " is now known as " + msg.newName, true);
                        }

                        sp.name = msg.newName;
                    }
                }
                break;

            case (short)Message.ID.PlayerCon:
                {
                    common.printConsole (tag, "PlayerCon received: " + netMsg.msgType, true);
                }
                break;

            case (short)Message.ID.PlayerDisc:
                {
                    common.printConsole (tag, "PlayerDisc received: " + netMsg.msgType, true);
                }
                break;

            case (short)Message.ID.PlayerList:
                {
                    // TODO: See this as a request for the player list ?
                    common.printConsole (tag, "PlayerList received: " + netMsg.msgType, true);
                }
                break;

            case (short)Message.ID.HeartBeat:
                {
                    // Do nothing, since this is not interresting for us
                }
                break;

            default:
                common.printConsole (tag, "Unknown message id received: " + netMsg.msgType, true);
                break;
            }
        }