/// <summary> /// Runs when data is sent to the client /// </summary> /// <param name="asyncResult">Result containing the client info</param> public void DataSentCallback(IAsyncResult asyncResult) { ClientInfo client = (ClientInfo)asyncResult.AsyncState; try { if (_usingSsl) { client._clientSslStream.EndWrite(asyncResult); Console.WriteLine($"DataSentCallbackSsl(): Successfully sent message to {client.GetUsername()}"); _semaphorePool.Release(); } else { int result = client.ClientSocket().EndSend(asyncResult, out var errorCode); Console.WriteLine(errorCode == SocketError.Success ? $"Successfully sent message with size of {result} bytes to {client.GetUsername()}" : $"Error sending. code: {errorCode}"); } } catch (Exception e) { Console.WriteLine("Unhandled DataSentCallback() Exception! " + e); } }
/// <summary> /// Runs when data is received from a client. Parses the information and sends it off /// to a method to handle the information. /// </summary> /// <param name="asyncResult">Result containing the client info</param> public void DataReceivedCallback(IAsyncResult asyncResult) { ClientInfo client = (ClientInfo)asyncResult.AsyncState; try { var readBytes = _usingSsl ? client._clientSslStream.EndRead(asyncResult) : client.ClientSocket().EndReceive(asyncResult); client.ClientStringBuilder().Append(Encoding.ASCII.GetString(client.DataBuffer(), 0, readBytes));// Add on the new data string currentData = client.ClientStringBuilder().ToString(); int position; while ((position = currentData.IndexOf("<EOF>", StringComparison.Ordinal)) != -1)// Keep going until we don't have any more commands { string command = currentData.Substring(0, position); Console.WriteLine($"<{client._username}> {command}"); currentData = currentData.Remove(0, position + 5); // Remove the command from the currentData string HelperMethods.CommandHandler(command, client); // Handle the command } client.ClientStringBuilder().Clear(); // We used the data in the string builder so now we can clear it client.ClientStringBuilder().Append(currentData); // There still might be some data left so we add it back in client.BeginReceiveData(); // Continue receiving data } catch (ObjectDisposedException) { Console.WriteLine("DataReceivedCallback(): Socket was closed before finishing receive"); } catch (SocketException se) { if (se.ErrorCode == 10054)// Connection reset by peer. { Console.WriteLine($"DataReceivedCallback(): Client went offline"); } else { Console.WriteLine($"DataReceivedCallback(): {se.Message} Error Code: {se.ErrorCode}"); } HelperMethods.RemoveClientFromAll(client); } catch (IOException ioe) { if (ioe.InnerException is SocketException se) { if (se.ErrorCode == 10054)// Connection reset by peer. { Console.WriteLine($"DataReceivedCallback(): Client went offline"); } } else { Console.WriteLine($"DataReceivedCallback(): {ioe.Message}"); } HelperMethods.RemoveClientFromAll(client); } catch (Exception e) { Console.WriteLine($"DataReceivedCallbackSsl(): {e.Message}"); if (e.InnerException != null) { Console.WriteLine($"DataReceivedCallbackSsl(): {e.InnerException}"); } HelperMethods.RemoveClientFromAll(client); } }