private Packet DoRecieve(MQTTClient mqttClient, IEnumerable<byte> dataBytes, out bool disconnect) { // declare hub var hubContext = GlobalHost.ConnectionManager.GetHubContext<BrokerHub>(); List<byte> byteList = dataBytes.ToList(); MQTTPacketTypes mqttPacketType = (MQTTPacketTypes)byteList.First(); disconnect = false; string data = string.Empty; Packet responsePacket = null; Console.ForegroundColor = ConsoleColor.Red; switch (mqttPacketType) { // types we should recieve case MQTTPacketTypes.PUBLISH: Console.WriteLine("PUBLISH recieved"); var pubPacket = new Publish(byteList); PublishToAllClients(pubPacket.TopicName, pubPacket.Payload); data = string.Format("Topic: {0} Data: {1}", pubPacket.TopicName, pubPacket.Payload); // responsePacket = new Puback(); break; case MQTTPacketTypes.DISCONNECT: Console.WriteLine("DISCONNECT recieved"); disconnect = true; // handle at the higher level MQTTClient temp; if (!ClientCollection.TryRemove(mqttClient.UniqueId, out temp)) { //Trace.WriteLine("Error occurred removing client from client collection"); } hubContext.Clients.All.clientDisconnected(mqttClient.UniqueId); break; case MQTTPacketTypes.PINGREQ: Console.WriteLine("PINGREQ recieved"); var pingRespPacket = new Pingresp(); responsePacket = pingRespPacket; break; case MQTTPacketTypes.UNSUBSCRIBE: Console.WriteLine("UNSUBSCRIBE recieved"); var unsubPacket = new Unsubscribe((List<byte>)dataBytes); string summaryString = string.Empty; foreach (var topic in unsubPacket.TopicNames) { hubContext.Clients.All.removeSubscription(mqttClient.UniqueId, topic); summaryString += topic + " "; } data = string.Format("Topics: {0}",summaryString); // remove all subscriptions in unsubscribe mqttClient.SubscriptionList = mqttClient.SubscriptionList.Except(unsubPacket.TopicNames).ToList(); var unsubackPacket = new Unsuback(unsubPacket.PacketId); responsePacket = unsubackPacket; break; case MQTTPacketTypes.SUBSCRIBE: Console.WriteLine("SUBSCRIBE recieved"); var subPacket = new Subscribe((List<byte>)dataBytes); List<int> subSuccess = new List<int>(); foreach (var item in subPacket.subList) { if(!mqttClient.SubscriptionList.Contains(item.Name)) { mqttClient.SubscriptionList.Add(item.Name); subSuccess.Add(1); } else subSuccess.Add(0); } string summaryString2 = string.Empty; foreach (var topic in subPacket.subList) { hubContext.Clients.All.addSubscription(mqttClient.UniqueId, topic.Name); summaryString2 += topic.Name + " "; } data = string.Format("Topics: {0}",summaryString2); var subAckPacket = new Suback(subPacket.PacketId, subSuccess.ToArray()); responsePacket = subAckPacket; break; case MQTTPacketTypes.CONNECT: Console.WriteLine("CONNECT recieved"); var connectPacket = new Connect((List<byte>)dataBytes); mqttClient.ClientID = connectPacket.ClientId; ClientCollection.GetOrAdd(clientCounter, mqttClient); hubContext.Clients.All.newClientConnected(mqttClient); responsePacket = new Connack(); break; // types we should NOT recieve case MQTTPacketTypes.CONNACK: Console.WriteLine("CONNACK recieved"); break; case MQTTPacketTypes.SUBACK: Console.WriteLine("SUBACK recieved"); break; case MQTTPacketTypes.PINGRESP: Console.WriteLine("PINGRESP recieved"); break; case MQTTPacketTypes.UNSUBACK: Console.WriteLine("UNSUBACK recieved"); break; default: Console.WriteLine("Unknown packet recieved"); break; } Console.ResetColor(); hubContext.Clients.All.newPacketRecieved(true, mqttClient.UniqueId, Enum.GetName(typeof(MQTTPacketTypes), mqttPacketType), data, DateTime.Now.ToLongTimeString()); return responsePacket; }
private void DoRecieve(List<byte> dataBytes) { MQTTPacketTypes mqttPacketType = (MQTTPacketTypes)dataBytes.First(); string payload = string.Empty; string topic = string.Empty; int? packetId = null; switch (mqttPacketType) { case MQTTPacketTypes.CONNACK: Connected = true; payload = "Connection acknowledged"; break; case MQTTPacketTypes.PUBLISH: var pubPacket = new Publish(dataBytes); payload = pubPacket.Payload; topic = pubPacket.TopicName; break; case MQTTPacketTypes.PUBACK: payload = "Publish acknowledged"; break; case MQTTPacketTypes.SUBACK: var subackPacket = new Suback(dataBytes); packetId = subackPacket.PacketId; payload = "Subscription acknowledged"; break; case MQTTPacketTypes.UNSUBACK: var unsubackPacket = new Unsuback(dataBytes); packetId = unsubackPacket.PacketId; payload = "Unsubscription acknowledged"; break; case MQTTPacketTypes.PINGRESP: payload = "Ping response."; break; // other packet types that our clients shouldn't recieve case MQTTPacketTypes.DISCONNECT: case MQTTPacketTypes.PINGREQ: case MQTTPacketTypes.UNSUBSCRIBE: case MQTTPacketTypes.SUBSCRIBE: case MQTTPacketTypes.CONNECT: default: payload = "Invalid receive or unknown packet"; break; } AddListViewItem(true, mqttPacketType, packetId, topic, payload, DateTime.Now); }