/// <summary> /// Call On start of service /// </summary> private void CallOnStart() { Packets = new Dictionary <int, Dictionary <int, Action <string> > >(); PacketCollection = new PacketCollection(); try { if (ConfigurationManager.AppSettings["ResetENGDBPassword"] != "") //if the reset engineering password needs to be reset { Encryptor.UpdateEngDBPassword(ConfigurationManager.AppSettings["ResetENGDBPassword"], true); //do so } if (ConfigurationManager.AppSettings["ResetCamstarPassword"] != "") //if the camstar password needs to be reset { Encryptor.UpdateCamstarPassword(ConfigurationManager.AppSettings["ResetCamstarPassword"], true); //do so } IsProd = ConfigurationManager.AppSettings["IsProd"] == "1"; if (IsProd) //if this is the prod Service setup the forward topic { DiagnosticItems.Enqueue(new DiagnosticItem("Prod version, all pacekts will be forwarded to " + ConfigurationManager.AppSettings["ForwardTopic"] + " Topic", 2)); ForwardPublisher = new TopicPublisher(ConfigurationManager.AppSettings["ForwardTopic"], Broker); //do so ThingsToDispose.Add(new Disposable(nameof(ForwardPublisher), ForwardPublisher)); } running = true; //stops diagnostic thread from dropping through until onstop is called. Task.Run(() => DiagnosticThread()); //start diagnostic thread. ( jsut loops displaying errors. SubTopicName = ConfigurationManager.AppSettings["MainTopicName"]; //load everything from the app settings Broker = ConfigurationManager.AppSettings["BrokerIP"]; ClientID = ConfigurationManager.AppSettings["ClientID"]; ConsumerID = ConfigurationManager.AppSettings["ConsumerID"]; ENG_DBDataSource = ConfigurationManager.AppSettings["ENGDBIP"]; ENG_DBUserID = ConfigurationManager.AppSettings["ENGDBUser"]; ENG_DBPassword = Encryptor.EncryptOrDecrypt(ConfigurationManager.AppSettings["ENGDBPassword"]); ENG_DBInitialCatalog = ConfigurationManager.AppSettings["ENGDBDatabase"]; LogggingLevel = Convert.ToInt32(ConfigurationManager.AppSettings["LogggingLevel"]); Listening = Convert.ToInt32(ConfigurationManager.AppSettings["Listening"]) == 1; Sending = Convert.ToInt32(ConfigurationManager.AppSettings["Sending"]) == 1; try { if (Convert.ToInt32(ConfigurationManager.AppSettings["UDPListeningEnabled"]) == 1) { UDPEnabled = true; UDPPort = Convert.ToInt32(ConfigurationManager.AppSettings["UDPPort"]); } } catch (Exception ex) { DiagnosticItems.Enqueue(new DiagnosticItem(ex.ToString(), 1)); } //setup the packet classes. ThingsToDispose = new List <Disposable>(); //reset list of objects that need to be disposed Task.Run(() => MQTTConnections()); //open all MQTT Connections Task.Run(() => SQLConnections()); //open alll SQL Connections Task.Run(() => TCPConnections()); //open all TCPConnections Task.Run(() => UDPConnections()); //open all UDP Connections } catch (Exception ex) //catch exceptions { DiagnosticItems.Enqueue(new DiagnosticItem(ex.ToString(), 1)); //logem } }
/// <summary> /// Logs a collection of raw packets /// </summary> /// <param name="packets"></param> public static void Log(PacketCollection packets) { if (packets == null) throw new ArgumentNullException("packets"); foreach (var item in packets) { Log(item); } }
/// <summary> /// Get a set of packets that are in the given buffer /// </summary> /// <param name="bytes">The buffer to use</param> /// <param name="start">The index from which to start reading</param> /// <param name="count">The total amount of bytes in the buffer</param> /// <returns>Returns a collection of packets and how many bytes it used in total</returns> public PacketCollection GetPackets(byte[] bytes, int start, int count) { var collection = new PacketCollection(); while (start < count) { if (TryGetPacket(bytes, start, count, out var used, out var packet)) { collection.Packets.Add(packet); start += used; }
public void GetDataTest() { List <byte[]> rawPackets = DataGenerator.GetInfoPackets(); InfoPacket packet1 = new InfoPacket(rawPackets[0]); InfoPacket packet2 = new InfoPacket(rawPackets[1]); PacketCollection packets = new PacketCollection(); packets.Add(packet1); packets.Add(packet2); byte[] result = packets.GetData(); Assert.IsNotNull(result); Assert.IsTrue(result.Length > 0); Assert.IsTrue(result.Length == packet1.GetData().Length + packet2.GetData().Length); }
public void GetDataStringTest() { List <byte[]> rawPackets = DataGenerator.GetInfoPackets(); InfoPacket packet1 = new InfoPacket(rawPackets[0]); InfoPacket packet2 = new InfoPacket(rawPackets[1]); PacketCollection packets = new PacketCollection(); packets.Add(packet1); packets.Add(packet2); string dataString1 = packet1.GetDataString(); string dataString2 = packet2.GetDataString(); string result = packets.GetDataString(); Assert.IsTrue(!String.IsNullOrWhiteSpace(result)); Assert.AreEqual <string>(packet1.GetDataString() + packet2.GetDataString(), result); }
public PacketCollection QueryServer(NetworkSettings nwSettings) { if (nwSettings == null) throw new ArgumentNullException("nwSettings"); PacketCollection packets = new PacketCollection(); using (UdpClient client = new UdpClient(nwSettings.LocalPort)) { byte[] request, response, timestamp = Helpers.GetTimeStamp(); IPEndPoint remoteIpEndpoint = new IPEndPoint(IPAddress.Parse(nwSettings.Host), nwSettings.RemotePort); client.Client.ReceiveTimeout = nwSettings.ReceiveTimeout; client.Connect(remoteIpEndpoint); request = GetChallengeRequest(timestamp); client.Send(request, request.Length); response = client.Receive(ref remoteIpEndpoint); ChallengePacket pChallenge = new ChallengePacket(response); request = GetInfosRequest(timestamp, pChallenge.GetChallenge()); client.Send(request, request.Length); while (true) { try { response = client.Receive(ref remoteIpEndpoint); packets.Add(new InfoPacket(response)); } catch (SocketException ex) { if (ex.ErrorCode == (int)SocketError.TimedOut) break; else throw; } } } return packets; }
public static void LogData(PacketCollection packets) { if (packets == null) throw new ArgumentNullException("packets"); string fileName = String.Format(CultureInfo.InvariantCulture, "DataStreams\\{0}.bin", GetFileNameHeader()); Log(fileName, packets.GetData()); }