public void SendCommand <T>(string message) where T : TemplateCommand { if (!isWorking) { allocToDrop(info, new Exception("Client dosen't connect")); return; } if (message == null) { message = ""; } var buffer = commandController.GetBufferMessage <T>(message); if (buffer == null) { DebugAdapter.Log($"Havan't \"{typeof(T).Name}\" command"); return; } var descripte = commandController.GetDescriptMessage(buffer); sendner.asynSendMessageFromClient(descripte, info, allocSended, allocToDrop); }
private void allocDropClientSendMessage(ClientInfo client, Exception e) { DebugAdapter.Log("Drop client: " + e); DisconnectClient(client.Name); SendCommandFromAllClients <TemplateDisconnect>(client.Name); DebugAdapter.Log($"Client \"{client.Name}\" has been removed"); }
private void allocFindedClient(ClientInfo client) { if (Clients.ContainsKey(client.Name)) { CloseClient(client); return; } Clients.Add(client.Name, client); DebugAdapter.Log($"Client \"{client.Name}\" has join to server"); sendner.asynListeningClient(client, allocSendedMessageClientFromServer, allocDropClientSendMessage); }
public void StopServer() { foreach (var clientInfo in Clients) { clientInfo.Value.ClientTCP.GetStream().Close(); clientInfo.Value.ClientTCP.Close(); } listner.Stop(); sendner.Stop(); isWorking = false; DebugAdapter.Log("Server is stoped"); }
public void Connect(string ip, int port) { if (isWorking) { DebugAdapter.Log("You already connect"); return; } info = new ClientInfo(info.Name, info.hashPassword); info.ClientTCP.Connect(ip, port); stream = info.ClientTCP.GetStream(); sendner.Start(); isWorking = true; SendCommand <TemplateConnect>(info.Name + " " + info.hashPassword); sendner.asynListeningClient(info, allocServerSended, allocToDrop); }
public void AbortClient() { try { info.ClientTCP.Close(); stream.Close(); sendner.Stop(); } catch { } finally { isWorking = false; DebugAdapter.Log("Client disconnected"); } }
public void StartServer() { if (isWorking) { return; } isWorking = true; DebugAdapter.Log("Server is running"); DebugAdapter.Log($"Server password: {ServerPassword}"); listner.Start(); sendner.Start(); sendner.asynFindingClient(listner, ServerPassword, allocFindedClient, allocBreackServer); }
private void allocSendedMessageClientFromServer(ClientInfo client, byte[] buffer) { var descript = commandController.GetDescriptWhithCommands(buffer, this, null); if (descript == null) { DebugAdapter.Log($"Server take brake message from {client.Name}"); } else if (descript.Tempate is TemplateDisconnect) { DisconnectClient(client.Name); } else { DebugAdapter.Log($"Server take message from {client.Name}: {descript.Message}"); SendCommandFromAllClients(descript); } }
private void allocBreackServer(Exception e) { DebugAdapter.Log("Server is crashed: " + e.Message); StopServer(); }
private void allocServerSended(ClientInfo info, byte[] buffer) { var message = commandController.GetDescriptWhithCommands(buffer, null, this); DebugAdapter.Log($"Take message: {message.Message}"); }
private void allocSended(ClientInfo info, MessageDescript descript) { DebugAdapter.Log($"Sended: {descript.Message}"); }
private void allocToDrop(ClientInfo info, Exception exception) { DebugAdapter.Log($"Client drop: {exception}"); }