Пример #1
0
        private void ReadMessages(NetServer server)
        {
            NetIncomingMessage msg;

            while ((msg = server.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.Data:
                    HandleIncomingMessage(MessageHandler.Decode(msg), msg);
                    break;

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

                case NetIncomingMessageType.StatusChanged:
                    var status = (NetConnectionStatus)msg.ReadByte();
                    switch (status)
                    {
                    case NetConnectionStatus.Connected:
                        Console.WriteLine("Connection Established " + msg.SenderConnection.RemoteUniqueIdentifier);
                        break;

                    case NetConnectionStatus.Disconnected:
                        Console.WriteLine("User Disconnected" + msg.SenderConnection.RemoteUniqueIdentifier);
                        _room.RemovePlayer(msg.SenderConnection);
                        break;

                    default:
                        Console.WriteLine("Connection Status: " + status.ToString());
                        break;
                    }
                    break;

                case NetIncomingMessageType.ConnectionApproval:
                    ConnectionApprovalMessage approval = MessageHandler.Decode(msg) as ConnectionApprovalMessage;
                    Console.WriteLine(approval.Name);
                    if (IsApproved(approval))
                    {
                        msg.SenderConnection.Approve();
                        _room.AddPlayer(msg.SenderConnection, approval);
                    }
                    else
                    {
                        msg.SenderConnection.Deny();
                    }
                    break;

                default:
                    Console.WriteLine("Unhandled type: " + msg.MessageType);
                    break;
                }

                server.Recycle(msg);
            }
        }
Пример #2
0
        internal void update()
        {
            NetIncomingMessage incomingMessage;

            while ((incomingMessage = server.ReadMessage()) != null)
            {
                switch (incomingMessage.MessageType)
                {
                case NetIncomingMessageType.StatusChanged:
                    statusChanged(incomingMessage);
                    break;

                case NetIncomingMessageType.Data:
                    data(incomingMessage);
                    break;

                case NetIncomingMessageType.Receipt:
                    break;

                case NetIncomingMessageType.DiscoveryRequest:
                    discoveryRequest(incomingMessage);
                    break;

                case NetIncomingMessageType.DiscoveryResponse:
                    break;

                case NetIncomingMessageType.DebugMessage:
                    break;

                case NetIncomingMessageType.WarningMessage:
                    break;

                case NetIncomingMessageType.ErrorMessage:
                    break;
                }

                server.Recycle(incomingMessage);
            }
        }
Пример #3
0
        public void Start()
        {
            NetPeerConfiguration config = new NetPeerConfiguration("MulTUNG");

            config.Port         = (int)Configuration.Get <long>("ServerPort", 5678);
            config.LocalAddress = IPAddress.Parse(Configuration.Get("LocalServerIP", "127.0.0.1"));
            config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);

            Server = new NetServer(config);
            Server.Start();

            Network.IsServer = true;

            ThreadPool.QueueUserWorkItem(_ =>
            {
                NetIncomingMessage msg;
                while (Server != null)
                {
                    msg = Server.WaitMessage(int.MaxValue);

                    if (msg == null)
                    {
                        continue;
                    }

                    HandleMessage(msg);

                    Server.Recycle(msg);
                }
            });

            World.AddNetObjects();
            World.LoadCircuitState();

            Network.StartPositionUpdateThread(Constants.PositionUpdateInterval);

            Log.WriteLine("Listening on port " + config.Port);
        }
Пример #4
0
        public void ReadMessages()
        {
            NetIncomingMessage message;
            var stop = false;

            while (!stop)
            {
                message = server.ReadMessage();
                Console.Write("");
                if (message != null)
                {
                    switch (message.MessageType)
                    {
                    case NetIncomingMessageType.Data:
                    {
                        Console.WriteLine(NetUtility.ToHexString(message.SenderConnection.RemoteUniqueIdentifier) + " said: ");
                        var data = message.ReadString();
                        Console.WriteLine(data);

                        List <NetConnection> all = server.Connections;
                        all.Remove(message.SenderConnection);

                        if (all.Count > 0)
                        {
                            NetOutgoingMessage om = server.CreateMessage();
                            om.Write(NetUtility.ToHexString(message.SenderConnection.RemoteUniqueIdentifier) + " said: " + data);
                            server.SendMessage(om, all, NetDeliveryMethod.ReliableOrdered, 0);
                        }

                        if (data == "exit")
                        {
                            stop = true;
                        }

                        break;
                    }

                    case NetIncomingMessageType.DebugMessage:
                        Console.WriteLine(message.ReadString());
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        Console.WriteLine(message.SenderConnection.Status);
                        if (message.SenderConnection.Status == NetConnectionStatus.Connected)
                        {
                            clients.Add(message.SenderConnection.Peer);
                            Console.WriteLine("{0} has connected.", message.SenderConnection.Peer.Configuration.LocalAddress);
                        }
                        if (message.SenderConnection.Status == NetConnectionStatus.Disconnected)
                        {
                            clients.Remove(message.SenderConnection.Peer);
                            Console.WriteLine("{0} has disconnected.", message.SenderConnection.Peer.Configuration.LocalAddress);
                        }
                        break;

                    default:
                        Console.WriteLine("Unhandled message type: {message.MessageType}");
                        break;
                    }
                    server.Recycle(message);
                }
            }

            Console.WriteLine("Shutdown package \"exit\" received. Press any key to finish shutdown");
            Console.ReadKey();
        }
Пример #5
0
        public void ReadMessages()
        {
            NetIncomingMessage message;
            var stop = false;

            while (!stop)
            {
                while ((message = server.ReadMessage()) != null)
                {
                    switch (message.MessageType)
                    {
                    case NetIncomingMessageType.Data:
                    {
                        Console.WriteLine("I got smth!");
                        var data = message.ReadString();
                        Console.WriteLine(data);

                        if (data == "exit")
                        {
                            stop = true;
                        }

                        break;
                    }

                    case NetIncomingMessageType.DebugMessage:
                        Console.WriteLine(message.ReadString());
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        Console.WriteLine(message.SenderConnection.Status);
                        if (message.SenderConnection.Status == NetConnectionStatus.Connected)
                        {
                            clients.Add(message.SenderConnection.Peer);
                            Console.WriteLine("{0} has connected.", message.SenderConnection.Peer.Configuration.LocalAddress);
                        }
                        if (message.SenderConnection.Status == NetConnectionStatus.Disconnected)
                        {
                            clients.Remove(message.SenderConnection.Peer);
                            Console.WriteLine("{0} has disconnected.", message.SenderConnection.Peer.Configuration.LocalAddress);
                        }
                        break;

                    case NetIncomingMessageType.UnconnectedData:
                        Console.WriteLine("UnconnectedData Modify");
                        break;

                    case NetIncomingMessageType.DiscoveryResponse:
                        Console.WriteLine("DiscoveryResponse Modify");
                        break;

                    case NetIncomingMessageType.DiscoveryRequest:
                        Console.WriteLine("DiscoveryRequest Modify");
                        break;

                    default:
                        Console.WriteLine("Unhandled message type: {message.MessageType}");
                        break;
                    }
                    server.Recycle(message);
                }
            }

            Console.WriteLine("Shutdown package \"exit\" received. Press any key to finish shutdown");
            Console.ReadKey();
        }