コード例 #1
0
 public void WriteTo(BinaryWriter writer)
 {
     writer.Write(name);
     writer.Write((ushort)playerCount);
     Tools.Serialize(writer, internalAddress);
     Tools.Serialize(writer, externalAddress);
 }
コード例 #2
0
 public void ReadFrom(BinaryReader reader)
 {
     name        = reader.ReadString();
     playerCount = reader.ReadUInt16();
     Tools.Serialize(reader, out internalAddress);
     Tools.Serialize(reader, out externalAddress);
 }
コード例 #3
0
        /// <summary>
        /// Send periodic updates.
        /// </summary>

        void ThreadFunction()
        {
            mInternal = new IPEndPoint(Tools.localAddress, mGameServer.tcpPort);
            mExternal = new IPEndPoint(Tools.externalAddress, mGameServer.tcpPort);

            for (; ;)
            {
#if !STANDALONE
                if (TNManager.isPaused)
                {
                    Thread.Sleep(500);
                    continue;
                }
#endif
                long time = DateTime.UtcNow.Ticks / 10000;

                if (mShutdown)
                {
                    Buffer       buffer = Buffer.Create();
                    BinaryWriter writer = buffer.BeginPacket(Packet.RequestRemoveServer);
                    writer.Write(GameServer.gameID);
                    Tools.Serialize(writer, mInternal);
                    Tools.Serialize(writer, mExternal);
                    buffer.EndPacket();
                    mUdp.Send(buffer, mRemoteAddress);
                    buffer.Recycle();
                    mThread = null;
                    break;
                }

                if (mNextSend < time && mGameServer != null)
                {
                    mNextSend = time + 3000;
                    Buffer buffer = Buffer.Create();
                    var    writer = buffer.BeginPacket(Packet.RequestAddServer);
                    writer.Write(GameServer.gameID);
                    writer.Write(mGameServer.name);
                    writer.Write((short)mGameServer.playerCount);
                    Tools.Serialize(writer, mInternal);
                    Tools.Serialize(writer, mExternal);
                    buffer.EndPacket();
                    mUdp.Send(buffer, mRemoteAddress);
                    buffer.Recycle();
                }

                try { Thread.Sleep(10); }
                catch (System.Threading.ThreadInterruptedException) { return; }
            }
        }
