Пример #1
0
		public bool existPlayerByConnection (List<ServerPlayer> L, NetworkConnection query)
		{
			ServerPlayer sp = new ServerPlayer();

			return existPlayerByConnection(L, query, out sp);
		}
Пример #2
0
		public bool existPlayerByName (List<ServerPlayer> L, string query)
		{
			ServerPlayer sp = new ServerPlayer();

			return existPlayerByName(L, query, out sp);
		}
Пример #3
0
		public bool existPlayerByConnection (List<ServerPlayer> L, NetworkConnection query, out ServerPlayer player)
		{
			Predicate<ServerPlayer> pr = sp => sp.connection == query;

			int i = L.FindIndex(pr);
			if (i == -1) {
				player = null;
				return false;
			}
			player = L.Find(pr);
			return true;
		}
Пример #4
0
		public bool existPlayerByID (List<ServerPlayer> L, byte query)
		{
			ServerPlayer sp = new ServerPlayer();

			return existPlayerByID(L, query, out sp);
		}
Пример #5
0
		public bool existPlayerByName (List<ServerPlayer> L, string query, out ServerPlayer player)
		{
			Predicate<ServerPlayer> pr = sp => sp.name == query;

			int i = L.FindIndex(pr);
			if (i == -1) {
				player = null;
				return false;
			}
			player = L.Find(pr);
			return true;
		}
Пример #6
0
        public bool existPlayerByConnection(List <ServerPlayer> L, NetworkConnection query)
        {
            ServerPlayer sp = new ServerPlayer();

            return(existPlayerByConnection(L, query, out sp));
        }
Пример #7
0
        // ===========================================================================
        // ========================= 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;
            }
        }
Пример #8
0
        public bool existPlayerByConnection(List <ServerPlayer> L, NetworkConnection query, out ServerPlayer player)
        {
            Predicate <ServerPlayer> pr = sp => sp.connection == query;

            int i = L.FindIndex(pr);

            if (i == -1)
            {
                player = null;
                return(false);
            }
            player = L.Find(pr);
            return(true);
        }
Пример #9
0
        public bool existPlayerByName(List <ServerPlayer> L, string query)
        {
            ServerPlayer sp = new ServerPlayer();

            return(existPlayerByName(L, query, out sp));
        }
Пример #10
0
        public bool existPlayerByID(List <ServerPlayer> L, byte query)
        {
            ServerPlayer sp = new ServerPlayer();

            return(existPlayerByID(L, query, out sp));
        }
Пример #11
0
		// ===========================================================================
		// ========================= 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;
			}
		}
Пример #12
0
		void ServerSendMessage(Message.ID id, MessageBase msg, byte channel, ServerPlayer sp)
		{
			if(sp.connection.isConnected)
				sp.connection.SendByChannel((short)id, msg, channel);
			else
				common.printConsole (tag, "Player is not connected on client " + sp.connection.address, true);
		}