예제 #1
0
        void NetThread()
        {
            FLLog.Info("Server", "Loading Game Data...");
            GameData.LoadData();
            FLLog.Info("Server", "Finished Loading Game Data");
            Database = new ServerDatabase(DbConnectionString);
            var netconf = new NetPeerConfiguration(AppIdentifier);

            netconf.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
            netconf.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
            netconf.EnableMessageType(NetIncomingMessageType.UnconnectedData);
            netconf.Port = Port;
            netconf.MaximumConnections = 200;
            NetServer = new NetServer(netconf);
            NetServer.Start();
            FLLog.Info("Server", "Listening on port " + Port);
            NetIncomingMessage im;

            while (running)
            {
                while ((im = NetServer.ReadMessage()) != null)
                {
                    switch (im.MessageType)
                    {
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.ErrorMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.VerboseDebugMessage:
                        FLLog.Info("Lidgren", im.ReadString());
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.ConnectionApproval:
                        //Ban IP?
                        im.SenderConnection.Approve();
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.DiscoveryRequest:
                        NetOutgoingMessage dresp = NetServer.CreateMessage();
                        //Include Server Data
                        dresp.Write(ServerName);
                        dresp.Write(ServerDescription);
                        dresp.Write(GameData.DataVersion);
                        dresp.Write(NetServer.ConnectionsCount);
                        dresp.Write(NetServer.Configuration.MaximumConnections);
                        //Send off
                        NetServer.SendDiscoveryResponse(dresp, im.SenderEndPoint);
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.UnconnectedData:
                        //Respond to pings
                        try
                        {
                            if (im.ReadUInt32() == NetConstants.PING_MAGIC)
                            {
                                var om = NetServer.CreateMessage();
                                om.Write(NetConstants.PING_MAGIC);
                                NetServer.SendUnconnectedMessage(om, im.SenderEndPoint);
                            }
                        }
                        catch (Exception)
                        {
                        }
                        break;

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

                        string reason = im.ReadString();
                        FLLog.Info("Lidgren", NetUtility.ToHexString(im.SenderConnection.RemoteUniqueIdentifier) + " " + status + ": " + reason);

                        if (status == NetConnectionStatus.Connected)
                        {
                            FLLog.Info("Lidgren", "Remote hail: " + im.SenderConnection.RemoteHailMessage.ReadString());
                            BeginAuthentication(NetServer, im.SenderConnection);
                        }
                        else if (status == NetConnectionStatus.Disconnected)
                        {
                            FLLog.Info("Lidgren", im.SenderEndPoint.ToString() + " disconnected");
                            if (im.SenderConnection.Tag is NetPlayer)
                            {
                                ((NetPlayer)im.SenderConnection.Tag).Disconnected();
                            }
                        }
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.Data:
                        var pkt = im.ReadPacket();
                        if (im.SenderConnection.Tag == TagConnecting)
                        {
                            if (pkt is AuthenticationReplyPacket)
                            {
                                var auth = (AuthenticationReplyPacket)pkt;

                                //im.SenderConnection.Disconnect("boilerplate reason from server");

                                /*
                                 *                                  var authkind = (AuthenticationKind)im.ReadByte();
                                 *                                  var guid = new Guid(im.ReadBytes(16));
                                 *                                  if (guid == Guid.Empty) im.SenderConnection.Disconnect("Invalid UUID");
                                 *                                  FLLog.Info("Lidgren", "GUID for " + im.SenderEndPoint + " = " + guid.ToString());
                                 *                                  var p = new NetPlayer(im.SenderConnection, this, guid);
                                 *                                  im.SenderConnection.Tag = p;
                                 *                                  AsyncManager.RunTask(() => p.DoAuthSuccess());*/

                                var p = new NetPlayer(im.SenderConnection, this, auth.Guid);
                                im.SenderConnection.Tag = p;
                                AsyncManager.RunTask(() => p.DoAuthSuccess());
                            }
                            else
                            {
                                im.SenderConnection.Disconnect("Invalid Packet");
                            }
                            NetServer.Recycle(im);
                        }
                        else
                        {
                            var player = (NetPlayer)im.SenderConnection.Tag;
                            AsyncManager.RunTask(() => player.ProcessPacket(pkt));
                            NetServer.Recycle(im);
                        }
                        break;
                    }
                }
                Thread.Sleep(0);                 //Reduce CPU load
            }
            NetServer.Shutdown("Shutdown");
            Database.Dispose();
        }
예제 #2
0
        void NetThread()
        {
            FLLog.Info("Server", "Loading Game Data...");
            GameData.LoadData();
            FLLog.Info("Server", "Finished Loading Game Data");
            Database = new ServerDatabase(DbConnectionString, GameData);
            var netconf = new NetPeerConfiguration(AppIdentifier);

            netconf.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
            netconf.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
            netconf.Port = Port;
            netconf.MaximumConnections = 200;
            NetServer = new NetServer(netconf);
            NetServer.Start();
            FLLog.Info("Server", "Listening on port " + Port);
            NetIncomingMessage im;

            while (running)
            {
                while ((im = NetServer.ReadMessage()) != null)
                {
                    switch (im.MessageType)
                    {
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.ErrorMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.VerboseDebugMessage:
                        FLLog.Info("Lidgren", im.ReadString());
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.ConnectionApproval:
                        //Ban IP?
                        im.SenderConnection.Approve();
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.DiscoveryRequest:
                        NetOutgoingMessage dresp = NetServer.CreateMessage();
                        //Include Server Data
                        dresp.Write(ServerName);
                        dresp.Write(ServerDescription);
                        dresp.Write(NetServer.ConnectionsCount);
                        dresp.Write(NetServer.Configuration.MaximumConnections);
                        //Send off
                        NetServer.SendDiscoveryResponse(dresp, im.SenderEndPoint);
                        NetServer.Recycle(im);
                        break;

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

                        string reason = im.ReadString();
                        FLLog.Info("Lidgren", NetUtility.ToHexString(im.SenderConnection.RemoteUniqueIdentifier) + " " + status + ": " + reason);

                        if (status == NetConnectionStatus.Connected)
                        {
                            FLLog.Info("Lidgren", "Remote hail: " + im.SenderConnection.RemoteHailMessage.ReadString());
                            BeginAuthentication(NetServer, im.SenderConnection);
                        }
                        NetServer.Recycle(im);
                        break;

                    case NetIncomingMessageType.Data:
                        var kind = (PacketKind)im.ReadByte();
                        if (im.SenderConnection.Tag == TagConnecting)
                        {
                            if (kind == PacketKind.Authentication)
                            {
                                var authkind = (AuthenticationKind)im.ReadByte();
                                var guid     = new Guid(im.ReadBytes(16));
                                if (guid == Guid.Empty)
                                {
                                    im.SenderConnection.Disconnect("Invalid UUID");
                                }
                                FLLog.Info("Lidgren", "GUID for " + im.SenderEndPoint + " = " + guid.ToString());
                                var p = new NetPlayer(im.SenderConnection, this, guid);
                                im.SenderConnection.Tag = p;
                                AsyncManager.RunTask(() => p.DoAuthSuccess());
                            }
                            else
                            {
                                im.SenderConnection.Disconnect("Invalid Packet");
                            }
                        }
                        else
                        {
                            var player = (NetPlayer)im.SenderConnection.Tag;
                            AsyncManager.RunTask(() => player.ProcessPacket(im, kind));
                        }
                        break;
                    }
                }
                Thread.Sleep(1);                 //Reduce CPU load
            }
            Database.Dispose();
        }