//******************************************************************** /// <summary> Called when a message is extracted from the socket </summary> /// <param name="pSocket"> The SocketClient object the message came from </param> /// <param name="iNumberOfBytes"> The number of bytes in the RawBuffer inside the SocketClient </param> private void MessageHandlerClient(CSocketClient pSocket, int iNumberOfBytes) { // Process the packet try { ProcessPacket(pSocket.GetRawBuffer, iNumberOfBytes); } catch (Exception pException) { LogLib.WriteLine("Error with ProcessPacket: " + pException.Message); } }
//******************************************************************** /// <summary> Called when a socket error occurs </summary> /// <param name="pSocket"> The SocketClient object the message came from </param> /// <param name="pException"> The reason for the error </param> public void ErrorHandler(CSocketClient pSocket, Exception pException) { LogLib.WriteLine("Error with ErrorHandler(): " + pException.Message); MessageBox.Show(pException.Message); }
public bool ConnectToServer(string ServerAddress, int ServerPort, bool errMsg = true) { try { if (pSocketClient != null) { Thread.Sleep(2000); pSocketClient.Dispose(); pSocketClient = null; } // Instantiate a CSocketClient object pSocketClient = new CSocketClient(100000, null, new CSocketClient.MESSAGE_HANDLER(MessageHandlerClient), new CSocketClient.CLOSE_HANDLER(CloseHandler), new CSocketClient.ERROR_HANDLER(ErrorHandler)); // Establish a connection to the server mbGetProcessInfo = true; pSocketClient.Connect(ServerAddress, (short)ServerPort); return(true); } catch (Exception pException) { string msg = String.Format("Could not connect to the server: {0}", pException.Message); LogLib.WriteLine(msg); if (errMsg) { MessageBox.Show(msg + "\r\nTry selecting a different server!", "Server Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(false); } }
//******************************************************************** /// <summary> Called when a socket connection is closed </summary> /// <param name="pSocket"> The SocketClient object the message came from </param> private void CloseHandler(CSocketClient pSocket) { try { if (f1 != null) { f1.StopListening(); } else { StopListening(); } } catch (Exception pException) { LogLib.WriteLine($"Error with CloseHandler(): {pException.Message}"); } }
public void StopListening() { try { RequestPending = false; numPackets = numProcessed = 0; pSocketClient?.Dispose(); pSocketClient = null; } catch (Exception pException) { LogLib.WriteLine($"Error with StopListening(): {pException.Message}"); } }
public void StopListening() { try { RequestPending = false; numPackets = numProcessed = 0; if (pSocketClient != null) { pSocketClient.Dispose(); } pSocketClient = null; } catch (Exception pException) { LogLib.WriteLine("Error with StopListening(): " + pException.Message); } }