Esempio n. 1
0
        public void SendChatMessageToAll(string sender, string message)
        {
            var chatObj = new ChatData()
            {
                Sender  = sender,
                Message = message
            };

            SendToAll(chatObj, PacketType.ChatData, true);
        }
Esempio n. 2
0
        public void SendChatMessageToPlayer(Client player, string sender, string message)
        {
            var chatObj = new ChatData()
            {
                Sender  = sender,
                Message = message
            };
            var data = Util.SerializeBinary(chatObj);
            var msg  = Server.CreateMessage();

            msg.Write((int)PacketType.ChatData);
            msg.Write(data.Length);
            msg.Write(data);
            player.NetConnection.SendMessage(msg, NetDeliveryMethod.ReliableOrdered, 0);
        }
Esempio n. 3
0
        public void Tick()
        {
            if (DateTime.UtcNow.Day != _lastDay)
            {
                _lastDay = DateTime.UtcNow.Day;
                if (AnnounceSelf)
                {
                    AnnounceSelfToMaster();
                }
            }

            NetIncomingMessage msg;

            while ((msg = Server.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.UnconnectedData:
                    var isPing = msg.ReadString();
                    if (isPing == "ping")
                    {
                        Console.WriteLine("INFO: ping received from " + msg.SenderEndPoint.Address.ToString());
                        var pong = Server.CreateMessage();
                        pong.Write("pong");
                        Server.SendMessage(pong, msg.SenderConnection, NetDeliveryMethod.ReliableOrdered);
                    }
                    break;

                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.WarningMessage:
                case NetIncomingMessageType.ErrorMessage:
                    Console.WriteLine(msg.ReadString());
                    break;

                case NetIncomingMessageType.ConnectionLatencyUpdated:
                    var latency = msg.ReadFloat();
                    if (KnownLatencies.ContainsKey(msg.SenderConnection.RemoteUniqueIdentifier))
                    {
                        KnownLatencies[msg.SenderConnection.RemoteUniqueIdentifier] = latency;
                    }
                    else
                    {
                        KnownLatencies.Add(msg.SenderConnection.RemoteUniqueIdentifier, latency);
                    }
                    break;

                case NetIncomingMessageType.ConnectionApproval:
                    var type    = msg.ReadInt32();
                    var leng    = msg.ReadInt32();
                    var connReq = DeserializeBinary <ConnectionRequest>(msg.ReadBytes(leng)) as ConnectionRequest;
                    if (connReq == null)
                    {
                        msg.SenderConnection.Deny("Connection Object is null");
                        Server.Recycle(msg);
                        continue;
                    }

                    if (Clients.Count < MaxPlayers)
                    {
                        if (PasswordProtected && !string.IsNullOrWhiteSpace(Password))
                        {
                            if (Password != connReq.Password)
                            {
                                msg.SenderConnection.Deny("Wrong password.");
                                Console.WriteLine("Player connection refused: wrong password.");
                                if (_gamemode != null)
                                {
                                    _gamemode.OnConnectionRefused(msg.SenderConnection, "Wrong password");
                                }
                                if (_filterscripts != null)
                                {
                                    _filterscripts.ForEach(fs => fs.OnConnectionRefused(msg.SenderConnection, "Wrong password"));
                                }
                                Server.Recycle(msg);
                                continue;
                            }
                        }

                        Clients.Add(msg.SenderConnection);
                        if (NickNames.ContainsKey(msg.SenderConnection.RemoteUniqueIdentifier))
                        {
                            NickNames[msg.SenderConnection.RemoteUniqueIdentifier] = connReq.Name;
                        }
                        else
                        {
                            NickNames.Add(msg.SenderConnection.RemoteUniqueIdentifier, connReq.Name);
                        }

                        var channelHail = Server.CreateMessage();
                        channelHail.Write(GetChannelIdForConnection(msg.SenderConnection));
                        msg.SenderConnection.Approve(channelHail);

                        var chatObj = new ChatData()
                        {
                            Sender  = "SERVER",
                            Message =
                                "Player ~h~" + NickNames[msg.SenderConnection.RemoteUniqueIdentifier] +
                                "~h~ has connected.",
                        };

                        SendToAll(chatObj, PacketType.ChatData, 0);

                        Console.WriteLine("New player connected: " + NickNames[msg.SenderConnection.RemoteUniqueIdentifier]);

                        if (_gamemode != null)
                        {
                            _gamemode.OnPlayerConnect(msg.SenderConnection);
                        }
                        if (_filterscripts != null)
                        {
                            _filterscripts.ForEach(fs => fs.OnPlayerConnect(msg.SenderConnection));
                        }
                    }
                    else
                    {
                        msg.SenderConnection.Deny("No available player slots.");
                        Console.WriteLine("Player connection refused: server full.");
                        if (_gamemode != null)
                        {
                            _gamemode.OnConnectionRefused(msg.SenderConnection, "Server is full");
                        }
                        if (_filterscripts != null)
                        {
                            _filterscripts.ForEach(fs => fs.OnConnectionRefused(msg.SenderConnection, "Server is full"));
                        }
                    }
                    break;

                case NetIncomingMessageType.StatusChanged:
                    var newStatus = (NetConnectionStatus)msg.ReadByte();
                    if (newStatus == NetConnectionStatus.Disconnected && Clients.Contains(msg.SenderConnection))
                    {
                        var name = "";
                        if (NickNames.ContainsKey(msg.SenderConnection.RemoteUniqueIdentifier))
                        {
                            name = NickNames[msg.SenderConnection.RemoteUniqueIdentifier];
                        }

                        var chatObj = new ChatData()
                        {
                            Sender  = "SERVER",
                            Message =
                                "Player ~h~" + name +
                                "~h~ has disconnected.",
                        };

                        SendToAll(chatObj, PacketType.ChatData, 0);

                        var dcObj = new PlayerDisconnect()
                        {
                            Id = msg.SenderConnection.RemoteUniqueIdentifier,
                        };

                        SendToAll(dcObj, PacketType.PlayerDisconnect, 0);

                        Console.WriteLine("Player disconnected: " + name);

                        if (_gamemode != null)
                        {
                            _gamemode.OnPlayerDisconnect(msg.SenderConnection);
                        }
                        if (_filterscripts != null)
                        {
                            _filterscripts.ForEach(fs => fs.OnPlayerDisconnect(msg.SenderConnection));
                        }

                        Clients.Remove(msg.SenderConnection);
                        NickNames.Remove(msg.SenderConnection.RemoteUniqueIdentifier);
                    }
                    break;

                case NetIncomingMessageType.DiscoveryRequest:
                    NetOutgoingMessage response = Server.CreateMessage();
                    var obj = new DiscoveryResponse();
                    obj.ServerName        = Name;
                    obj.MaxPlayers        = MaxPlayers;
                    obj.PasswordProtected = PasswordProtected;
                    obj.PlayerCount       = Clients.Count;
                    obj.Port = Port;

                    var bin = SerializeBinary(obj);

                    response.Write((int)PacketType.DiscoveryResponse);
                    response.Write(bin.Length);
                    response.Write(bin);

                    Server.SendDiscoveryResponse(response, msg.SenderEndPoint);
                    break;

                case NetIncomingMessageType.Data:
                    var packetType = (PacketType)msg.ReadInt32();

                    switch (packetType)
                    {
                    case PacketType.ChatData:
                    {
                        try
                        {
                            var len  = msg.ReadInt32();
                            var data = DeserializeBinary <ChatData>(msg.ReadBytes(len)) as ChatData;
                            if (data != null)
                            {
                                var pass = true;
                                if (_gamemode != null)
                                {
                                    pass = _gamemode.OnChatMessage(msg.SenderConnection, data.Message);
                                }

                                if (_filterscripts != null)
                                {
                                    _filterscripts.ForEach(fs => pass = pass && fs.OnChatMessage(msg.SenderConnection, data.Message));
                                }

                                if (pass)
                                {
                                    data.Id     = msg.SenderConnection.RemoteUniqueIdentifier;
                                    data.Sender = NickNames[msg.SenderConnection.RemoteUniqueIdentifier];
                                    SendToAll(data, PacketType.ChatData, 0);
                                    Console.WriteLine(data.Sender + ": " + data.Message);
                                }
                            }
                        }
                        catch (IndexOutOfRangeException)
                        { }
                    }
                    break;

                    case PacketType.VehiclePositionData:
                    {
                        try
                        {
                            var name = "";
                            if (NickNames.ContainsKey(msg.SenderConnection.RemoteUniqueIdentifier))
                            {
                                name = NickNames[msg.SenderConnection.RemoteUniqueIdentifier];
                            }

                            var len  = msg.ReadInt32();
                            var data =
                                DeserializeBinary <VehicleData>(msg.ReadBytes(len)) as
                                VehicleData;
                            if (data != null)
                            {
                                data.Id   = msg.SenderConnection.RemoteUniqueIdentifier;
                                data.Name = name;
                                if (KnownLatencies.ContainsKey(msg.SenderConnection.RemoteUniqueIdentifier))
                                {
                                    data.Latency =
                                        KnownLatencies[msg.SenderConnection.RemoteUniqueIdentifier];
                                }

                                SendToAll(data, PacketType.VehiclePositionData, GetChannelIdForConnection(msg.SenderConnection), msg.SenderConnection.RemoteUniqueIdentifier);
                            }
                        }
                        catch (IndexOutOfRangeException)
                        { }
                    }
                    break;

                    case PacketType.PedPositionData:
                    {
                        try
                        {
                            var name = "";
                            if (NickNames.ContainsKey(msg.SenderConnection.RemoteUniqueIdentifier))
                            {
                                name = NickNames[msg.SenderConnection.RemoteUniqueIdentifier];
                            }
                            var len  = msg.ReadInt32();
                            var data =
                                DeserializeBinary <PedData>(msg.ReadBytes(len)) as PedData;
                            if (data != null)
                            {
                                data.Id   = msg.SenderConnection.RemoteUniqueIdentifier;
                                data.Name = name;
                                if (KnownLatencies.ContainsKey(msg.SenderConnection.RemoteUniqueIdentifier))
                                {
                                    data.Latency =
                                        KnownLatencies[msg.SenderConnection.RemoteUniqueIdentifier];
                                }
                                SendToAll(data, PacketType.PedPositionData, GetChannelIdForConnection(msg.SenderConnection), msg.SenderConnection.RemoteUniqueIdentifier);
                            }
                        }
                        catch (IndexOutOfRangeException)
                        { }
                    }
                    break;

                    case PacketType.NpcVehPositionData:
                    {
                        try
                        {
                            var len  = msg.ReadInt32();
                            var data =
                                DeserializeBinary <VehicleData>(msg.ReadBytes(len)) as
                                VehicleData;
                            if (data != null)
                            {
                                data.Id = msg.SenderConnection.RemoteUniqueIdentifier;
                                SendToAll(data, PacketType.NpcVehPositionData, GetChannelIdForConnection(msg.SenderConnection), msg.SenderConnection.RemoteUniqueIdentifier);
                            }
                        }
                        catch (IndexOutOfRangeException)
                        { }
                    }
                    break;

                    case PacketType.NpcPedPositionData:
                    {
                        try
                        {
                            var len  = msg.ReadInt32();
                            var data =
                                DeserializeBinary <PedData>(msg.ReadBytes(len)) as PedData;
                            if (data != null)
                            {
                                data.Id = msg.SenderConnection.RemoteUniqueIdentifier;
                                SendToAll(data, PacketType.NpcPedPositionData, GetChannelIdForConnection(msg.SenderConnection), msg.SenderConnection.RemoteUniqueIdentifier);
                            }
                        }
                        catch (IndexOutOfRangeException)
                        { }
                    }
                    break;

                    case PacketType.WorldSharingStop:
                    {
                        var dcObj = new PlayerDisconnect()
                        {
                            Id = msg.SenderConnection.RemoteUniqueIdentifier,
                        };
                        SendToAll(dcObj, PacketType.WorldSharingStop, 0);
                    }
                    break;

                    case PacketType.NativeResponse:
                    {
                        var len  = msg.ReadInt32();
                        var data = DeserializeBinary <NativeResponse>(msg.ReadBytes(len)) as NativeResponse;
                        if (data == null || !_callbacks.ContainsKey(data.Id))
                        {
                            continue;
                        }
                        object resp = null;
                        if (data.Response is IntArgument)
                        {
                            resp = ((IntArgument)data.Response).Data;
                        }
                        else if (data.Response is UIntArgument)
                        {
                            resp = ((UIntArgument)data.Response).Data;
                        }
                        else if (data.Response is StringArgument)
                        {
                            resp = ((StringArgument)data.Response).Data;
                        }
                        else if (data.Response is FloatArgument)
                        {
                            resp = ((FloatArgument)data.Response).Data;
                        }
                        else if (data.Response is BooleanArgument)
                        {
                            resp = ((BooleanArgument)data.Response).Data;
                        }
                        else if (data.Response is Vector3Argument)
                        {
                            var tmp = (Vector3Argument)data.Response;
                            resp = new Vector3()
                            {
                                X = tmp.X,
                                Y = tmp.Y,
                                Z = tmp.Z,
                            };
                        }
                        if (_callbacks.ContainsKey(data.Id))
                        {
                            _callbacks[data.Id].Invoke(resp);
                        }
                        _callbacks.Remove(data.Id);
                    }
                    break;

                    case PacketType.PlayerKilled:
                    {
                        if (_gamemode != null)
                        {
                            _gamemode.OnPlayerKilled(msg.SenderConnection);
                        }
                        if (_filterscripts != null)
                        {
                            _filterscripts.ForEach(fs => fs.OnPlayerKilled(msg.SenderConnection));
                        }
                    }
                    break;
                    }
                    break;

                default:
                    Console.WriteLine("WARN: Unhandled type: " + msg.MessageType);
                    break;
                }
                Server.Recycle(msg);
            }
            if (_gamemode != null)
            {
                _gamemode.OnTick();
            }
            if (_filterscripts != null)
            {
                _filterscripts.ForEach(fs => fs.OnTick());
            }
        }