Ejemplo n.º 1
0
        /// <summary>
        /// Update the network context; ie, invoke handlers for received network messages if we're a
        /// client or broadcast out received messages if we're a server.
        /// </summary>
        public void Update()
        {
            NetPeer peer = Peer;

            NetIncomingMessage msg;

            while ((msg = peer.ReadMessage()) != null)
            {
                Log <NetworkContext> .Info("NetworkContext.Update: got message type " + msg.MessageType);

                switch (msg.MessageType)
                {
                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.WarningMessage:
                case NetIncomingMessageType.ErrorMessage:
                    Log <NetworkContext> .Warn(msg.ReadString());

                    break;

                case NetIncomingMessageType.StatusChanged:
                    NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                    Log <NetworkContext> .Info("\\" + status + " = " + msg.ReadString());

                    switch (status)
                    {
                    case NetConnectionStatus.Connected:
                        foreach (var montitor in _connectionMonitors)
                        {
                            montitor.OnConnected((Player)msg.SenderConnection.Tag);
                        }
                        break;

                    case NetConnectionStatus.Disconnected:
                        foreach (var monitor in _connectionMonitors)
                        {
                            monitor.OnDisconnected((Player)msg.SenderConnection.Tag);
                        }
                        break;
                    }
                    break;

                case NetIncomingMessageType.DiscoveryRequest: {
                    ServerDiscoveryFormat response = new ServerDiscoveryFormat()
                    {
                        Host  = LocalPlayer,
                        Title = "NYI"
                    };
                    NetOutgoingMessage outgoing = _server.CreateMessage();
                    outgoing.Write(SerializationHelpers.Serialize(response));
                    _server.SendDiscoveryResponse(outgoing, msg.SenderEndPoint);
                    break;
                }

                case NetIncomingMessageType.ConnectionApproval:
                    var hail = SerializationHelpers.Deserialize <HailMessageFormat>(msg.ReadString());

                    // bad password; deny the connection
                    if (hail.Password != _serverPassword)
                    {
                        Log <NetworkContext> .Info("Connection denied (from " + msg.SenderEndPoint + ")");

                        msg.SenderConnection.Deny("Bad password");
                    }

                    // good password; approve the connection
                    else
                    {
                        Log <NetworkContext> .Info("Connection approved (from " + msg.SenderEndPoint + ")");

                        msg.SenderConnection.Tag = hail.Player;

                        NetOutgoingMessage outgoingHail = _server.CreateMessage();
                        outgoingHail.Write(SerializationHelpers.Serialize(LocalPlayer));
                        msg.SenderConnection.Approve(outgoingHail);
                    }
                    break;

                case NetIncomingMessageType.Data:
                    string serializedMessage = msg.ReadString();
                    Log <NetworkContext> .Info("  " + serializedMessage);

                    NetworkMessageFormat message = SerializationHelpers.Deserialize <NetworkMessageFormat>(serializedMessage);

                    if (IsServer && message.IfServerRebroadcast)
                    {
                        var outgoingMessage = CreateMessage(message.Sender, message.NetworkMessage, broadcast: false);
                        _server.SendToAll(outgoingMessage, NetDeliveryMethod.ReliableOrdered);
                    }

                    _dispatcher.InvokeHandlers(message.Sender, message.NetworkMessage);
                    break;

                default:
                    throw new InvalidOperationException("Unhandled type: " + msg.MessageType);
                }
                peer.Recycle(msg);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Update the network context; ie, invoke handlers for received network messages if we're a
        /// client or broadcast out received messages if we're a server.
        /// </summary>
        public void Update() {
            NetPeer peer = Peer;

            NetIncomingMessage msg;
            while ((msg = peer.ReadMessage()) != null) {
                Log<NetworkContext>.Info("NetworkContext.Update: got message type " + msg.MessageType);

                switch (msg.MessageType) {
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.ErrorMessage:
                        Log<NetworkContext>.Warn(msg.ReadString());
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                        Log<NetworkContext>.Info("\\" + status + " = " + msg.ReadString());
                        switch (status) {
                            case NetConnectionStatus.Connected:
                                foreach (var montitor in _connectionMonitors) {
                                    montitor.OnConnected((Player)msg.SenderConnection.Tag);
                                }
                                break;

                            case NetConnectionStatus.Disconnected:
                                foreach (var monitor in _connectionMonitors) {
                                    monitor.OnDisconnected((Player)msg.SenderConnection.Tag);
                                }
                                break;
                        }
                        break;

                    case NetIncomingMessageType.DiscoveryRequest: {
                            ServerDiscoveryFormat response = new ServerDiscoveryFormat() {
                                Host = LocalPlayer,
                                Title = "NYI"
                            };
                            NetOutgoingMessage outgoing = _server.CreateMessage();
                            outgoing.Write(SerializationHelpers.Serialize(response));
                            _server.SendDiscoveryResponse(outgoing, msg.SenderEndPoint);
                            break;
                        }

                    case NetIncomingMessageType.ConnectionApproval:
                        var hail = SerializationHelpers.Deserialize<HailMessageFormat>(msg.ReadString());

                        // bad password; deny the connection
                        if (hail.Password != _serverPassword) {
                            Log<NetworkContext>.Info("Connection denied (from " + msg.SenderEndPoint + ")");
                            msg.SenderConnection.Deny("Bad password");
                        }

                        // good password; approve the connection
                        else {
                            Log<NetworkContext>.Info("Connection approved (from " + msg.SenderEndPoint + ")");
                            msg.SenderConnection.Tag = hail.Player;

                            NetOutgoingMessage outgoingHail = _server.CreateMessage();
                            outgoingHail.Write(SerializationHelpers.Serialize(LocalPlayer));
                            msg.SenderConnection.Approve(outgoingHail);
                        }
                        break;

                    case NetIncomingMessageType.Data:
                        string serializedMessage = msg.ReadString();
                        Log<NetworkContext>.Info("  " + serializedMessage);
                        NetworkMessageFormat message = SerializationHelpers.Deserialize<NetworkMessageFormat>(serializedMessage);

                        if (IsServer && message.IfServerRebroadcast) {
                            var outgoingMessage = CreateMessage(message.Sender, message.NetworkMessage, broadcast: false);
                            _server.SendToAll(outgoingMessage, NetDeliveryMethod.ReliableOrdered);
                        }

                        _dispatcher.InvokeHandlers(message.Sender, message.NetworkMessage);
                        break;

                    default:
                        throw new InvalidOperationException("Unhandled type: " + msg.MessageType);
                }
                peer.Recycle(msg);
            }
        }