示例#1
0
        /// <summary>
        /// Listen for incomming messages
        /// </summary>
        protected void Listen()
        {
            using (Stream = GetStream()) {
                Writer = new TCPWriter(Stream);
                Reader = new TCPReader(Stream);

                if (Logging)
                {
                    Logger.Write("SUCCESS", "Connected to the server");
                }

                OnConnected?.Invoke();

                if (RequireHandshake)
                {
                    byte[] rand = new byte[10];
                    RandomGen.Random.NextBytes(rand);

                    TCPMessage init = new TCPMessage()
                    {
                        Code    = TCPMessageCode.Init,
                        Content = rand
                    };

                    if (Logging)
                    {
                        Logger.Write("INFO", "Sending handshake");
                    }

                    Send(init);
                }

                while (Running)
                {
                    TCPMessage message = Reader.Read(Socket);

                    if (message == null)
                    {
                        Running = false;
                        OnDisconnected?.Invoke();
                        continue;
                    }

                    if (message.Code == TCPMessageCode.Init)
                    {
                        if (Logging)
                        {
                            Logger.Write("SUCCESS", "Successful handshake");
                        }
                        OnHandshake?.Invoke();
                    }
                    else if (message.Code == TCPMessageCode.Message)
                    {
                        OnMessage?.Invoke(message);
                    }
                }
            }
        }
示例#2
0
        private async void Listen()
        {
            using (Stream ns = Stream) {
                WebSocketReader reader = new WebSocketReader();

                OnHandshake?.Invoke(this, new HandshakeEventArgs(null, null));

                while (Running && !ListenToken.IsCancellationRequested)
                {
                    WebSocketFrame frame = await reader.Read(ns, null);

                    if (frame == null || frame.Opcode == WebSocketOpcode.ConnectionCloseFrame)
                    {
                        Remove(WebSocketDisconnection.Disconnect);
                        break;
                    }

                    switch (frame.Opcode)
                    {
                    case WebSocketOpcode.PingFrame:

                        if (frame.Data.Length <= 125)
                        {
                            await Writer.WritePong(frame);

                            OnPing?.Invoke(this, new PingEventArgs(frame.Data));
                        }

                        break;

                    case WebSocketOpcode.PongFrame:

                        OnPong?.Invoke(this, new PongEventArgs(frame.Data));

                        break;

                    case WebSocketOpcode.BinaryFrame:

                        if (frame.Data.Length == 4 && frame.Data[0] == 22 &&
                            frame.Data[1] == 23 && frame.Data[2] == 24 && frame.Data[3] == 25)
                        {
                            await OnPingReceived();
                        }

                        OnMessage?.Invoke(this, new MessageEventArgs(null, frame));

                        break;

                    case WebSocketOpcode.TextFrame:

                        OnMessage?.Invoke(this, new MessageEventArgs(null, frame));

                        break;
                    }
                }
            }
        }
示例#3
0
        public SyncIOClient(TransportProtocol protocol, Packager packager)
        {
            Protocol         = protocol;
            _packager        = packager;
            _callbacks       = new CallbackManager <SyncIOClient>();
            _remoteFunctions = new RemoteFunctionManager();

            _callbacks.SetHandler <HandshakePacket>((c, p) =>
            {
                _handshakeComplete = p.Success;
                _connection.SetID(p.Id);
                _handshakeEvent?.Set();
                _handshakeEvent?.Dispose();
                _handshakeEvent = null;
                OnHandshake?.Invoke(this, ID, _handshakeComplete);
                _callbacks.RemoveHandler <HandshakePacket>();
            });

            _callbacks.SetHandler <RemoteCallResponse>((c, p) => _remoteFunctions.RaiseFunction(p));
        }
