/// <summary>
        /// Processes incoming UDP data
        /// </summary>
        /// <param name="result">Async result</param>
        private void ReceiveCallback(IAsyncResult result)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("Socket {0} ReceiveCallback", GetHashCode()));
            try
            {
                lock (Socket)
                {
                    if ((Socket != null) && (Socket.Client != null))
                    {
                        IPEndPoint messageSource = null;
                        System.Diagnostics.Trace.WriteLine(string.Format("{0} DiscoverySocket::ReceiveCallback entry point",
                                                                         DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss:fff")));
                        byte[] messageBytes = Socket.EndReceive(result, ref messageSource);

                        System.Diagnostics.Trace.WriteLine(string.Format("{0} DiscoverySocket::ReceiveCallback from[{1}] message[{2}]",
                                                                         DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss:fff"), messageSource.ToString(), Encoding.UTF8.GetString(messageBytes)));

                        DiscoverySocketEventArgs args = new DiscoverySocketEventArgs();
                        args.Message = messageBytes;
                        args.Source  = messageSource;
                        MessageReceived(this, args);
                        System.Diagnostics.Trace.WriteLine(string.Format("{0} DiscoverySocket::ReceiveCallback begin receive",
                                                                         DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss:fff")));
                        System.Diagnostics.Trace.Flush();
                        Socket.BeginReceive(ReceiveCallback, null);
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("{0} DiscoverySocket::ReceiveCallback error [{1}]",
                                                                 DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss:fff"), e.Message));
                System.Diagnostics.Trace.Flush();
            }
        }
Example #2
0
 /// <summary>
 /// Processed incoming UDP packet
 /// </summary>
 /// <typeparam name="T">Type of expected object</typeparam>
 /// <param name="sender">Discovery socket</param>
 /// <param name="e">Event arguments, containing endpoint address and incoming bytes</param>
 protected void OnMessageReceived <T>(object sender, DiscoverySocketEventArgs e)
     where T : class
 {
     if (_parseLater)
     {
         DiscoverySocketEventArgs newPacket = new DiscoverySocketEventArgs();
         newPacket.Source  = new IPEndPoint(e.Source.Address, e.Source.Port);
         newPacket.Message = new byte[e.Message.Count()];
         e.Message.CopyTo(newPacket.Message, 0);
         _packetsReceived.Add(newPacket);
     }
     else
     {
         ProcessIncomingPacket <T>(e, () => { _stopListenEvent.Set(); });
     }
 }
Example #3
0
 /// <summary>
 /// Parse UDP packet
 /// </summary>
 /// <typeparam name="T">Type of expected object</typeparam>
 /// <param name="e">Event arguments, containing endpoint address and incoming bytes</param>
 /// <param name="action">Post-processing action</param>
 protected void ProcessIncomingPacket <T>(DiscoverySocketEventArgs e, Action action)
     where T : class
 {
     if ((_listenAddress == null) || DiscoveryUtils.CompareAddresses(e.Source.Address, _listenAddress))
     {
         try
         {
             //try to parse message according to expected type
             SoapMessage <T> message = DiscoverySoapBuilder.ParseMessage <T>(e.Message, _discoverySchemas);
             if (IsExpectedMessage <T>(message))
             {
                 EventHandler <DiscoveryMessageEventArgs> handler = GetHandler(message.Object.GetType());
                 DiscoveryMessageEventArgs args = new DiscoveryMessageEventArgs(
                     message.ToSoapMessage <object>(),
                     e.Source.Address);
                 if (handler != null)
                 {
                     handler(this, args);
                 }
                 action();
             }
         }
         catch (SoapFaultException ex)
         {
             if (ex.Message != null)
             {
                 if (IsExpectedMessage <Fault>(ex.FaultMessage))
                 {
                     if (SoapFaultReceived != null)
                     {
                         SoapFaultReceived(this, new DiscoveryErrorEventArgs(ex, ex.Fault));
                     }
                     action();
                 }
             }
         }
         catch (UnxpectedElementException ex)
         {
             if ((_listenMessages.Count() > 0) && IsExpectedMessageHeader(ex.Headers))
             {
                 //throw this  exception only is message contains proper RelatedTo, otherwise just ignore message
                 if (ReceiveError != null)
                 {
                     ReceiveError(this, new DiscoveryErrorEventArgs(ex, null));
                 }
                 action();
                 throw;
             }
         }
         catch (Exception ex)
         {
             if (ReceiveError != null)
             {
                 ReceiveError(this, new DiscoveryErrorEventArgs(ex, null));
             }
             System.Diagnostics.Trace.WriteLine(string.Format("Discovery::OnMessageReceived error [{0}]",
                                                              ex.Message));
             System.Diagnostics.Trace.Flush();
         }
     }
 }