private void onClientDisconnected(IWebSocketConnection socket, Gateway client) { if (client == null) { _unregisteredConnections.Remove(socket); } else { DisconnectClient(client); } }
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"); } }
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); } }
private void DisconnectClient(Gateway c) { c.Close(); _allClients.Remove(c.Id); }
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); }