Пример #1
0
        public static PluginMessage Parse(TimestampedBytes rawMessage)
        {
            var rawData = rawMessage.RawData;

            if (rawData == null)
            {
                throw new ArgumentNullException();
            }

            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 of not implemented type: " + rawData[0]);
            }

            var 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("Can’t parse the message properly: " + newMsg.GetType().Name);
                    }
                    newMsg.Deserialize(br);
                }

            return(newMsg);
        }
Пример #2
0
 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);
     }
 }
Пример #3
0
        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);
            }
        }
Пример #4
0
 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);
             }
         }
     }
 }