コード例 #4
0
        /// <summary>
        /// Send periodic updates.
        /// </summary>

        void ThreadFunction()
        {
            mInternal = new IPEndPoint(Tools.localAddress, mGameServer.tcpPort);
            mExternal = new IPEndPoint(Tools.externalAddress, mGameServer.tcpPort);

            for (; ;)
            {
                long time = DateTime.Now.Ticks / 10000;

                if (mShutdown)
                {
                    Buffer       buffer = Buffer.Create();
                    BinaryWriter writer = buffer.BeginPacket(Packet.RequestRemoveServer);
                    writer.Write(GameServer.gameID);
                    Tools.Serialize(writer, mInternal);
                    Tools.Serialize(writer, mExternal);
                    buffer.EndPacket();
                    mUdp.Send(buffer, mRemoteAddress);
                    buffer.Recycle();
                    mThread = null;
                    break;
                }

                if (mNextSend < time && mGameServer != null)
                {
                    mNextSend = time + 3000;
                    Buffer       buffer = Buffer.Create();
                    BinaryWriter writer = buffer.BeginPacket(Packet.RequestAddServer);
                    writer.Write(GameServer.gameID);
                    writer.Write(mGameServer.name);
                    writer.Write((short)mGameServer.playerCount);
                    Tools.Serialize(writer, mInternal);
                    Tools.Serialize(writer, mExternal);
                    buffer.EndPacket();
                    mUdp.Send(buffer, mRemoteAddress);
                    buffer.Recycle();
                }
                Thread.Sleep(10);
            }
        }
        /// <summary>
        /// Send periodic updates.
        /// </summary>

        void ThreadFunction()
        {
            mInternal = new IPEndPoint(Tools.localAddress, mGameServer.tcpPort);
            mExternal = new IPEndPoint(Tools.externalAddress, mGameServer.tcpPort);

            for (; ;)
            {
                long time = DateTime.UtcNow.Ticks / 10000;

                if (mShutdown)
                {
                    mTcp.Disconnect();
                    mThread = null;
#if STANDALONE
                    Tools.Print("TcpLobbyLink shut down");
#endif
                    break;
                }

#if !STANDALONE
                if (TNManager.isPaused)
                {
                    Thread.Sleep(500);
                    continue;
                }
#endif
                Buffer buffer;

                // Try to establish a connection
                if (mGameServer != null && !mTcp.isConnected && !mTcp.isTryingToConnect && mNextConnect < time)
                {
#if STANDALONE
                    Tools.Print("TcpLobbyLink is connecting to " + mRemoteAddress + "...");
#endif
                    mUpdateNeeded = true;
                    mNextConnect  = time + 15000;
                    mTcp.Connect(mRemoteAddress);
                }

                while (mTcp.ReceivePacket(out buffer))
                {
                    BinaryReader reader   = buffer.BeginReading();
                    Packet       response = (Packet)reader.ReadByte();

                    if (mTcp.stage == TcpProtocol.Stage.Verifying)
                    {
                        if (mTcp.VerifyResponseID(response, reader))
                        {
                            mTimeDifference = reader.ReadInt64() - (System.DateTime.UtcNow.Ticks / 10000);
                            mWasConnected   = true;
#if STANDALONE
                            Tools.Print("TcpLobbyLink connection established");
#endif
                        }
                        else
                        {
#if STANDALONE
                            Tools.Print("TcpLobbyLink Error: Protocol version mismatch");
#endif
                            mThread = null;
                            return;
                        }
                    }
                    else if (response == Packet.RequestPing)
                    {
                    }
#if STANDALONE
                    else if (response == Packet.Error)
                    {
                        Tools.Print("TcpLobbyLink Error: " + reader.ReadString());
                    }
                    else if (response == Packet.Disconnect)
                    {
                        Tools.Print("TcpLobbyLink disconnected");
                    }
                    else
                    {
                        Tools.Print("TcpLobbyLink can't handle this packet: " + response);
                    }
#endif
                    buffer.Recycle();
                }


                // Automatically try to re-establish a connection on disconnect
                if (mWasConnected && !mTcp.isConnected && !mTcp.isTryingToConnect)
                {
                    mNextConnect  = time + 5000;
                    mWasConnected = false;
                }
                else if (mGameServer != null && mTcp.isConnected && (mUpdateNeeded || mNextSend < time))
                {
                    mUpdateNeeded = false;
                    mNextSend     = time + 5000;

                    Buffer       buff   = Buffer.Create();
                    BinaryWriter writer = buff.BeginPacket(Packet.RequestAddServer);
                    writer.Write(GameServer.gameID);
                    writer.Write(mGameServer.name);
                    writer.Write((short)mGameServer.playerCount);
                    Tools.Serialize(writer, mInternal);
                    Tools.Serialize(writer, mExternal);
                    buff.EndPacket();
                    mTcp.SendTcpPacket(buff);
                    buff.Recycle();
                }

                try { Thread.Sleep(10); }
                catch (System.Threading.ThreadInterruptedException) { return; }
            }
        }
コード例 #6
0
        /// <summary>
        /// Process an incoming packet.
        /// </summary>

        bool ProcessPacket(Buffer buffer, IPEndPoint ip)
        {
            BinaryReader reader  = buffer.BeginReading();
            Packet       request = (Packet)reader.ReadByte();

            switch (request)
            {
            case Packet.RequestPing:
            {
                BeginSend(Packet.ResponsePing);
                EndSend(ip);
                break;
            }

            case Packet.RequestAddServer:
            {
                if (reader.ReadUInt16() != GameServer.gameID)
                {
                    return(false);
                }
                ServerList.Entry ent = new ServerList.Entry();
                ent.ReadFrom(reader);

                if (ent.externalAddress.Address.Equals(IPAddress.None))
                {
                    ent.externalAddress = ip;
                }

                mList.Add(ent, mTime);
#if STANDALONE
                Tools.Print(ip + " added a server (" + ent.internalAddress + ", " + ent.externalAddress + ")");
#endif
                return(true);
            }

            case Packet.RequestRemoveServer:
            {
                if (reader.ReadUInt16() != GameServer.gameID)
                {
                    return(false);
                }
                IPEndPoint internalAddress, externalAddress;
                Tools.Serialize(reader, out internalAddress);
                Tools.Serialize(reader, out externalAddress);

                if (externalAddress.Address.Equals(IPAddress.None))
                {
                    externalAddress = ip;
                }

                RemoveServer(internalAddress, externalAddress);
#if STANDALONE
                Tools.Print(ip + " removed a server (" + internalAddress + ", " + externalAddress + ")");
#endif
                return(true);
            }

            case Packet.RequestServerList:
            {
                if (reader.ReadUInt16() != GameServer.gameID)
                {
                    return(false);
                }
                mList.WriteTo(BeginSend(Packet.ResponseServerList));
                EndSend(ip);
                return(true);
            }
            }
            return(false);
        }
