Exemplo n.º 1
0
        private static void Configure()
        {
            if (IsAlive || !IsEnabled)
            {
                return;
            }

            PortalTransport t = null;

            if (IsServer)
            {
                t = new PortalServer();
            }
            else if (IsClient)
            {
                if (CreateClientHandler != null)
                {
                    t = CreateClientHandler(null);
                }

                if (t == null)
                {
                    t = new PortalClient();
                }
            }

            _Transport = t;
        }
Exemplo n.º 2
0
 public static void InvokeConnected(PortalClient client)
 {
     if (client != null && client.IsAlive && OnConnected != null)
     {
         OnConnected(client);
     }
 }
Exemplo n.º 3
0
 public static void InvokeDisposed(PortalClient client)
 {
     if (client != null && OnDisposed != null)
     {
         ThreadPool.QueueUserWorkItem(InvokeDisposed, client);
     }
 }
Exemplo n.º 4
0
 public static void InvokeDisposed(PortalClient client)
 {
     if (client != null && (client.IsDisposing || client.IsDisposed) && OnDisposed != null)
     {
         OnDisposed(client);
     }
 }
Exemplo n.º 5
0
        private static void Configure()
        {
            if (!IsEnabled)
            {
                Stop();
                return;
            }

            if (IsAlive)
            {
                return;
            }

            Stop();

            if (!IsEnabled)
            {
                return;
            }

            Thread = new Thread(ThreadStart)
            {
                Name = "Portal" + (IsServer ? " Server" : IsClient ? " Client" : String.Empty)
            };

            if (IsServer)
            {
                Transport = new PortalServer();
            }
            else
            {
                Transport = new PortalClient();
            }
        }
Exemplo n.º 6
0
        public static void RegisterHandlers(PortalClient client)
        {
            client.Register(0, PortalContext.Server, OnHandshakeRequest);
            client.Register(1, PortalContext.Client, OnHandshakeResponse);

            client.Register(2, PortalContext.Any, OnPingRequest);
            client.Register(3, PortalContext.Any, OnPingResponse);

            client.Register(255, PortalContext.Any, OnDisconnectNotify);

            if (OnRegister != null)
            {
                OnRegister(client);
            }
        }
Exemplo n.º 7
0
        public static void RegisterHandlers(PortalClient client)
        {
            client.Register(0, PortalContext.Server, OnHandshakeRequest);
            client.Register(1, PortalContext.Client, OnHandshakeResponse);

            client.Register(2, PortalContext.Any, OnPingRequest);
            client.Register(3, PortalContext.Any, OnPingResponse);

            client.Register(255, PortalContext.Any, OnDisconnectNotify);

            if (OnRegister != null)
            {
                OnRegister(client);
            }
        }
Exemplo n.º 8
0
        private static void OnHandshakeRequest(PortalClient client, PortalPacketReader p)
        {
            if (client.IsAuthed)
            {
                client.Send(PortalPackets.HandshakeResponse.Accepted);
                return;
            }

            var key = p.ReadBytes(20);

            client.IsAuthed = PortalAuthentication.Key.SequenceEqual(key);

            var r = client.IsAuthed ? PortalPackets.HandshakeResponse.Accepted : PortalPackets.HandshakeResponse.Rejected;

            client.Send(r);
        }
Exemplo n.º 9
0
        private static void OnHandshakeRequest(PortalClient client, PortalPacketReader p)
        {
            if (client.IsAuthed)
            {
                client.Send(PortalPackets.HandshakeResponse.Accepted);
                return;
            }

            var key = p.ReadBytes(20);

            client.IsAuthed = PortalAuthentication.Key.SequenceEqual(key);

            var r = client.IsAuthed ? PortalPackets.HandshakeResponse.Accepted : PortalPackets.HandshakeResponse.Rejected;

            client.Send(r);
        }
