public async Task ForceHandover(Predicate <Client> predicate) { foreach (var client in ConnectedClients.Where(predicate.Invoke)) { await clientsManager.SendControlMessageToClient(GetJson(new { Action = "Handover" }), client); } }
public async Task ToggleStateForClients(Predicate <Client> predicate) { foreach (var client in ConnectedClients.Where(predicate.Invoke)) { if (client.TestState.State != UserTestState.UserState.OnHold && client.TestState.State != UserTestState.UserState.Testing) { continue; } if (client.TestState.State == UserTestState.UserState.Testing) { await clientsManager.SendControlMessageToClient(GetJson(new { Action = "Pause" }), client); client.TestState.State = UserTestState.UserState.OnHold; } else if (client.TestState.State == UserTestState.UserState.OnHold) { await clientsManager.SendControlMessageToClient(GetJson(new { Action = "Resume" }), client); client.TestState.State = UserTestState.UserState.Testing; } ClientStatusUpdated?.Invoke(client); } }
public async Task SyncClient(Test.TestState state, Predicate <Client> clients) { foreach (var client in ConnectedClients.Where(clients.Invoke)) { await clientsManager.SendControlMessageToClient( GetJson(new { Action = "Sync", State = client.TestState }), client); } }
public ServerViewModel(IWCFHostService hostService, IUnitOfWork db, ISettingsService settingsService, IDialogService dialogService) { this.hostService = hostService; this.db = db; this.settingsService = settingsService; this.dialogService = dialogService; Client = new ChatWCFService.Client(settingsService.Name); Port = settingsService.Port; hostService.ClientConnected += (s, e) => { DispatcherHelper.CheckBeginInvokeOnUI(() => { ConnectedClients.Add(e.Client); Notifications.Add(new Notification($"Client {e.Client.Name} connected", DateTime.Now, NotificationType.ClientConnected)); }); }; hostService.ClientDisconneced += (s, e) => { DispatcherHelper.CheckBeginInvokeOnUI(() => { var client = ConnectedClients.Where(c => c.Name == e.Client.Name).FirstOrDefault(); ConnectedClients.Remove(client); Notifications.Add(new Notification($"Client {e.Client.Name} disconnected", DateTime.Now, NotificationType.ClientDisconnected)); }); }; hostService.MessageReceived += (s, e) => { DispatcherHelper.CheckBeginInvokeOnUI(() => { Messages?.Add(new Model.Message(e.Message)); }); }; DispatcherHelper.RunAsync(async() => { var messages = (await db.Messages.GetAll()).Select(m => new Model.Message(m)); foreach (var message in messages) { Messages.Add(message); } }); Messenger.Default.Register <NotificationMessage>(this, (m) => { switch (m.Notification) { case "ServerWindowClosed": StopServer(); break; } }); }
protected void DecodeMessage(ClientConnectionState state) { if (state.Buffer.Length < 2) { throw new InvalidOperationException("ERROR DECODE Buffer length is invalid"); } string msg = Encoding.ASCII.GetString(state.Buffer, 0, Math.Min(10, state.Buffer.Length)); IPAddress ip = (state.Socket.RemoteEndPoint as IPEndPoint).Address ?? null; /* Protocol reading section */ if (msg.StartsWith("LOGIN ")) // Connecting to server { msg = Encoding.ASCII.GetString(state.Buffer, 6, Math.Min(300, state.Buffer.Length)); string[] arguments = msg.Split(' '); if (arguments.Length != 4 || arguments[0] != "HOST" || arguments[2] != "PASS") { state.Buffer = null; state.Socket.Send(this.invalidMessage); } string host = arguments[1]; string pass = arguments[3]; if (pass.Length != 8 || pass != CurrentPassword) { state.Buffer = null; state.Socket.Send(Encoding.ASCII.GetBytes("INVALID PASS")); } CurrentPassword = GeneratePassword(); ClientConnectedEventArgs e = new ClientConnectedEventArgs(); e.Server = this; e.UtcTime = DateTime.UtcNow; state.Password = e.Password = pass; e.Hostname = host; state.ClientId = e.ClientId = Guid.NewGuid(); e.IP = ip; OnClientConnected?.Invoke(this, e); } else if (msg.StartsWith("FILE ADD\0")) // Start new file transfer { ReadonlyStream stream = new ReadonlyStream(state.Buffer); stream.Position = 9; using (BinaryReader reader = new BinaryReader(stream)) { try { Guid clientid = new Guid(reader.ReadBytes(16)); Client client = ConnectedClients.Where(item => item.Id == clientid && item.IP == ip).First(); if (client is null) { state.Buffer = null; state.Socket.Send(Encoding.ASCII.GetBytes("ABORTED")); } if (reader.ReadByte() != 0) { state.Buffer = null; state.Socket.Send(Encoding.ASCII.GetBytes("INVALID MESSAGE")); } string filename = string.Empty; char c; while ((c = reader.ReadChar()) != 0) { filename += c; } ulong size = reader.ReadUInt64(); TransferRequestEventArgs e = new TransferRequestEventArgs();; e.Client = client; e.Filename = filename; e.Id = Guid.NewGuid(); e.Server = this; e.Size = size; e.UtcTime = DateTime.UtcNow; OnTransferRequest?.Invoke(this, e); e.Server = this; if (!e.IsAccepted || e.IsCanceled) { } if (e.Stream is null || !e.Stream.CanWrite) { } } catch (EndOfStreamException) { state.Buffer = null; state.Socket.Send(Encoding.ASCII.GetBytes("INVALID MESSAGE")); } } } else if (msg.StartsWith("FILE STOP\0")) // Abort file transfer { } else if (msg.StartsWith("FILE DATA\0")) // Set data block of file { } else if (msg.StartsWith("LOGOUT\0")) // Close connection { } else // Invalid message { state.Buffer = null; state.Socket.Send(this.invalidMessage); } }
public bool AlreadyConnected(string ip) => ConnectedClients.Where(client => ((IPEndPoint)client.RemoteEndPoint).Address.ToString() == ip).Count() > 1;
public Socket GetClientByIP(string ip) => ConnectedClients.Where(client => ((IPEndPoint)client.RemoteEndPoint).Address.ToString() == ip).FirstOrDefault();