示例#4
0
        public SyncIOClient(TransportProtocal _protocal, Packager _packager)
        {
            Protocal        = _protocal;
            Packager        = _packager;
            Callbacks       = new CallbackManager <SyncIOClient>();
            RemoteFunctions = new RemoteFunctionManager();

            Callbacks.SetHandler <HandshakePacket>((c, p) => {
                HandshakeComplete = p.Success;
                Connection.SetID(p.ID);
                HandshakeEvent?.Set();
                HandshakeEvent?.Dispose();
                HandshakeEvent = null;
                OnHandshake?.Invoke(this, ID, HandshakeComplete);
                Callbacks.RemoveHandler <HandshakePacket>();
            });

            Callbacks.SetHandler <RemoteCallResponse>((c, p) => {
                RemoteFunctions.RaiseFunction(p);
            });
        }
        private async Task ListenClient(WebSocketUser user)
        {
            using (Stream ns = user.Stream) {
                WebSocketReader reader = new WebSocketReader();
                var             res    = await InterpretHeader(user, ns);

                if (!(res.Item1))
                {
                    RemoveClient(user, WebSocketDisconnection.NoHeader);
                    return;
                }

                Logger.DebugWrite("INFO", $"Socket successfully handshaked the update. UID: {user.UID}");
                OnHandshake?.Invoke(this, new HandshakeEventArgs(user, res.Item2));

                while (Running && !user.ListenToken.IsCancellationRequested)
                {
                    WebSocketFrame frame = await reader.Read(ns, user);

                    if (frame == null || frame.Opcode == WebSocketOpcode.ConnectionCloseFrame)
                    {
                        RemoveClient(user, WebSocketDisconnection.Disconnect);
                        break;
                    }

                    switch (frame.Opcode)
                    {
                    case WebSocketOpcode.PingFrame:

                        if (frame.Data.Length <= 125)
                        {
                            await user.Writer.WritePong(frame);

                            OnPing?.Invoke(this, new PingEventArgs(frame.Data));
                        }

                        break;

                    case WebSocketOpcode.PongFrame:

                        OnPong?.Invoke(this, new PongEventArgs(frame.Data));

                        break;

                    case WebSocketOpcode.BinaryFrame:

                        user.Meta.LastTime.Binary = DateTime.UtcNow;

                        if (RttEnabled && frame.Data.Length == 4 && frame.Data[0] == 26 &&
                            frame.Data[1] == 27 && frame.Data[2] == 28 && frame.Data[3] == 29)
                        {
                            OnPongReceived(user);
                        }

                        OnMessage?.Invoke(this, new MessageEventArgs(user, frame));

                        break;

                    case WebSocketOpcode.TextFrame:

                        user.Meta.LastTime.Text = DateTime.UtcNow;

                        OnMessage?.Invoke(this, new MessageEventArgs(user, frame));

                        break;
                    }
                }
            }
        }
示例#6
0
        /// <summary>
        /// Listen for new messages of individual clients
        /// </summary>
        /// <param name="client"></param>
        protected void ListenClient(TCPServerClient client)
        {
            if (Logging)
            {
                Logger.Write("REGION", "Method [ListenClient]");
            }

            using (Stream ns = GetStream(client)) {
                client.Stream = ns;

                client.Writer = new TCPWriter(ns);
                client.Reader = new TCPReader(ns);

                if (Logging)
                {
                    Logger.Write("INFO", "Created stream, writer and reader for client: " + client.UID);
                }

                lock (ClientsList) ClientsList.Add(client);
                lock (ClientsDict) ClientsDict.Add(client.UID, client);
                OnConnected?.Invoke(client);

                if (RequireHandshake)
                {
                    TCPMessage message = client.Reader.Read(client);

                    if (message == null || message.Code != TCPMessageCode.Init ||
                        message.Content.Length > 10)
                    {
                        RemoveClient(client, TCPDisconnectType.NoHandshake);
                        return;
                    }

                    if (Logging)
                    {
                        Logger.Write("SUCCESS", "Handshake: " + client.UID);
                    }

                    client.DoneHandshake = true;

                    client.Send(new TCPMessage()
                    {
                        Code    = TCPMessageCode.Init,
                        Content = new byte[] { 0, 1, 0 }
                    });

                    OnHandshake?.Invoke(client);
                }

                while (Running && ClientsDict.ContainsKey(client.UID))
                {
                    TCPMessage message = client.Reader.Read(client);

                    if (message == null)
                    {
                        RemoveClient(client, TCPDisconnectType.Timeout);
                        return;
                    }

                    if (Logging)
                    {
                        Logger.Write("INFO", "New message " + Enum.GetName(typeof(TCPMessageCode), message.Code) + " from user: " + client.UID);
                    }

                    if (message.Code == TCPMessageCode.Close)
                    {
                        RemoveClient(client, TCPDisconnectType.Disconnect);
                    }

                    if (message.Code == TCPMessageCode.Pong)
                    {
                        HandlePong(message);
                        continue;
                    }

                    if (message.Code == TCPMessageCode.Message)
                    {
                        OnMessage?.Invoke(client, message);
                    }
                }
            }
        }