Exemplo n.º 10
0
        private void OnAccept(IAsyncResult r)
        {
            PortalClient client = null;

            try
            {
                var socket = _Server.EndAccept(r);

                if (socket != null)
                {
                    if (Portal.CreateClientHandler != null)
                    {
                        client = Portal.CreateClientHandler(socket);
                    }

                    if (client == null)
                    {
                        client = new PortalClient(socket);
                    }

                    _Accepted.Enqueue(client);
                }
            }
            catch
            {
                if (client != null)
                {
                    _Disposed.Enqueue(client);
                }
            }

            try
            {
                BeginAccept();
                return;
            }
            catch (Exception e)
            {
                ToConsole("Listener: Failed", e);
            }

            Dispose();
        }
Exemplo n.º 11
0
        private static void OnHandshakeRequest(PortalClient client, PortalPacketReader p)
        {
            if (client.IsAuthed)
            {
                client.Send(PortalPackets.HandshakeResponse.Accepted);
                return;
            }

            var key = p.ReadBytes(20);

            client.IsAuthed = PortalAuthentication.Verify(key);

            if (client.IsAuthed && Portal.UniqueIDs && Portal.Transport is PortalServer &&
                ((PortalServer)Portal.Transport).Clients.Any(c => c != client && c.IsIdentified && c.ServerID == p.ServerID))
            {
                client.IsAuthed = false;
            }

            var r = client.IsAuthed             //
                                ? PortalPackets.HandshakeResponse.Accepted
                                : PortalPackets.HandshakeResponse.Rejected;

            client.Send(r);
        }
Exemplo n.º 12
0
        private static void OnPingRequest(PortalClient client, PortalPacketReader p)
        {
            p.ReadToEnd(); // random bytes

            client.Send(PortalPackets.PingResponse.Instance);
        }
Exemplo n.º 13
0
 private static void OnHandshakeResponse(PortalClient client, PortalPacketReader p)
 {
     client.IsAuthed = p.ReadBoolean();
 }
Exemplo n.º 14
0
 private static void OnDisconnectNotify(PortalClient client, PortalPacketReader p)
 {
     client.Dispose();
 }
Exemplo n.º 15
0
 private static void OnDisconnectNotify(PortalClient client, PortalPacketReader p)
 {
     client.Dispose();
 }
Exemplo n.º 16
0
 private static void OnPingResponse(PortalClient client, PortalPacketReader p)
 {
     p.ReadToEnd();             // random bytes
 }
Exemplo n.º 17
0
 private static void OnHandshakeResponse(PortalClient client, PortalPacketReader p)
 {
     client.IsAuthed = p.ReadBoolean();
 }
Exemplo n.º 18
0
 public static void InvokeConnected(PortalClient client)
 {
     if (client != null && client.IsAlive && OnConnected != null)
     {
         OnConnected(client);
     }
 }
Exemplo n.º 19
0
        private static void OnConnected(PortalClient c)
        {
            PingTest(c);

            c.Dispose();
        }
Exemplo n.º 20
0
        protected override void OnStart()
        {
            try
            {
                ToConsole("Listener: Binding: {0}", Portal.Server);

                _Server.LingerState.Enabled = false;
#if !MONO
                _Server.ExclusiveAddressUse = false;
#endif
                _Server.Bind(Portal.Server);
                _Server.Listen(8);

                ToConsole("Listener: Bound: {0}", Portal.Server);
            }
            catch (Exception e)
            {
                ToConsole("Listener: Failed: {0}", e.Message);

                Dispose();
                return;
            }

            try
            {
                do
                {
                    if (_Server != null && _Server.Poll(-1, SelectMode.SelectRead))
                    {
                        var s = _Server.Accept();

                        var client = new PortalClient(s);

                        lock (_ClientsLock)
                        {
                            _Clients.Add(client);

                            ToConsole("{0} Connected [{1} Active]", client, _Clients.Count);
                        }

                        try
                        {
                            var thread = new Thread(client.Start);

                            thread.Start();

                            while (!thread.IsAlive)
                            {
                                Thread.Sleep(1);
                            }
                        }
                        catch
                        {
                            lock (_ClientsLock)
                            {
                                _Clients.Remove(client);

                                ToConsole("{0} Disconnected [{1} Active]", client, _Clients.Count);
                            }

                            try
                            {
                                client.Dispose();
                            }
                            catch
                            { }
                        }
                    }

                    Thread.Sleep(10);
                }while (CheckAlive());
            }
            catch (Exception e)
            {
                ToConsole("Listener: Failed: {0}", e.Message);

                Dispose();
            }
        }
