// Private Methods //******************************************************************** /// <summary> Called when a message arrives </summary> /// <param name="ar"> RefType: An async result interface </param> private void ReceiveComplete(IAsyncResult ar) { try { // Is the Network Stream object valid if (GetNetworkStream.CanRead) { // Read the current bytes from the stream buffer int iBytesRecieved = GetNetworkStream.EndRead(ar); // If there are bytes to process else the connection is lost if (iBytesRecieved > 0) { // A message came in send it to the MessageHandler try { GetMessageHandler(this, iBytesRecieved); } catch (Exception ex) { LogLib.WriteLine("Error with GetMessageHandler() in CSocketClient.ReceiveComplete(): ", ex); } // Wait for a new message Receive(); } else { LogLib.WriteLine("CSocketClient.ReceiveComplete(): Shuting Down", LogLevel.Error); throw new Exception("Shut Down"); } } } catch (Exception) { // The connection must have dropped call the CloseHandler try { GetCloseHandler(this); } catch (Exception ex) { LogLib.WriteLine("Error in CSocketClient.ReceiveComplete(): ", ex); } // Dispose of the class Dispose(); } }
//******************************************************************** /// <summary> Function used to disconnect from the server </summary> public void Disconnect() { LogLib.WriteLine("Entering in CSocketClient.Disconnect()", LogLevel.Trace); // Close down the connection if (GetNetworkStream != null) { GetNetworkStream.Close(); } if (GetTcpClient != null) { GetTcpClient.Close(); } if (GetClientSocket != null) { GetClientSocket.Close(); } // Clean up the connection state GetClientSocket = null; GetNetworkStream = null; GetTcpClient = null; LogLib.WriteLine("Exiting in CSocketClient.Disconnect()", LogLevel.Trace); }
//******************************************************************** /// <summary> Wait for a message to arrive </summary> public void Receive() { if (GetNetworkStream?.CanRead == true) { // Issue an asynchronous read GetNetworkStream.BeginRead(GetRawBuffer, 0, GetSizeOfRawBuffer, GetCallbackReadMethod, null); } else { LogLib.WriteLine("Error in CSocketClient.Receive(): Socket Closed"); } }
/// <summary> Function to send a raw buffer to the server </summary> /// <param name="pRawBuffer"> RefType: A Raw buffer of bytes to send </param> public void Send(byte[] pRawBuffer) { if (GetNetworkStream?.CanWrite == true) { // Issue an asynchronus write GetNetworkStream.BeginWrite(pRawBuffer, 0, pRawBuffer.GetLength(0), GetCallbackWriteMethod, null); } else { LogLib.WriteLine("Error in CSocketClient.Send(Byte[]): Socket Closed"); } }
//******************************************************************** /// <summary> Called when a message is sent </summary> /// <param name="ar"> RefType: An async result interface </param> private void SendComplete(IAsyncResult ar) { try { // Is the Network Stream object valid if (GetNetworkStream.CanWrite) { GetNetworkStream.EndWrite(ar); LogLib.WriteLine("CSocketClient.SendComplete(): GetNetworkStream.EndWrite()", LogLevel.Debug); } } catch (Exception ex) { LogLib.WriteLine("Error in CSocketClient.SendComplete(): ", ex); } }
//******************************************************************** /// <summary> Function used to disconnect from the server </summary> public void Disconnect() { // Close down the connection GetNetworkStream?.Close(); GetTcpClient?.Close(); GetClientSocket?.Close(); // Clean up the connection state GetClientSocket = null; GetNetworkStream = null; GetTcpClient = null; }
//******************************************************************** /// <summary> Wait for a message to arrive </summary> public void Receive() { LogLib.WriteLine("Entering in CSocketClient.Receive()", LogLevel.Trace); if ((GetNetworkStream != null) && (GetNetworkStream.CanRead)) { // Issue an asynchronous read GetNetworkStream.BeginRead(GetRawBuffer, 0, GetSizeOfRawBuffer, GetCallbackReadMethod, null); } else { LogLib.WriteLine("Error in CSocketClient.Receive(): Socket Closed"); } LogLib.WriteLine("Exiting in CSocketClient.Receive()", LogLevel.Trace); }
//******************************************************************** /// <summary> Function to send a raw buffer to the server </summary> /// <param name="pRawBuffer"> RefType: A Raw buffer of bytes to send </param> public void Send(Byte[] pRawBuffer) { LogLib.WriteLine("Entering in CSocketClient.Send(Byte[])", LogLevel.Trace); if ((GetNetworkStream != null) && (GetNetworkStream.CanWrite)) { // Issue an asynchronus write GetNetworkStream.BeginWrite(pRawBuffer, 0, pRawBuffer.GetLength(0), GetCallbackWriteMethod, null); } else { LogLib.WriteLine("Error in CSocketClient.Send(Byte[]): Socket Closed"); } LogLib.WriteLine("Exiting in CSocketClient.Send(Byte[])", LogLevel.Trace); }
//******************************************************************** /// <summary> Function to send a string to the server </summary> /// <param name="strMessage"> RefType: A string to send </param> public void Send(String strMessage) { LogLib.WriteLine("Entering in CSocketClient.Send(String): " + strMessage, LogLevel.Trace); if ((GetNetworkStream != null) && (GetNetworkStream.CanWrite)) { // Convert the string into a Raw Buffer Byte[] pRawBuffer = System.Text.Encoding.ASCII.GetBytes(strMessage); // Issue an asynchronus write GetNetworkStream.BeginWrite(pRawBuffer, 0, pRawBuffer.GetLength(0), GetCallbackWriteMethod, null); } else { LogLib.WriteLine("Error in CSocketClient.Send(string): Socket Closed"); } LogLib.WriteLine("Exiting in CSocketClient.Send(String): " + strMessage, LogLevel.Trace); }
//******************************************************************** /// <summary> Function used to disconnect from the server </summary> public void Disconnect() { // Close down the connection GetNetworkStream?.Close(); GetTcpClient?.Close(); }