示例#1
0
        private static void CleanupExit()
        {
            Listener.Dispose();

            foreach (var ClientCommunication in CommunicationDictionary)
            {
                try
                {
                    ClientCommunication.Key.Shutdown(SocketShutdown.Both);
                    ClientCommunication.Key.Close();
                }
                catch (Exception ex)
                {
                    Log.ErrorEvent.Write(ex.Message);
                }
            }

            SocketCloseSignal.Set();
            ConnectionEvent.Dispose();

            Debug.WriteLine("DR module stopped successfully.");
        }
示例#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");
        }