Exemplo n.º 21
0
 private static void OnPingResponse(PortalClient client, PortalPacketReader p)
 {
     p.ReadToEnd(); // random bytes
 }
Exemplo n.º 22
0
        private static void OnPingRequest(PortalClient client, PortalPacketReader p)
        {
            p.ReadToEnd();             // random bytes

            client.Send(PortalPackets.PingResponse.Instance);
        }
Exemplo n.º 23
0
        protected override void OnStart()
        {
            try
            {
                ToConsole("Listener: Binding: {0}", Portal.Server);

                _Server.LingerState.Enabled = false;
            #if !MONO
                _Server.ExclusiveAddressUse = false;
            #endif
                _Server.Bind(Portal.Server);
                _Server.Listen(8);

                ToConsole("Listener: Bound: {0}", Portal.Server);
            }
            catch (Exception e)
            {
                ToConsole("Listener: Failed", e);

                Dispose();
                return;
            }

            try
            {
                do
                {
                    if (_Server != null && _Server.Poll(-1, SelectMode.SelectRead))
                    {
                        var s = _Server.Accept();

                        PortalClient client = null;

                        if (CreateClientHandler != null)
                        {
                            client = CreateClientHandler(s);
                        }

                        if (client == null)
                        {
                            client = new PortalClient(s);
                        }

                        lock (_ClientsLock)
                        {
                            _Clients.Add(client);

                            ToConsole("{0} Connected [{1} Active]", client, _Clients.Count);
                        }

                        try
                        {
                            var thread = new Thread(client.Start)
                            {
                                Name = client.ToString()
                            };

                            thread.Start();

                            while (!thread.IsAlive)
                            {
                                Thread.Sleep(10);
                            }
                        }
                        catch
                        {
                            lock (_ClientsLock)
                            {
                                _Clients.Remove(client);

                                ToConsole("{0} Disconnected [{1} Active]", client, _Clients.Count);
                            }

                            try
                            {
                                client.Dispose();
                            }
                            catch
                            { }
                        }
                    }

                    Thread.Sleep(10);
                }
                while (CheckAlive());
            }
            catch (Exception e)
            {
                ToConsole("Listener: Failed", e);
            }

            Dispose();
        }
Exemplo n.º 24
0
        private static void OnPingRequest(PortalClient client, PortalPacketReader p)
        {
            //p.ReadToEnd(); // random bytes

            client.Ping(true);
        }
Exemplo n.º 25
0
        private static void PingTest(PortalClient c)
        {
            if (Closing || !c.IsAlive)
            {
                return;
            }

            var count = 0;

            do
            {
                Portal.ToConsole("Test: Enter the number of ping requests to send...");
            }while (!Closing && c.IsAlive && (!Int32.TryParse(Console.ReadLine(), out count) || count < 0));

            if (Closing || !c.IsAlive)
            {
                Portal.ToConsole("Test: Skipped ping testing...");
                return;
            }

            if (count <= 0)
            {
                Portal.ToConsole("Test: Skipped ping testing...");
                return;
            }

            var samples = new long[count];

            Portal.ToConsole("Ping: {0:#,0} requests...", samples.Length);

            var time = 0L;

            var watch = new Stopwatch();

            for (var i = 0; i < samples.Length; i++)
            {
                watch.Start();

                Portal.ToConsole("Ping: ...");

                var result = c.Ping(false);

                watch.Stop();

                samples[i] = watch.ElapsedMilliseconds;

                watch.Reset();

                time += samples[i];

                Portal.ToConsole("Pong: {0:#,0}ms", samples[i]);

                if (!result)
                {
                    break;
                }
            }

            Portal.ToConsole("Completed: T:{0:#,0}ms A:{1:#,0}ms", time, time / samples.Length);

            PingTest(c);
        }
Exemplo n.º 26
0
 public static void InvokeDisposed(PortalClient client)
 {
     if (client != null && (client.IsDisposing || client.IsDisposed) && OnDisposed != null)
     {
         OnDisposed(client);
     }
 }