public uint GetUIntValue(uint data) { if (_debug) { DBCommon.DebugP(ToDebugString() + " >> Data is : " + data); } lock (_lockObject) { return((data & Mask) >> Shiftvalue); } }
public void Shutdown() { try { try { _shutdown = true; DBCommon.DebugP("DCSBIOS is SHUTTING DOWN"); _dcsProtocolParser.Shutdown(); _dcsbiosListeningThread?.Abort(); } catch (Exception) { } try { _udpReceiveClient.Close(); } catch (Exception) { } try { _udpSendClient.Close(); } catch (Exception) { } /* * try * { * _tcpListener.Stop(); * } * catch (Exception) * { * } * try * { * _tcpClient.Close(); * } * catch (Exception) * { * } */ _started = false; } catch (Exception ex) { SetLastException(ex); DBCommon.LogError(9212, ex, "DCSBIOS.Shutdown()"); } }
private void ProcessArrays() { try { var interval = 0; DBCommon.DebugP("ProtocolParser starting processing loop"); while (!_shutdown) { try { if (interval >= 100) { //Debug.Print("_arraysToProcess.Count = " + _arraysToProcess.Count); interval = 0; } //lock (_lockArrayToProcess) //{ byte[] array = null; while (_arraysToProcess.TryDequeue(out array)) { if (array != null) { for (int i = 0; i < array.Length; i++) { ProcessByte(array[i]); } } } //} interval++; } catch (Exception e) { DBCommon.LogError(9243, e, "DCSBIOSProtocolParser.ProcessArrays(), arrays to process : " + _arraysToProcess.Count); } _autoResetEvent.WaitOne(); } DBCommon.DebugP("ProtocolParser exiting processing loop"); } catch (ThreadAbortException) { } catch (Exception e) { DBCommon.LogError(9244, e, "DCSBIOSProtocolParser.ProcessArrays(), arrays to process : " + _arraysToProcess.Count); } }
public void ReceiveDataUdp() { try { DBCommon.DebugP("DCSBIOS entering threaded receive data loop"); while (!_shutdown) { var byteData = _udpReceiveClient.Receive(ref _ipEndPointReceiverUdp); if ((_dcsBiosNotificationMode & DcsBiosNotificationMode.AddressValue) == DcsBiosNotificationMode.AddressValue) { _dcsProtocolParser.AddArray(byteData); } } DBCommon.DebugP("DCSBIOS exiting threaded receive data loop"); } catch (ThreadAbortException) { } catch (Exception e) { SetLastException(e); DBCommon.LogError(9213, e, "DCSBIOS.ReceiveData()"); } }
/* * private static byte[] GetBytes(string str) * { * var bytes = new byte[str.Length * sizeof(char)]; * Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); * return bytes; * } * * private static string GetString(byte[] bytes) * { * var chars = new char[bytes.Length / sizeof(char)]; * Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); * return new string(chars); * } */ private void SetLastException(Exception ex) { try { if (ex == null) { return; } DBCommon.LogError(666, ex, "Via DCSBIOS.SetLastException()"); var message = ex.GetType() + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace; if (DBCommon.Debug) { DBCommon.DebugP(message); } lock (_lockExceptionObject) { _lastException = new Exception(message); } } catch (Exception) { } }
public int SendDataFunction(string stringData) { var result = 0; lock (_lockObjectForSendingData) { try { //byte[] bytes = _iso8859_1.GetBytes(stringData); var unicodeBytes = Encoding.Unicode.GetBytes(stringData); var asciiBytes = new List <byte>(stringData.Length); asciiBytes.AddRange(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, unicodeBytes)); result = _udpSendClient.Send(asciiBytes.ToArray(), asciiBytes.ToArray().Length, _ipEndPointSenderUdp); //result = _udpSendClient.Send(bytes, bytes.Length, _ipEndPointSender); } catch (Exception e) { DBCommon.DebugP("Error sending data to DCS-BIOS. " + e.Message + Environment.NewLine + e.StackTrace); SetLastException(e); DBCommon.LogError(9216, e, "DCSBIOS.SendDataFunction()"); } } return(result); }
private void UpdateStrings(uint address, uint data) { //Common.DebugP("**********Received address 0x" + address.ToString("x") + " ****************"); lock (_lockObject) { if (address == 0x55) { //start of update cycle return; } if (address == 0xfffe) { //end of update cycle, clear all existing values. //broadcast every string now if (OnDCSBIOSStringReceived != null) { foreach (var kvp in _dcsBiosStrings) { //kvp.Value.Address == start address for the string if (kvp.Value.IsComplete) { OnDCSBIOSStringReceived(kvp.Value.Address, kvp.Value.StringValue); } } } } else { foreach (var kvp in _dcsBiosStrings) { if (kvp.Value.IsMatch(address)) { try { //Send "AB" //0x4241 //41 = A //42 = B var hex = Convert.ToString(data, 16); //Little Endian ! var secondByte = new[] { Convert.ToByte(hex.Substring(0, 2), 16) }; var firstByte = new[] { Convert.ToByte(hex.Substring(2, 2), 16) }; var firstChar = _iso8859_1.GetString(firstByte); var secondChar = _iso8859_1.GetString(secondByte); kvp.Value.Add(address, firstChar, secondChar); //Common.DebugP("**********Received (0x" + data.ToString("x") + ") ****************"); //Common.DebugP("**********Received data:(0x" + data.ToString("x") + ") following from DCS : 1st : " + firstChar + "(0x" + firstByte[0].ToString("x") + "), 2nd " + secondChar + "(0x" + secondByte[0].ToString("x") + ") ****************"); //kvp.Value.StepUp(); } catch (Exception ex) { DBCommon.DebugP("**********Received (0x" + data.ToString("x") + ") Exception = " + ex.Message + Environment.NewLine + ex.StackTrace); throw; } } } } } }
public void Startup() { try { /* * None, do as normal. */ /* * Client, try establish connection to the master FP on the network every two seconds. After connection listen to data from master and add that to the arraysToProcess queue. */ /* * Master, start listening TCP socket, for every client contacting via TCP add them as clients and send raw DCS-BIOS data to them */ if (_started) { return; } _shutdown = false; DBCommon.DebugP("DCSBIOS is STARTING UP"); _dcsProtocolParser = DCSBIOSProtocolParser.GetParser(); _dcsProtocolParser.Attach(_iDcsBiosDataListener); _ipEndPointReceiverUdp = new IPEndPoint(IPAddress.Any, ReceivePort); _ipEndPointSenderUdp = new IPEndPoint(IPAddress.Parse(SendToIp), SendPort); _udpReceiveClient = new UdpClient(); _udpReceiveClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); _udpReceiveClient.Client.Bind(_ipEndPointReceiverUdp); _udpReceiveClient.JoinMulticastGroup(IPAddress.Parse(ReceiveFromIp)); _udpSendClient = new UdpClient(); _udpSendClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); _udpSendClient.EnableBroadcast = true; _dcsbiosListeningThread?.Abort(); _dcsbiosListeningThread = new Thread(ReceiveDataUdp); _dcsbiosListeningThread.Start(); _dcsProtocolParser.Startup(); //_tcpListeningThread = new Thread(TCPListenerThread); //_tcpListeningThread.Start(); if (!_started) { _started = true; } } catch (Exception e) { SetLastException(e); DBCommon.LogError(9211, e, "DCSBIOS.Startup()"); if (_udpReceiveClient != null && _udpReceiveClient.Client.Connected) { _udpReceiveClient.Close(); _udpReceiveClient = null; } if (_udpSendClient != null && _udpSendClient.Client.Connected) { _udpSendClient.Close(); _udpSendClient = null; } /*if (_tcpListener != null) * { * _tcpListener.Stop(); * _tcpListener = null; * } * if (_tcpClient != null) * { * _tcpClient.Close(); * _tcpClient = null; * }*/ } }
public void Shutdown() { DBCommon.DebugP("ProtocolParser shutting down"); _shutdown = true; _autoResetEvent.Set(); }
public void Startup() { DBCommon.DebugP("ProtocolParser starting up"); _processingThread = new Thread(ProcessArrays); _processingThread.Start(); }
public static void LoadControls() { if (_airframe == DCSAirframe.NONE || _airframe == DCSAirframe.NOFRAMELOADEDYET) { return; } if (_dcsbiosControls.Count > 0 && !_airFrameChanged) { return; } _dcsbiosControls = new List <DCSBIOSControl>(); try { lock (_lockObject) { //Always read CommonData.json var directoryInfo = new DirectoryInfo(_jsonDirectory); IEnumerable <FileInfo> files; try { DBCommon.DebugP("Searching for " + _airframe.GetDescription() + ".json in directory " + _jsonDirectory); try { files = directoryInfo.EnumerateFiles(_airframe.GetDescription() + ".json", SearchOption.TopDirectoryOnly); } catch (Exception ex) { throw new Exception("Failed to find DCS-BIOS files. -> " + Environment.NewLine + ex.Message); } foreach (var file in files) { DBCommon.DebugP("Opening " + file.DirectoryName + "\\" + file.Name); var reader = file.OpenText(); string text; try { text = reader.ReadToEnd(); } finally { reader.Close(); } var jsonData = DCSBIOSJsonFormatterVersion1.Format(text); /*var newfile = File.CreateText(@"e:\temp\regexp_debug_output.txt.txt"); * newfile.Write(jsonData); * newfile.Close();*/ var dcsBiosControlList = JsonConvert.DeserializeObject <DCSBIOSControlRootObject>(jsonData); _dcsbiosControls.AddRange(dcsBiosControlList.DCSBIOSControls); } var commonDataText = File.ReadAllText(_jsonDirectory + "\\CommonData.json"); var commonDataControlsText = DCSBIOSJsonFormatterVersion1.Format(commonDataText); var commonDataControls = JsonConvert.DeserializeObject <DCSBIOSControlRootObject>(commonDataControlsText); _dcsbiosControls.AddRange(commonDataControls.DCSBIOSControls); var metaDataEndText = File.ReadAllText(_jsonDirectory + "\\MetadataEnd.json"); var metaDataEndControlsText = DCSBIOSJsonFormatterVersion1.Format(metaDataEndText); var metaDataEndControls = JsonConvert.DeserializeObject <DCSBIOSControlRootObject>(metaDataEndControlsText); _dcsbiosControls.AddRange(metaDataEndControls.DCSBIOSControls); _airFrameChanged = false; } finally { //nada } } } catch (Exception ex) { throw new Exception("LoadControls() : " + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace); } }