public bool Send(IFreeformEntity_Msg request)
        {
            using (ILogMethod method = Log.LogMethod(LOGGER, this.DYN_MODULE_NAME, "Send"))
            {
                bool result = default(bool);

                try
                {
                    if (request == null)
                    {
                        method.Info("Unable to send message. request is empty");
                        return false;
                    }

                    using (UdpFreeformEntity entity = new UdpFreeformEntity())
                    {
                        entity.Address = request.IpAddress2;
                        entity.EntityData = request;
                        result = this.Send(entity);
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return result;
            }
        }
        /// <summary>
        /// Creates the raw freeform buffer from the given udp entity.
        /// </summary>
        /// <param name="direction">The direction.</param>
        /// <param name="udpEntity">The UDP entity.</param>
        /// <returns>Raw freeform buffer.</returns>
        public static byte[] CreateBuffer(FF_FlowDirection direction, UdpFreeformEntity udpEntity)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "CreateEntity"))
            {
                byte[] result = default(byte[]);

                try
                {
                    // get the gmu factory for the given gmu ip adddress
                    _GMU_Factory gmuFactory = GetGMUFactory(udpEntity.AddressString);
                    if (gmuFactory != null)
                    {
                        if (udpEntity.EntityData != null)
                        {
                            result            = gmuFactory[direction].CreateBuffer(udpEntity.EntityData);
                            udpEntity.RawData = result;
                        }
                        else if (udpEntity.RawData != null)
                        {
                            result = udpEntity.RawData;
                        }
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return(result);
            }
        }
 void OnTransceiver_Receive(UdpFreeformEntity item)
 {
     using (ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, "ff_trans_Receive"))
     {
         Log.Info(PROC, "Processing G2H Message ...");
         ExCommsExecutorFactory.ProcessMessage(item.EntityData as FFMsg_G2H);
     }
 }
        public bool Send(UdpFreeformEntity request)
        {
            using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "Send"))
            {
                bool result = default(bool);

                try
                {
                    if (request == null)
                    {
                        method.Info("Unable to send message (request is empty).");
                        return false;
                    }

                    byte[] rawData = FreeformEntityFactory.CreateBuffer(FF_FlowDirection.G2H, request);
                    if (rawData == null ||
                        rawData.Length == 0)
                    {
                        method.Info("Unable to send message (rawData was null or zero length).");
                        return false;
                    }

                    using (UdpSocketSendData sendData = new UdpSocketSendData()
                    {
                        IPAddress = Extensions.GetIpAddress(-1),
                        PortNo = _configStore.ReceivePortNo,
                        Buffer = rawData,
                    })
                    {
                        result = _sockTransmitter.Transmit(sendData);

                        if (this.AfterSendUdpEntityData != null)
                        {
                            byte[] rawDataWithoutIp = new byte[rawData.Length - 4];
                            Buffer.BlockCopy(rawData, 4, rawDataWithoutIp, 0, rawDataWithoutIp.Length);
                            request.RawData = rawDataWithoutIp;
                            request.ProcessDate = DateTime.Now;
                            this.AfterSendUdpEntityData(request);
                        }
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return result;
            }
        }
        public bool Send(UdpFreeformEntity entity)
        {
            bool result = false;

            try
            {
                byte[] buffer = FreeformEntityFactory.CreateBuffer(FF_FlowDirection.G2H, entity.EntityData);
                _udpClient.Send(buffer, buffer.Length);
                result = true;
            }
            catch (Exception ex)
            {
                ExceptionManager.Publish(ex);
            }

            return result;
        }
        public static T CreateEntity <T>(FF_FlowDirection direction, byte[] buffer, string givenAddress)
            where T : IFreeformEntity_Msg
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "CreateEntity"))
            {
                IFreeformEntity result = default(IFreeformEntity);

                try
                {
                    UdpFreeformEntity udpEntity = CreateUdpEntity(direction, buffer, givenAddress);
                    if (udpEntity != null)
                    {
                        result = CreateEntity(direction, udpEntity);
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return((T)result);
            }
        }
        /// <summary>
        /// Creates the freeform entity from given buffer.
        /// </summary>
        /// <param name="direction">The direction.</param>
        /// <param name="udpEntity">The UDP entity.</param>
        /// <returns>
        /// Freeform entity.
        /// </returns>
        public static IFreeformEntity CreateEntity(FF_FlowDirection direction, UdpFreeformEntity udpEntity)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "CreateEntity"))
            {
                IFreeformEntity result = default(IFreeformEntity);

                try
                {
                    // get the gmu factory for this ip address
                    _GMU_Factory gmuFactory = GetGMUFactory(udpEntity.AddressString);
                    if (gmuFactory != null &&
                        udpEntity.RawData != null)
                    {
                        result = gmuFactory[direction].CreateEntity(udpEntity.RawData, udpEntity.AddressString);
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return(result);
            }
        }
 public static void AddReceiverToQueue(UdpFreeformEntity udpFreeformEntity)
 {
     BlockingCollection<UdpFreeformEntity>
     itemUdpFreeformEntities.Enqueue(udpFreeformEntity);
 }
        public bool Send(UdpFreeformEntity udpEntity)
        {
            using (ILogMethod method = Log.LogMethod(LOGGER, this.DYN_MODULE_NAME, "Send"))
            {
                bool result = default(bool);

                try
                {
                    if (udpEntity == null)
                    {
                        method.Info("Unable to send message (request is empty).");
                        return false;
                    }

                    byte[] rawData = FreeformEntityFactory.CreateBuffer(FF_FlowDirection.H2G, udpEntity);
                    if (rawData == null ||
                        rawData.Length == 0)
                    {
                        method.Info("Unable to send message (rawData was null or zero length).");
                        return false;
                    }

                    if (_configStore.LogRawFreeformMessages)
                    {
                        udpEntity.ProcessDate = DateTime.Now;
                        string udpEntityString = udpEntity.ToStringRaw(FF_FlowDirection.H2G);
                        Log.Info(udpEntityString);
                        LOGGER.WriteLogInfo(udpEntityString + Environment.NewLine);
                        LOGGER_UDP_SEND.WriteLogInfo(udpEntity.ProcessDate.ToStringSafe());
                        LOGGER_UDP_SEND.WriteLogInfo(udpEntityString + Environment.NewLine);
                    }

                    using (UdpSocketSendData sendData = new UdpSocketSendData()
                    {
                        IPAddress = udpEntity.Address,
                        PortNo = _configStore.TransmitPortNo,
                        Buffer = rawData,
                    })
                    {
                        result = _sockTransmitter.Transmit(sendData);
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return result;
            }
        }
 protected abstract void OnReceiveFromSocket(UdpFreeformEntity udp);
 protected void RaiseReceiveEvent(UdpFreeformEntity udp)
 {
     using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "RaiseReceiveEvent"))
     {
         try
         {
             if (this.Receive != null)
             {
                 this.Receive(udp);
             }
         }
         catch (Exception ex)
         {
             method.Exception(ex);
         }
     }
 }
        /// <summary>
        /// Creates the raw freeform message from the given buffer.
        /// </summary>
        /// <param name="direction">The flow direction.</param>
        /// <param name="buffer">The given buffer.</param>
        /// <param name="givenAddress">The given address.</param>
        /// <returns>
        /// Raw freeform entity.
        /// </returns>
        public static UdpFreeformEntity CreateUdpEntity(FF_FlowDirection direction, byte[] buffer, string givenAddress)
        {
            using (ILogMethod method = Log.LogMethod("FreeformEntityFactory", "CreateUdpEntity"))
            {
                UdpFreeformEntity result = default(UdpFreeformEntity);

                try
                {
                    int minLength    = _headerLengths[(int)direction];
                    int hasIPAddress = _headerLengths[(int)direction];
                    int ipOffset     = 0;
                    int ipLength     = 0;

                    // invalid message
                    if (buffer == null ||
                        buffer.Length < minLength)
                    {
                        Log.Info("Parsing buffer (Failure) : Buffer was not in expected length.");
                        return(result);
                    }
                    method.Info("Parsing buffer (Success)");

                    // ip address
                    string    ipAddress2 = givenAddress;
                    IPAddress ipAddress  = null;
                    if (givenAddress.IsEmpty())
                    {
                        ipLength   = FreeformConstants.LEN_GMU_IPADDRESS;
                        ipAddress2 = string.Format("{0:D}.{1:D}.{2:D}.{3:D}",
                                                   buffer[0], buffer[1],
                                                   buffer[2], buffer[3]);
                        ipOffset += ipLength;
                    }

                    // parse the ip address
                    if (!IPAddress.TryParse(ipAddress2, out ipAddress))
                    {
                        method.Info("Parsing ip address (Failure) : Invalid ipaddress from the buffer.");
                        return(result);
                    }
                    method.Info("Parsing ip address (Success)");

                    // get the factory
                    int          dataLength = (buffer.Length - ipLength);
                    _GMU_Factory gmuFactory = GetGMUFactory(ipAddress2);

                    // raw data
                    byte[] dataWithoutIP = new byte[dataLength];
                    Buffer.BlockCopy(buffer, ipOffset, dataWithoutIP, 0, dataWithoutIP.Length);
                    result = new UdpFreeformEntity()
                    {
                        Address     = ipAddress,
                        RawData     = dataWithoutIP,
                        ProcessDate = DateTime.Now,
                    };
                    method.Info("Received ip address : " + ipAddress + ", Data Length : " + dataWithoutIP.Length);
                }
                catch (Exception ex)
                {
                    Log.Exception(method.PROC, ex);
                }

                return(result);
            }
        }
 protected override void OnReceiveFromSocket(UdpFreeformEntity udp)
 {
     _queue.Enqueue(udp);
 }
 protected void RaiseReceiveEvent(UdpFreeformEntity udp)
 {
     if (this.Receive != null)
     {
         this.Receive(udp);
     }
 }
 protected override void OnReceiveFromSocket(UdpFreeformEntity udp)
 {
     // put it into sqlite db
 }