コード例 #1
0
        /// <summary>
        /// A client has connected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnClientConnected(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
        {
            if (sender == null)
            {
                throw new ArgumentNullException("sender");
            }
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            Debug.WriteLine("Connection from {0}:{1} to {2}:{3} was established",
                            args.Socket.Information.RemoteHostName.DisplayName,
                            args.Socket.Information.RemotePort,
                            args.Socket.Information.LocalAddress.DisplayName,
                            args.Socket.Information.LocalPort);

            var clientKey = Guid.NewGuid();

            ActiveClients.Add(clientKey, args.Socket);

            ClientReader(args.Socket);

            args.Socket.Dispose();
            ActiveClients.Remove(clientKey);

            Debug.WriteLine("Connection from {0}:{1} to {2}:{3} was disconnected",
                            args.Socket.Information.RemoteHostName.DisplayName,
                            args.Socket.Information.RemotePort,
                            args.Socket.Information.LocalAddress.DisplayName,
                            args.Socket.Information.LocalPort);
        }
コード例 #2
0
 public static void ReleaseClient(int user, int pipe_id)
 {
     if (ActiveClients.TryGetValue(user, out var c))
     {
         Server.ReleaseClient(pipe_id, c.Id);
         ActiveClients.Remove(user);
     }
 }
コード例 #3
0
            public void RemoveUser(Client client)
            {
                var c = ActiveClients.FirstOrDefault(x => x.GridID == client.GridID | x.ID == client.ID);

                if (c != default(Client))
                {
                    ActiveClients.Remove(c);
                }
            }
コード例 #4
0
        public void Release()
        {
            Disconnect();

            lock (active_client_lock)
            {
                ActiveClients.Remove(Id);
            }
        }
コード例 #5
0
 public override void RemoveClient(IActiveClient client)
 {
     lock (ActiveClients)
     {
         if (ActiveClients.Any(c => c.ClientGuid == client.ClientGuid))
         {
             ActiveClients.Remove(client);
         }
     }
 }
コード例 #6
0
ファイル: SocketClient.cs プロジェクト: Nick-W/PorTee
        private void ClientHandler(Object client)
        {
            TcpClient User = (TcpClient)client;

            ActiveClients.Add(User);

            //Main Receiver
            while (_active)
            {
                byte[] dataSegment = new byte[Program.BufferSize];
                int    bytesRead   = 0;
                byte[] ReceivedData;
                try
                {
                    //Blocks until data is received
                    bytesRead    = User.GetStream().Read(dataSegment, 0, Program.BufferSize);
                    ReceivedData = new byte[bytesRead];
                    Buffer.BlockCopy(dataSegment, 0, ReceivedData, 0, bytesRead);
                }

                catch //Socket Error
                {
                    Console.WriteLine($"Client Disconnected (Socket Read Error): {User.Client.RemoteEndPoint}");
                    break;
                }
                if (!_active)
                {
                    break;
                }

                if (bytesRead == 0) //Disconnect
                {
                    Console.WriteLine($"Client Disconnected: {User.Client.RemoteEndPoint}");
                    break;
                }

                if (ReceivedDataHandler != null)
                {
                    ReceivedDataHandler(ReceivedData, User);
                }
                else
                {
                    Console.WriteLine("Data received, but no method has registered to handle it");
                }
            }
            //Close client TCP stream
            ActiveClients.Remove(User);
            User.Close();
            ConnectToServer();
        }
コード例 #7
0
ファイル: SocketServer.cs プロジェクト: Nick-W/PorTee
        private void ClientHandler(Object client)
        {
            var user = (TcpClient)client;

            ActiveClients.Add(user);

            //Main Receiver
            while (_active)
            {
                var dataSegment = new byte[Program.BufferSize];

                try
                {
                    //Blocks until data is received
                    var bytesRead = user.GetStream().Read(dataSegment, 0, Program.BufferSize);
                    if (bytesRead == 0) //Disconnected
                    {
                        Console.WriteLine($"Client Disconnected: {user.Client.RemoteEndPoint}");
                        break;
                    }
                    //Resize the dataSegment to the actual packet length
                    Array.Resize(ref dataSegment, bytesRead);
                    //Dispatch the incoming packet
                    if (ReceivedDataHandler != null)
                    {
                        ReceivedDataHandler(dataSegment, user);
                    }
                    else
                    {
                        Console.WriteLine($"Data received, but no method has registered to handle it");
                    }
                }
                //Socket Error
                catch
                {
                    Console.WriteLine($"Client Disconnected (Socket Read Error): {user.Client.RemoteEndPoint}");
                    break;
                }
                if (!_active)
                {
                    break;
                }
            }
            //Close client TCP stream
            ActiveClients.Remove(user);
            user.Close();
        }
コード例 #8
0
 protected void ConnectionCheck(object sender, ElapsedEventArgs e)
 {
     lock (ActiveClients)
     {
         var checkTime = DateTime.Now;
         for (var i = 0; i < ActiveClients.Count; i++)
         {
             var activeClient = ActiveClients[i];
             if (checkTime - activeClient.LastAliveDateTime <= DisconnectInterval)
             {
                 continue;
             }
             var client = (SocketActiveClient)activeClient;
             client.Close();
             ActiveClients.Remove(activeClient);
         }
     }
 }
コード例 #9
0
        /// <summary>
        /// A message was recived
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnMessageReceived(object sender, MessageReceivedEventArgs args)
        {
            foreach (var client in ActiveClients.ToList())
            {
                var results = SendMessage(client.Value, args.Message);

                if (results == false)
                {
                    Debug.WriteLine("Connection from {0}:{1} to {2}:{3} was disconnected",
                                    client.Value.Information.RemoteHostName.DisplayName,
                                    client.Value.Information.RemotePort,
                                    client.Value.Information.LocalAddress.DisplayName,
                                    client.Value.Information.LocalPort);

                    client.Value.Dispose();

                    ActiveClients.Remove(client.Key);
                }
            }
        }