예제 #1
0
 private byte[] ConvertToBytes(ChoScalarObject obj)
 {
     if (_messageType == ChoUDPMessageType.Xml)
         return _encoding.GetBytes(ChoObject.XmlSerialize(new ChoScalarObject(obj)));
     else
         return ChoObject.Serialize(new ChoScalarObject(obj));
 }
예제 #2
0
        private void IdentifyMessageType(byte[] payload)
        {
            if (_identifiedMessageType)
            {
                return;
            }

            try
            {
                try
                {
                    ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.Deserialize(payload);
                    _messageType = ChoUDPMessageType.Binary;
                }
                catch
                {
                    try
                    {
                        ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.XmlDeserialize <ChoScalarObject>(_encoding.GetString(payload));
                        _messageType = ChoUDPMessageType.Xml;
                    }
                    catch
                    {
                        Trace.WriteLine("Failed to identity message type. Messages will be discarded.");
                    }
                }
            }
            finally
            {
                _identifiedMessageType = true;
            }
        }
예제 #3
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            IPEndPoint ep      = null;
            var        args    = (object[])ar.AsyncState;
            var        session = (UdpClient)args[0];
            var        local   = (IPEndPoint)args[1];

            try
            {
                Byte[] payload = session.EndReceive(ar, ref ep);

                EventHandler <ChoUDPMessageEventArgs> rawMessageReceived = RawMessageReceived;
                EventHandler <ChoUDPMessageEventArgs> messageReceived    = MessageReceived;
                if (rawMessageReceived != null)
                {
                    rawMessageReceived(this, new ChoUDPMessageEventArgs(null, payload));
                }
                else if (messageReceived != null)
                {
                    IdentifyMessageType(payload);
                    if (_messageType == null)
                    {
                        if (rawMessageReceived != null)
                        {
                            rawMessageReceived(this, new ChoUDPMessageEventArgs(null, payload));
                        }
                    }
                    else if (_messageType.Value == ChoUDPMessageType.Xml)
                    {
                        try
                        {
                            ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.XmlDeserialize <ChoScalarObject>(_encoding.GetString(payload));
                            MessageReceived(this, new ChoUDPMessageEventArgs(scalarObject, null));
                        }
                        catch (Exception ex)
                        {
                            ChoTrace.Error(ex.ToString());
                        }
                    }
                    else
                    {
                        try
                        {
                            ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.Deserialize(payload);
                            MessageReceived(this, new ChoUDPMessageEventArgs(scalarObject, null));
                        }
                        catch (Exception ex)
                        {
                            ChoTrace.Error(ex.ToString());
                        }
                    }
                }
            }
            finally
            {
                _recieveResult = session.BeginReceive(ReceiveCallback, args);
            }
        }
예제 #4
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                Byte[] payload = _listener.EndReceive(ar, ref _groupEP);

                EventHandler <ChoUDPMessageEventArgs> rawMessageReceived = RawMessageReceived;
                EventHandler <ChoUDPMessageEventArgs> messageReceived    = MessageReceived;
                if (rawMessageReceived != null)
                {
                    rawMessageReceived(this, new ChoUDPMessageEventArgs(null, payload));
                }
                else if (messageReceived != null)
                {
                    IdentifyMessageType(payload);
                    if (_messageType == null)
                    {
                        if (rawMessageReceived != null)
                        {
                            rawMessageReceived(this, new ChoUDPMessageEventArgs(null, payload));
                        }
                    }
                    else if (_messageType.Value == ChoUDPMessageType.Xml)
                    {
                        try
                        {
                            ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.XmlDeserialize <ChoScalarObject>(_encoding.GetString(payload));
                            MessageReceived(this, new ChoUDPMessageEventArgs(scalarObject, null));
                        }
                        catch (Exception ex)
                        {
                            ChoTrace.Error(ex.ToString());
                        }
                    }
                    else
                    {
                        try
                        {
                            ChoScalarObject scalarObject = (ChoScalarObject)ChoObject.Deserialize(payload);
                            MessageReceived(this, new ChoUDPMessageEventArgs(scalarObject, null));
                        }
                        catch (Exception ex)
                        {
                            ChoTrace.Error(ex.ToString());
                        }
                    }
                }
            }
            finally
            {
                _recieveResult = _listener.BeginReceive(ReceiveCallback, null);
            }
        }
예제 #5
0
 public ChoUDPMessageEventArgs(ChoScalarObject payload, byte[] rawPayload)
 {
     Payload    = payload;
     RawPayload = rawPayload;
 }