예제 #1
0
파일: Server.cs 프로젝트: Phlarfl/TSML
        public void Update()
        {
            if (server != null)
            {
                NetIncomingMessage incoming;
                while ((incoming = server.ReadMessage()) != null)
                {
                    switch (incoming.MessageType)
                    {
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.ErrorMessage:
                    {
                        var message = incoming.ReadString();
                        Print(message);
                    }
                    break;

                    case NetIncomingMessageType.StatusChanged:
                    {
                        var status = (NetConnectionStatus)incoming.ReadByte();
                        var reason = incoming.ReadString();

                        switch (status)
                        {
                        case NetConnectionStatus.Connected:
                        {
                            var hailMessage = incoming.SenderConnection.RemoteHailMessage;
                            var name        = hailMessage.ReadString();
                            Print($"{name} joined the game");

                            if (BootMaster.instance.worldMaster && BootMaster.instance.worldMaster.state == WorldMaster.State.Done)
                            {
                                var packetWorld = new PacketWorld(server)
                                {
                                    WorldData = BootMaster.instance.worldMaster.GetSaveString()
                                };
                                var outgoing = packetWorld.Serialize();
                                incoming.SenderConnection.SendMessage(outgoing, NetDeliveryMethod.ReliableOrdered, 0);
                            }
                        }
                        break;

                        case NetConnectionStatus.Disconnected:
                        {
                            Print($"{NetUtility.ToHexString(incoming.SenderConnection.RemoteUniqueIdentifier)} {status}: {reason}");
                        }
                        break;
                        }
                    }
                    break;

                    case NetIncomingMessageType.Data:
                    {
                        var packetType = (PacketType)incoming.ReadByte();
                        switch (packetType)
                        {
                        case PacketType.WORLD_DATA:
                        {
                            var packet = new PacketWorld(server);
                            packet.Deserialize(incoming);
                        }
                        break;

                        case PacketType.ADD_VOXEL:
                        {
                            var packet = new PacketAddVoxel(server);
                            packet.Deserialize(incoming);

                            var others = server.Connections;
                            others.Remove(incoming.SenderConnection);

                            if (others.Count > 0)
                            {
                                server.SendMessage(packet.Serialize(), others, NetDeliveryMethod.ReliableOrdered, 0);
                            }
                        }
                        break;

                        case PacketType.REMOVE_VOXEL:
                        {
                            var packet = new PacketRemoveVoxel(server);
                            packet.Deserialize(incoming);

                            var others = server.Connections;
                            others.Remove(incoming.SenderConnection);

                            if (others.Count > 0)
                            {
                                server.SendMessage(packet.Serialize(), others, NetDeliveryMethod.ReliableOrdered, 0);
                            }
                        }
                        break;
                        }
                    }
                    break;
                    }
                    server.Recycle(incoming);
                }
            }
        }
예제 #2
0
파일: Client.cs 프로젝트: Phlarfl/TSML
        public void Update(object peer)
        {
            NetIncomingMessage incoming;

            while ((incoming = client.ReadMessage()) != null)
            {
                switch (incoming.MessageType)
                {
                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.WarningMessage:
                case NetIncomingMessageType.ErrorMessage:
                {
                    var message = incoming.ReadString();
                    Print(message);
                }
                break;

                case NetIncomingMessageType.StatusChanged:
                {
                    var status = (NetConnectionStatus)incoming.ReadByte();
                    var reason = incoming.ReadString();

                    switch (status)
                    {
                    case NetConnectionStatus.Connected:
                    {
                        Print($"{status}: {reason}");
                    }
                    break;

                    case NetConnectionStatus.Disconnected:
                    {
                        Print($"{status}: {reason}");
                        Shutdown(reason);
                    }
                    break;
                    }
                }
                break;

                case NetIncomingMessageType.Data:
                {
                    var packetType = (PacketType)incoming.ReadByte();
                    switch (packetType)
                    {
                    case PacketType.WORLD_DATA:
                    {
                        var packet = new PacketWorld(client);
                        packet.Deserialize(incoming);

                        if (BootMaster.instance.worldMaster)
                        {
                            BootMaster.instance.worldMaster.SaveCurrentAndLoadAsNew(packet.WorldData);
                        }
                    }
                    break;

                    case PacketType.ADD_VOXEL:
                    {
                        var packet = new PacketAddVoxel(client);
                        packet.Deserialize(incoming);

                        var master        = BootMaster.instance.worldMaster;
                        var maker         = master.maker;
                        var groundClicker = master.groundClicker;

                        if (packet.ValidAdd)
                        {
                            var voxel = AddClick(master, maker, packet);
                            groundClicker.lastAddedVoxel = voxel;
                            master.clickEffect.Click(true, packet.PlanePos, packet.DestinationHeight, voxel.type);
                        }
                    }
                    break;

                    case PacketType.REMOVE_VOXEL:
                    {
                        var packet = new PacketRemoveVoxel(client);
                        packet.Deserialize(incoming);

                        var master = BootMaster.instance.worldMaster;
                        var maker  = master.maker;

                        if (packet.ValidRemove)
                        {
                            var voxel = RemoveClick(master, maker, packet);
                            master.clickEffect.Click(false, packet.PlanePos, packet.DestinationHeight, voxel.type);
                        }
                    }
                    break;
                    }
                }
                break;
                }
                client.Recycle(incoming);
            }
        }