// Handle new connection private Task HandleConnectionAsync(SockTcpClient sockTcpClient) { string receivedBuffer = ""; #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously return(Task.Run(async() => { using (NetworkStream networkStream = sockTcpClient.ClientSocket.GetStream()) { while (true) { bool isDisconnected = false; string dataFromClient = ""; var bytesFrom = new byte[sockTcpClient.ClientSocket.ReceiveBufferSize]; try { networkStream.Read(bytesFrom, 0, (int)sockTcpClient.ClientSocket.ReceiveBufferSize); } catch { isDisconnected = true; } if (isDisconnected == false) { dataFromClient = Encoding.ASCII.GetString(bytesFrom); dataFromClient = dataFromClient.Replace("\0", ""); if (dataFromClient.Length <= 0) { isDisconnected = true; } } if (isDisconnected) { Logger.Log($"Client Disconnected [Received 0 length buffer] (#{sockTcpClient.ClientNumber}, IP:{sockTcpClient.ClientAddress})"); ConnectedClients.Remove(sockTcpClient); receivedBuffer = ""; sockTcpClient.ClientSocket.Close(); break; } else { receivedBuffer += dataFromClient; //if (receivedBuffer.Contains("\r")) //{ Logger.Log($"Received from client {sockTcpClient.ClientNumber}: {receivedBuffer}"); OnReceivedFromClient(receivedBuffer); receivedBuffer = ""; //} } } } })); #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously }
private static SockTcpClient SendMessage(SockTcpClient clientSocket, string _message) { try { byte[] message = Encoding.ASCII.GetBytes(_message.Trim() + "\r\n"); clientSocket.ClientSocket.GetStream().WriteAsync(message, 0, message.Length); clientSocket.ClientSocket.GetStream().Flush(); } catch (Exception) { return(clientSocket); } return(null); }
public Task StartListener() { return(Task.Run(async() => { // Start Server SOCKET! _counter = 0; try { _serverSocket = new TcpListener(IPAddress.Any, _localport); } catch (Exception e) { OnListeningFailed?.Invoke(this, new MessengerEventArgs(e.Message)); return; } try { _serverSocket.Start(); OnListeningStarted?.Invoke(this, new MessengerEventArgs("Listening on Port " + _localport)); } catch (SocketException e) { OnListeningFailed?.Invoke(this, new MessengerEventArgs(e.Message)); return; } while (true) { _counter++; var tcpClient = await _serverSocket.AcceptTcpClientAsync(); SockTcpClient s = new SockTcpClient(tcpClient, _counter, ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString()); var task = StartHandleConnectionAsync(s); // if already faulted, re-throw any error on the calling context if (task.IsFaulted) { task.Wait(); } } })); }
// Register and handle the connection private async Task StartHandleConnectionAsync(SockTcpClient sockTcpClient) { lock (_lock) { SendMessage(sockTcpClient, _greeting); _counter++; //SockTcpClient sockTcpClient = new SockTcpClient(sockTcpClient, _counter, ((IPEndPoint)sockTcpClient.Client.RemoteEndPoint).Address.ToString()); Logger.Log($"Client Connected: #{Convert.ToString(sockTcpClient.ClientNumber)} ({sockTcpClient.ClientAddress})"); ConnectedClients.Add(sockTcpClient); Logger.Log($"Clients Count: {ConnectedClients.Count}"); } // start the new connection task var connectionTask = HandleConnectionAsync(sockTcpClient); // add it to the list of pending task lock (_lock) { _connections.Add(connectionTask); } // catch all errors of HandleConnectionAsync try { await connectionTask; // we may be on another thread after "await" } catch (Exception ex) { // Logger.Log the error Console.WriteLine(ex.ToString()); } finally { // remove pending task lock (_lock) _connections.Remove(connectionTask); } }
public static void Broadcast(string msg) { ArrayList toBeRemoved = new ArrayList(); foreach (SockTcpClient c in ConnectedClients) { SockTcpClient failed = SendMessage(c, msg); if (failed != null) { toBeRemoved.Add(failed); } } for (int i = toBeRemoved.Count - 1; i >= 0; i--) { SockTcpClient failedClient = toBeRemoved[i] as SockTcpClient; if (failedClient != null) { toBeRemoved.RemoveAt(i); ConnectedClients.Remove(failedClient); Logger.Log($"Client Disconnected (IP:{failedClient.ClientAddress})"); } } }