Exemple #1
0
        public PacketLogEntry(SerializationInfo info, StreamingContext context)
        {
            this.UnityTime = info.GetSingle("UnityTime");
            this.In        = info.GetBoolean("In");
            this.Client    = info.GetBoolean("Client");

            byte[] packetData = info.GetValue("Packet", typeof(byte[])) as byte[];
            this.Packet = PacketDeserializer.DeserializePacket(new PacketReader(packetData));
        }
Exemple #2
0
        private void HandleMessage(NetIncomingMessage msg)
        {
            switch (msg.MessageType)
            {
            case NetIncomingMessageType.ConnectionApproval:
                string  verStr = msg.ReadString();
                Version ver    = new Version(verStr);

                if (ver == MulTUNG.MulTUNG.Version)
                {
                    msg.SenderConnection.Approve();

                    string username = msg.ReadString().Trim();

                    if (!Players.Any(o => o.Value.Username.Equals(username)))
                    {
                        IGConsole.Log($"{username.Length} {MaxUsernameLength}");
                        if (username.Length < MaxUsernameLength)
                        {
                            msg.SenderConnection.Approve();
                        }
                        else
                        {
                            msg.SenderConnection.Deny($"your username must be shorter than {MaxUsernameLength} characters.");
                        }
                    }
                    else
                    {
                        msg.SenderConnection.Deny("someone is already using that username.");
                    }
                }
                else
                {
                    msg.SenderConnection.Deny($"wrong MulTUNG version, server has v{MulTUNG.MulTUNG.Version}.");
                }

                break;

            case NetIncomingMessageType.Data:
                var packet = PacketDeserializer.DeserializePacket(new MessagePacketReader(msg));

                if (packet.SenderID == Network.ServerPlayerID)
                {
                    break;
                }

                PacketLog.LogReceive(packet);

                if (packet.ShouldBroadcast)
                {
                    Broadcast(packet, packet.ReliableBroadcast ? NetDeliveryMethod.ReliableOrdered : NetDeliveryMethod.UnreliableSequenced);
                }
                else
                {
                    Network.ProcessPacket(packet, Network.ServerPlayerID);
                }

                break;

            case NetIncomingMessageType.StatusChanged:
                var status = (NetConnectionStatus)msg.ReadByte();

                if (status == NetConnectionStatus.Connected)
                {
                    int id = PlayerIdCounter++;

                    msg.SenderConnection.SendMessage(new PlayerWelcomePacket
                    {
                        YourID         = id,
                        ServerUsername = Network.Username,
                        Players        = Players.Select(o => new Tuple <int, string>(o.Key, o.Value.Username)).ToList()
                    }.GetMessage(Server), NetDeliveryMethod.ReliableOrdered, 0);

                    var player = new Player(id, msg.SenderConnection);

                    Log.WriteLine("Connected player " + player.ID);

                    Players.Add(id, player);
                }
                else if (status == NetConnectionStatus.Disconnected)
                {
                    var player = Players.SingleOrDefault(o => o.Value.Connection == msg.SenderConnection);

                    Players.Remove(player.Key);
                    PlayerManager.WaveGoodbye(player.Key);
                }

                break;
            }
        }
Exemple #3
0
        public void Connect(IPEndPoint endPoint)
        {
            DisconnectReason = null;

            NetPeerConfiguration config = new NetPeerConfiguration("MulTUNG");

            Client = new NetClient(config);
            Client.Start();

            var approval = Client.CreateMessage();

            approval.Write(MulTUNG.Version.ToString());
            approval.Write(Username);

            var conn = Client.Connect(endPoint, approval);

            ThreadPool.QueueUserWorkItem(o =>
            {
                var c       = o as NetConnection;
                int elapsed = 0;

                while (true)
                {
                    Thread.Sleep(50);
                    elapsed += 50;

                    if ((c.Status != NetConnectionStatus.Connected && elapsed >= Constants.WaitForConnection) || DisconnectReason != null)
                    {
                        Network.IsClient = false;

                        string status = MulTUNG.Status = "Couldn't connect to remote server." + (DisconnectReason != null ? " Check the console for more details." : "");

                        IGConsole.Error("Couldn't connect to remote server: " + DisconnectReason);

                        Thread.Sleep(3000);

                        MulTUNG.ShowMainMenuCanvases();
                        MulTUNG.ShowStatusWindow = false;
                        MulTUNG.Status           = "";

                        break;
                    }
                    else if (c.Status == NetConnectionStatus.Connected)
                    {
                        Network.IsClient = true;

                        MulTUNG.SynchronizationContext.Send(_ =>
                        {
                            SaveManager.SaveName = MulTUNG.ForbiddenSaveName;
                            World.DeleteSave();

                            SceneManager.LoadScene("gameplay");
                            EverythingHider.HideEverything();
                        }, null);

                        while (ModUtilities.IsOnMainMenu)
                        {
                            Thread.Sleep(500);
                        }

                        Thread.Sleep(1000);

                        IsInGameplay = true;
                        EnterEvent.Set();

                        InitWorld();

                        break;
                    }
                }
            }, conn);

            ThreadPool.QueueUserWorkItem(_ =>
            {
                NetIncomingMessage msg;

                while (Client.Status == NetPeerStatus.Running)
                {
                    msg = Client.WaitMessage(int.MaxValue);

                    if (msg == null)
                    {
                        continue;
                    }

                    switch (msg.MessageType)
                    {
                    case NetIncomingMessageType.Data:
                        var packet = PacketDeserializer.DeserializePacket(new MessagePacketReader(msg));

                        if (Network.ProcessPacket(packet, this.PlayerID))
                        {
                            PacketLog.LogReceive(packet);
                        }

                        break;

                    case NetIncomingMessageType.StatusChanged:
                        var status = (NetConnectionStatus)msg.ReadByte();
                        Log.WriteLine("Status: " + status);

                        if (status == NetConnectionStatus.Disconnected)
                        {
                            string reason = msg.ReadString();

                            if (!string.IsNullOrEmpty(reason))
                            {
                                DisconnectReason = reason;
                            }

                            Disconnect();
                        }

                        LastStatus = Client.ConnectionStatus;
                        break;
                    }

                    Client.Recycle(msg);
                }
            });
        }