コード例 #7
0
        /// <summary>
        /// Send periodic updates.
        /// </summary>

        void ThreadFunction()
        {
            mInternal = new IPEndPoint(Tools.localAddress, mGameServer.tcpPort);
            mExternal = new IPEndPoint(Tools.externalAddress, mGameServer.tcpPort);

            for (; ;)
            {
                long time = DateTime.UtcNow.Ticks / 10000;

                if (mShutdown)
                {
                    mTcp.Disconnect();
                    mThread = null;
                    break;
                }

                Buffer buffer;

                // Try to establish a connection
                if (mGameServer != null && !mTcp.isConnected && mNextConnect < time)
                {
                    mNextConnect = time + 15000;
                    mTcp.Connect(mRemoteAddress);
                }

                while (mTcp.ReceivePacket(out buffer))
                {
                    BinaryReader reader   = buffer.BeginReading();
                    Packet       response = (Packet)reader.ReadByte();

                    if (mTcp.stage == TcpProtocol.Stage.Verifying)
                    {
                        if (mTcp.VerifyResponseID(response, reader))
                        {
                            mTimeDifference = reader.ReadInt64() - (System.DateTime.UtcNow.Ticks / 10000);
                            mWasConnected   = true;
                        }
                        else
                        {
#if STANDALONE
                            Console.WriteLine("TcpLobbyLink: Protocol version mismatch");
#endif
                            mThread = null;
                            return;
                        }
                    }
                    else if (response == Packet.Error)
                    {
                        // Automatically try to re-establish a connection on disconnect
                        mNextConnect = mWasConnected ? 0 : time + 30000;
#if STANDALONE
                        Console.WriteLine("TcpLobbyLink: " + reader.ReadString());
#endif
                    }
#if STANDALONE
                    else
                    {
                        Console.WriteLine("TcpLobbyLink can't handle this packet: " + response);
                    }
#endif
                    buffer.Recycle();
                }

                if (mGameServer != null && mTcp.isConnected)
                {
                    BinaryWriter writer = mTcp.BeginSend(Packet.RequestAddServer);
                    writer.Write(GameServer.gameID);
                    writer.Write(mGameServer.name);
                    writer.Write((short)mGameServer.playerCount);
                    Tools.Serialize(writer, mInternal);
                    Tools.Serialize(writer, mExternal);
                    mTcp.EndSend();
                    mGameServer = null;
                }
                Thread.Sleep(10);
            }
        }
コード例 #8
0
        /// <summary>
        /// Process an incoming packet.
        /// </summary>

        bool ProcessPacket(Buffer buffer, IPEndPoint ip)
        {
            if (mBan.Count != 0 && mBan.Contains(ip.Address.ToString()))
            {
                return(false);
            }

            var reader  = buffer.BeginReading();
            var request = (Packet)reader.ReadByte();

            switch (request)
            {
            case Packet.RequestPing:
            {
                var writer = BeginSend(Packet.ResponsePing);
                writer.Write(mTime);
                writer.Write((ushort)mList.list.size);
                EndSend(ip);
                break;
            }

            case Packet.RequestAddServer:
            {
                if (reader.ReadUInt16() != GameServer.gameID)
                {
                    return(false);
                }

                var ent = new ServerList.Entry();
                ent.ReadFrom(reader);

                if (mBan.Count != 0 && (mBan.Contains(ent.externalAddress.Address.ToString()) || IsBanned(ent.name)))
                {
                    return(false);
                }

                if (ent.externalAddress.Address.Equals(IPAddress.None) ||
                    ent.externalAddress.Address.Equals(IPAddress.IPv6None))
                {
                    ent.externalAddress = ip;
                }

                mList.Add(ent, mTime);
#if STANDALONE
                Tools.Print(ip + " added a server (" + ent.internalAddress + ", " + ent.externalAddress + ")");
#endif
                return(true);
            }

            case Packet.RequestRemoveServer:
            {
                if (reader.ReadUInt16() != GameServer.gameID)
                {
                    return(false);
                }
                IPEndPoint internalAddress, externalAddress;
                Tools.Serialize(reader, out internalAddress);
                Tools.Serialize(reader, out externalAddress);

                if (externalAddress.Address.Equals(IPAddress.None) ||
                    externalAddress.Address.Equals(IPAddress.IPv6None))
                {
                    externalAddress = ip;
                }

                RemoveServer(internalAddress, externalAddress);
#if STANDALONE
                Tools.Print(ip + " removed a server (" + internalAddress + ", " + externalAddress + ")");
#endif
                return(true);
            }

            case Packet.RequestServerList:
            {
                if (reader.ReadUInt16() != GameServer.gameID)
                {
                    return(false);
                }
                mList.WriteTo(BeginSend(Packet.ResponseServerList));
                EndSend(ip);
                return(true);
            }
            }
            return(false);
        }