Exemplo n.º 1
0
        public static bool RegisterRelayServer(byte id, string password, RelayConnection con, short port, string ip)
        {
            if (!CurrentRelayServer.ContainsKey(id))
            {
                Log.Info("Game Server ID: {0} is not defined, please check", id);
                return(false);
            }

            var template = CurrentRelayServer[id]; //Checking Containing By Packet

            if (con.CurrentInfo != null)           //Fully Checking.
            {
                con.CurrentInfo = null;
            }

            if (template.password != password) //Checking Password
            {
                Log.Info("Game Server ID: {0} bad password", id);
                return(false);
            }

            var server = CurrentRelayServer[id];

            server.CurrentConnection = con;
            server.IPAddress         = ip;
            server.Port     = port;
            con.CurrentInfo = server;
            //Update
            CurrentRelayServer.Remove(id);
            CurrentRelayServer.Add(id, server);
            Log.Info("RelayServer ID: {0} registered", id);
            return(true);
        }
Exemplo n.º 2
0
        async Task ProcessConnection(RelayConnection connection)
        {
            // implements a simple protocol that uploads and downloads 1MB of data
            // since the connection is duplex, this happens in parallel.

            Console.WriteLine("Processing connection");
            // download
            var download = Task.Run(async() =>
            {
                Console.WriteLine("downloading data");
                var buf        = new byte[1024 * 1024];
                int totalBytes = 0, bytesRead;
                do
                {
                    totalBytes += bytesRead = await connection.ReadAsync(buf, 0, buf.Length);
                }while (bytesRead > 0);
                Console.WriteLine("downloaded complete, {0} bytes", totalBytes);
            });

            // upload
            var upload = Task.Run(async() =>
            {
                var buf = new byte[1024];
                for (var i = 0; i < 1024; i++)
                {
                    Rnd.NextBytes(buf);
                    await connection.WriteAsync(buf, 0, buf.Length);
                }
                await connection.ShutdownAsync();
            });

            Task.WaitAll(upload, download);
            connection.Close();
            Console.WriteLine("Connection done");
        }
Exemplo n.º 3
0
        Task SendAndReceive(RelayConnection connection)
        {
            // implements a simple protocol that uploads and downloads 1MB of data
            // since the connection is duplex, this happens in parallel.

            Console.WriteLine("Processing connection");
            // download
            var download = Task.Run(async() =>
            {
                Console.WriteLine("Downloading 1MByte");
                var buf = new byte[1024 * 1024];
                int bytesRead;
                do
                {
                    bytesRead = await connection.ReadAsync(buf, 0, buf.Length);
                } while (bytesRead > 0);
                Console.WriteLine("Download done");
            });

            // upload
            var upload = Task.Run(async() =>
            {
                Console.WriteLine("Uploading 1MByte");
                var buf = new byte[16 * 1024];
                for (var i = 0; i < 1024 / 16; i++)
                {
                    rnd.NextBytes(buf);
                    await connection.WriteAsync(buf, 0, buf.Length);
                }
                await connection.ShutdownAsync();
                Console.WriteLine("Upload done");
            });

            return(Task.WhenAll(upload, download));
        }
Exemplo n.º 4
0
        public static void Handle_RegisterRelayServer(RelayConnection net, PacketReader reader)
        {
            byte   id       = reader.ReadByte();
            short  port     = reader.ReadLEInt16();
            string ip       = reader.ReadDynamicString();
            string password = reader.ReadDynamicString();
            bool   success  = RelayController.RegisterRelayServer(id, password, net, port, ip);

            net.SendAsync(new NET_RelayRegistrationResult(success));
        }
Exemplo n.º 5
0
        public static void Handle_GetUDPInfo(RelayConnection net, PacketReader reader)
        {
            int    session = reader.ReadLEInt32();
            short  port    = reader.ReadLEInt16();
            int    iplen   = reader.ReadLEInt32();
            string ip      = reader.ReadStringSafe(iplen);
            //Console.WriteLine("Session: {0:X2}, UDPport: {1}, ip: {2}", session, port, ip);

            Account ac = ClientConnection.CurrentAccounts.First(p => p.Key == session).Value;

            ac.UDPPort = port;
        }