Ejemplo n.º 1
0
 private void onClientDisconnected(IWebSocketConnection socket, Gateway client)
 {
     if (client == null)
     {
         _unregisteredConnections.Remove(socket);
     }
     else
     {
         DisconnectClient(client);
     }
 }
Ejemplo n.º 2
0
        private void onClientMessage(IWebSocketConnection socket, byte[] message, Gateway gateway)
        {
            Utils.Log("Incoming binary message:{0}", BitConverter.ToString(message).Replace("-", ""));
            try
            {
                Packet packet = Packet.FromBytes(message);
                Utils.Log("{0}", packet);

                if (gateway == null)
                {
                    RegisterGateway(packet, socket);
                }
                else
                {
                    gateway.HandleMessage(packet);
                }
            }
            catch (BadPacketException)
            {
                Utils.Log("Failed to parse incoming packet");
            }
        }
Ejemplo n.º 3
0
 private void onClientMessage(IWebSocketConnection socket, string message, Gateway gateway)
 {
     Utils.Log("Incoming text message:{0}", message);
     try
     {
         byte[] data = Convert.FromBase64String(message);
         onClientMessage(socket, data, gateway);
     }
     catch (FormatException)
     {
         Utils.Log("Base64 string decoding failed:{0}", message);
     }
 }
Ejemplo n.º 4
0
 private void DisconnectClient(Gateway c)
 {
     c.Close();
     _allClients.Remove(c.Id);
 }
Ejemplo n.º 5
0
        private void RegisterGateway(Packet packet, IWebSocketConnection socket)
        {
            if (packet.Type != PacketType.Register)
            {
                Utils.Log("Unexpected packet:{0}", packet);
                return;
            }

            Gateway c = null;
            RegisterPacket rp = packet as RegisterPacket;

            if (!_allClients.TryGetValue(rp.GatewayId, out c))
            {
                c = new Gateway(rp.GatewayId, rp.GatewayAddr, rp.NetworkKey, socket);
                _allClients.Add(rp.GatewayId, c);
            }
            else
            {
                c.SetConnection(socket);
            }

            socket.OnMessage = (message) =>
            {
                onClientMessage(socket, message, c);
            };

            socket.OnBinary = (message) =>
            {
                onClientMessage(socket, message, c);
            };

            socket.OnClose = () =>
            {
                onClientDisconnected(socket, c);
            };

            c.HandleMessage(packet);
        }