예제 #1
0
        public void RemoveClient(HandleClient clientSocket)
        {
            HandleClient rmclient;

            if (clientSocket != null)
            {
                if (ClientList.ContainsKey(clientSocket))
                {
                    clientSocket.DisConnectTime = DateTime.Now;
                    ClientList.TryRemove(clientSocket, out rmclient);
                }

                if (DelClientConnectCheckHandler != null)
                {
                    DelClientConnectCheckHandler.Invoke(_serverPort, clientSocket.ClientNumber, false);
                }

                share.EventLog(clientSocket.ClientNumber, "[Disconnected]", SocketName);
            }
        }
예제 #2
0
        private void InitSocket()
        {
            //server = new TcpListener(new IPEndPoint(IPAddress.Any, _serverPort));
            ////client = default(TcpClient);
            //server.Start();

            while (!shutdown)
            {
                try
                {
                    var Clients = server.AcceptTcpClientAsync();
                    Clients.Wait(cts.Token);
                    var AcceptClient = Clients.Result;

                    h_client = new HandleClient(AcceptClient);
                    //client = server.AcceptTcpClient();

                    if (_serverPort == 1010)
                    {
                        SocketName = "Common Socket";
                    }
                    else if (_serverPort == 1011)
                    {
                        SocketName = "Voltage Read Socket";
                    }

                    if (!ClientList.TryAdd(h_client, h_client))
                    {
                        share.EventLog(h_client.ClientNumber, "[Connect Fail]", SocketName);

                        throw new InvalidOperationException("Tried to add connection twice");
                    }
                    else
                    {
                        share.EventLog(h_client.ClientNumber, "[Connected]", SocketName);

                        if (DelClientConnectCheckHandler != null)
                        {
                            DelClientConnectCheckHandler.Invoke(_serverPort, h_client.ClientNumber, true);
                        }
                    }

                    //h_client = new HandleClient(client);
                    //h_client.OnReceived += new HandleClient.MessageDisplayHandler(DataReceived);    //  1 Client / 1 Thread
                    //h_client.OnDisconnected += new HandleClient.DisconnectedHandler(RemoveClient);
                    //h_client.StartHandle();
                }
                catch (SocketException se)
                {
                    //share.EventLog(string.Format("{0} (0) > {1}", MethodBase.GetCurrentMethod().Name, se.Message), false);
                    share.EventLog(null, string.Empty, string.Format("\t{0} (0) > {1}", MethodBase.GetCurrentMethod().Name, se.Message), false);

                    RemoveClient(h_client);
                }
                catch (Exception ex)
                {
                    //share.EventLog(string.Format("{0} (1) > {1}", MethodBase.GetCurrentMethod().Name, ex.Message), false);
                    share.EventLog(null, string.Empty, string.Format("\t{0} (1) > {1}", MethodBase.GetCurrentMethod().Name, ex.Message), false);

                    RemoveClient(h_client);
                }
            }
        }