public static PluginMessage Parse(TimestampedBytes rawMessage) { var rawData = rawMessage.RawData; if (rawData == null) throw new ArgumentNullException("rawData"); if (rawData.Length == 0) throw new ArgumentException("rawData is empty"); ACSProtocol.MessageType msgType; try { msgType = (ACSProtocol.MessageType)rawData[0]; } catch (Exception) { throw new Exception("Message contains unknown/not implemented Type-Byte '" + rawData[0] + "'"); } PluginMessage newMsg = CreateInstance(msgType); newMsg.CreationDate = rawMessage.IncomingDate; using (var m = new MemoryStream(rawData)) using (var br = new BinaryReader(m)) { if (br.ReadByte() != (byte)newMsg.Type) throw new Exception("Error in parsing the message, just because Minolin is dumb and you can't do anything about it. Hint: Message Type wasn't the one the Constrcutor of" + newMsg.GetType().Name + " receives"); newMsg.Deserialize(br); } return newMsg; }
public bool TrySend(TimestampedBytes dgram) { try { Send(dgram); return(true);// we don't really know if it worked } catch (Exception ex) { ErrorHandler(ex); return(false); } }
public void Send(TimestampedBytes dgram) { if (_plugin == null) { throw new Exception("TrySend: UdpClient missing, please open first"); } lock (_sendMessageQueue) { _sendMessageQueue.Enqueue(dgram); Monitor.Pulse(_sendMessageQueue); } }
public void FromBinary(TimestampedBytes data) { using (var m = new MemoryStream(data.RawData)) using (var br = new BinaryReader(m)) { var type = br.Read(); if ((byte)Type != type) throw new Exception("FromBinary() Type != type"); Deserialize(br); CreationDate = data.IncomingDate; } }
private void MessageReceived(TimestampedBytes data) { // The plugin did send us a Message, how nice :) // It will be raw data and needs to be decoded by the type (first byte) // Using the acPlugins4net library we can directly receive a PluginMessage var msg = AcMessageParser.Parse(data); AwesomeViewerStolenFromTheInternet.Log(msg); // if there is a Request, we'll have to send the corresponding answer. if(msg.Type == ACSProtocol.MessageType.ACSP_GET_CAR_INFO) { var request = msg as RequestCarInfo; var response = CarInfoConfiguration.GetMessage(request.CarId); UDPServer.TrySend(response.ToBinary()); } /* // Currently we only have two very simple messages, so we're do it here: try { using (var br = new BinaryReader(new MemoryStream(data))) { var msgType = (ACSProtocol.MessageType)br.ReadByte(); switch (msgType) { case ACSProtocol.MessageType.ACSP_GET_CAR_INFO: { // The plugin asks for the car_info of car_id X var requested_car_id = br.ReadByte(); // Then it expects a single answer var carInfoMsg = new MsgCarInfo() { CarId = requested_car_id, CarModel = "bmw_e300", CarSkin = "anthrazit", DriverName = "Minolin", DriverTeam = "Team", DriverGuid = "2468274569", IsConnected = true }; UDPServer.TrySend(carInfoMsg); } break; case ACSProtocol.MessageType.ACSP_REALTIMEPOS_INTERVAL: { } break; default: throw new Exception("Unknown/unexpected incoming message type '" + msgType + "'"); } } } catch(Exception ex) { System.Windows.MessageBox.Show(ex.Message); }*/ }
public static PluginMessage Parse(TimestampedBytes rawMessage) { var rawData = rawMessage.RawData; if (rawData == null) { throw new ArgumentNullException("rawData"); } if (rawData.Length == 0) { throw new ArgumentException("rawData is empty"); } ACSProtocol.MessageType msgType; try { msgType = (ACSProtocol.MessageType)rawData[0]; } catch (Exception) { throw new Exception("Message contains unknown/not implemented Type-Byte '" + rawData[0] + "'"); } PluginMessage newMsg = CreateInstance(msgType); newMsg.CreationDate = rawMessage.IncomingDate; using (var m = new MemoryStream(rawData)) using (var br = new BinaryReader(m)) { if (br.ReadByte() != (byte)newMsg.Type) { throw new Exception("Error in parsing the message, just because Minolin is dumb and you can't do anything about it. Hint: Message Type wasn't the one the Constrcutor of" + newMsg.GetType().Name + " receives"); } newMsg.Deserialize(br); } return(newMsg); }
private void ReceiveMessages() { while (Opened) { try { var bytesReceived = _plugin.Receive(ref RemoteIpEndPoint); var tsb = new TimestampedBytes(bytesReceived); lock (_messageQueue) { _messageQueue.Enqueue(tsb); Monitor.Pulse(_messageQueue); } } catch (Exception ex) { if (Opened) { // it seems the acServer is not running/ready yet ErrorHandler(ex); } } } }
public bool TrySend(TimestampedBytes dgram) { try { Send(dgram); return true;// we don't really know if it worked } catch (Exception ex) { ErrorHandler(ex); return false; } }
public void Send(TimestampedBytes dgram) { if (_plugin == null) throw new Exception("TrySend: UdpClient missing, please open first"); lock (_sendMessageQueue) { _sendMessageQueue.Enqueue(dgram); Monitor.Pulse(_sendMessageQueue); } }