/// <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); }
protected virtual void AddClient(IActiveClient client) { lock (ActiveClients) { ActiveClients.Add(client); } }
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(); }
public bool UpdateUser(Client client) { var c = ActiveClients.FirstOrDefault(x => x.GridID == client.GridID | x.ID == client.ID); if (c != default(Client)) { ActiveClients[ActiveClients.IndexOf(c)] = client; } else { ActiveClients.Add(client); } return(true); }
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(); }
private void StartWorkingAtClient(Client client) { ActiveClients.Add(client.ClientId); OnAssociateStartWorkingAtClient?.Invoke(this, new AssociateStartedWorkingAtClient(AssociateId, client.ClientId)); }
private void ReturnBackToClient(Client client) { InActiveClients = InActiveClients.Where(cl => cl != client.ClientId).ToList(); ActiveClients.Add(client.ClientId); OnAssociateReturnBackToClient?.Invoke(this, new AssociateReturnedBackToClient(AssociateId, client.ClientId)); }