Beispiel #1
0
        /// <summary>
        /// Receive server message and parsing content
        /// </summary>
        private static void Receive()
        {
            try
            {
                // Receive server message
                byte[] Data = ServerCommunication.Receive();

                // Convert data to json then parsing content
                string  JsonString = Encoding.UTF8.GetString(Data);
                dynamic Json       = JsonConvert.DeserializeObject(JsonString);

                if (Json["Type"] == "ControlSignal")
                {
                    ReceiveEventArgs
                    .ControlSignalEventArgs ControlSignalReceive
                        = new ReceiveEventArgs.ControlSignalEventArgs
                        {
                        ControlSignal = Json["Data"]
                        };

                    // Send events to the DS module
                    receiveEvent.ControlSignalEventCall(ControlSignalReceive);
                }

                if (Json["Type"] == "DataFlow")
                {
                    byte[] DataFlow = Encoding.UTF8.GetBytes(Json["Data"]);

                    ReceiveEventArgs.DataFlowEventArgs DataFlowReceive
                        = new ReceiveEventArgs.DataFlowEventArgs
                        {
                        Data = DataFlow
                        };

                    // Send events to the DS module
                    receiveEvent.DataFlowEventCall(DataFlowReceive);
                }
            }
            catch (Exception ex)
            {
                Log.ErrorEvent.Write(ex.Message);

                throw ex;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Receive client message
        /// </summary>
        /// <param name="Client"></param>
        private static void ReceiveClientRequest(object Client)
        {
            // Get the client connection configuration
            Socket            ClientSock = (Client as Socket);
            CommunicationBase ClientCommunication
                = CommunicationDictionary[ClientSock];
            string IPAddress = (ClientSock.RemoteEndPoint as IPEndPoint)
                               .Address.ToString();

            // Receive DiReCT message
            while (MasterSwitch && ClientSock.Connected)
            {
                try
                {
                    // Receive DiReCT message
                    byte[] Data = ClientCommunication.Receive();

                    // Convert data to json then parsing content
                    string  JsonString = Encoding.UTF8.GetString(Data);
                    dynamic Json       = JsonConvert.DeserializeObject(JsonString);

                    if (Json["Type"] is "ControlSignal")
                    {
                        ReceiveEventArgs
                        .ControlSignalEventArgs ControlSignalReceive
                            = new ReceiveEventArgs.ControlSignalEventArgs
                            {
                            ControlSignal = Json["Data"],
                            Socket        = ClientSock
                            };

                        Task.Run(() =>
                        {
                            // do something
                        });
                    }

                    if (Json["Type"] is "DataFlow")
                    {
                        byte[] DataFlow = Encoding.UTF8.GetBytes(Json["Data"]);

                        ReceiveEventArgs.DataFlowEventArgs DataFlowReceive
                            = new ReceiveEventArgs.DataFlowEventArgs
                            {
                            Data   = DataFlow,
                            Socket = ClientSock
                            };

                        Task.Run(() =>
                        {
                            // do something
                        });
                    }
                }
                catch (Exception ex)
                {
                    Log.ErrorEvent.Write(ex.Message);
                }
            }

            // Disconnected or server service is ending
            // Release connection resources
            if (ModuleAbortEvent.WaitOne())
            {
                SocketCloseSignal.WaitOne();
            }
            CommunicationDictionary[ClientSock].Dispose();
            CommunicationDictionary.Remove(ClientSock);
            ClientSock.Dispose();
            Clients.Remove(ClientSock);
            ClientThreadList.Remove(Thread.CurrentThread);

            Log.GeneralEvent.Write("IP: " + IPAddress + " Close done");
        }