Ejemplo n.º 1
0
        public void Update()
        {
            if (client == null)
            {
                throw new Exception("net client object is null");
            }

            NetIncomingMessage msg = null;

            while ((msg = client.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.StatusChanged:
                {
                    NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();

                    if (clientDelegate != null)
                    {
                        if (status == NetConnectionStatus.Connected)
                        {
                            clientDelegate.OnNetworkClientConnect(this, 1);
                        }

                        if (status == NetConnectionStatus.Disconnected)
                        {
                            clientDelegate.OnNetworkClientDisconnect(this, 1);
                        }
                    }
                }
                break;

                case NetIncomingMessageType.Data:
                {
                    NetworkMessageType msgid = (NetworkMessageType)msg.PeekUInt16();
                    NetworkMessage     flmsg = msgFactory.Create(msgid);

                    if ((clientDelegate != null) && (flmsg != null))
                    {
                        flmsg.Read(msg);
                        clientDelegate.OnNetworkClientMessage(this, flmsg);
                    }
                }
                break;

                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.WarningMessage:
                case NetIncomingMessageType.ErrorMessage:
                default:
                    break;
                }

                client.Recycle(msg);
            }
        }
Ejemplo n.º 2
0
        public void Update(float time)
        {
            NetIncomingMessage im;

            while ((im = server.ReadMessage()) != null)
            {
                switch (im.MessageType)
                {
                case NetIncomingMessageType.DebugMessage:
                    break;

                case NetIncomingMessageType.ErrorMessage:
                    break;

                case NetIncomingMessageType.WarningMessage:
                    break;

                case NetIncomingMessageType.VerboseDebugMessage:
                    break;

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

                    if (status == NetConnectionStatus.Connected)
                    {
                        int newClient = clientIDStack.Pop();

                        clientData[newClient]   = new ClientData(newClient, im.SenderConnection);
                        im.SenderConnection.Tag = clientData[newClient];

                        if (serverDelegate != null)
                        {
                            serverDelegate.OnNetworkServerClientConnected(this, newClient);
                        }
                    }

                    if (status == NetConnectionStatus.Disconnected)
                    {
                        int clientID = ((ClientData)im.SenderConnection.Tag).id;

                        if (serverDelegate != null)
                        {
                            serverDelegate.OnNetworkServerClientDisconnected(this, clientID);
                        }

                        clientData[clientID] = null;
                        clientIDStack.Push(clientID);
                    }

                    break;

                case NetIncomingMessageType.Data:
                {
                    int clientID = ((ClientData)im.SenderConnection.Tag).id;

                    NetworkMessageType msgid = (NetworkMessageType)im.PeekUInt16();

                    NetworkMessage flmsg = msgFactory.Create(msgid);

                    if ((serverDelegate != null) && (flmsg != null))
                    {
                        flmsg.Read(im);
                        flmsg.source = clientID;
                        serverDelegate.OnNetworkServerClientMessage(this, clientID, flmsg);
                    }
                }
                break;

                default:
                    break;
                }

                server.Recycle(im);
            }
        }