/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Initiates the connection to the preset ip address and port /// /// We exect to be placed in our own thread or we will block the caller /// </summary> /// <history> /// 19 Nov 18 Cynic - Started /// </history> private void InitiateConnection() { try { // Create a TcpClient. If this client to work you need to have a TcpServer // listening on the same ip address and port tcpClient = new TcpClient(IPAddr, PortNumber); LogMessage("InitiateConnection, tcpClient opened"); Console.WriteLine("InitiateConnection, tcpClient opened"); // now we send a message saying we have connected ServerClientData scData = new ServerClientData(ServerClientDataContentEnum.REMOTE_CONNECT); SendData(scData); } catch (Exception ex) { if (shutdownInProgress == true) { // ignore the exception. Probably the shutdown triggered it return; } LogMessage("InitiateConnection, exception: " + ex.Message); Console.WriteLine("InitiateConnection, exception: " + ex.Message); } }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Sends data via the tcpConnection. /// </summary> /// <param name="scData">the data object we send</param> /// <history> /// 19 Nov 18 Cynic - Started /// </history> public void SendData(ServerClientData scData) { if (tcpClient == null) { LogMessage("SendData, tcpClient == null"); Console.WriteLine("SendData, tcpClient == null"); throw new Exception("SendData, tcpClient == null"); } if (scData == null) { LogMessage("SendData, scData == null"); Console.WriteLine("SendData, scData == null"); throw new Exception("SendData, scData == null"); } binaryFormatter.Serialize(tcpClient.GetStream(), scData); }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Shuts down the tcpConnection. Can be called from any thread. /// </summary> /// <history> /// 19 Nov 18 Cynic - Started /// </history> public void Shutdown() { LogMessage("Shutdown, called"); Console.WriteLine("Shutdown, called"); shutdownInProgress = true; // shutdown the tcpListener if (tcpListener != null) { tcpListener.Stop(); tcpListener = null; } if (tcpClient != null) { try { // now we send a message saying we have connected ServerClientData scData = new ServerClientData(ServerClientDataContentEnum.REMOTE_DISCONNECT); SendData(scData); Thread.Sleep(500); } catch { } LogMessage("Shutdown, tcpClient closing"); Console.WriteLine("Shutdown, tcpClient closing"); tcpClient.Client.Disconnect(false); tcpClient.Client.Close(); LogMessage("Shutdown, tcpClient stream closed"); Console.WriteLine("Shutdown, tcpClient stream closed"); tcpClient.Close(); LogMessage("Shutdown, tcpClient closed"); Console.WriteLine("Shutdown, tcpClient closed"); tcpClient = null; } else { // client did not successfully start. Just abort the thread clientReadThread.Abort(); LogMessage("Shutdown, tcpClient thread aborted"); Console.WriteLine("Shutdown, tcpClient thread aborted"); } }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Waits for data on the existing tcpClient connection /// /// We exect to be placed in our own thread or we will block the caller /// </summary> /// <history> /// 19 Nov 18 Cynic - Started /// </history> private void WaitForData() { try { // we loop waiting for data while (true) { // sit waiting for the data ServerClientData scData = (ServerClientData)binaryFormatter.Deserialize(tcpClient.GetStream()); if (scData == null) { LogMessage("WaitForData, scData == null"); Console.WriteLine("WaitForData, scData == null"); continue; } DebugMessage("Received scData: scData.DataInt, scData.DataStr" + scData.DataStr); // send the data to the interested parties if (ServerClientDataEvent != null) { // NOTE: we are in our own thread here. Caller must be aware of this ServerClientDataEvent(this, scData); } } // bottom of while (true) } catch (Exception ex) { if (shutdownInProgress == true) { // ignore the exception. Probably the shutdown triggered it return; } LogMessage("WaitForData, exception: " + ex.Message); Console.WriteLine("WaitForData, exception: " + ex.Message); } // if we get here the thread ends LogMessage("WaitForData, ending"); Console.WriteLine("WaitForData, ending"); }
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= /// <summary> /// Initiates the connection to the preset ip address and port /// /// We exect to be placed in our own thread or we will block the caller /// </summary> /// <history> /// 19 Nov 18 Cynic - Started /// </history> private void InitiateListener() { try { // Create a TcpClient. If this client to work you need to have a TcpServer // listening on the same ip address and port // start the TCP Listener tcpListener = new TcpListener(IPAddress.Parse(IPAddr), PortNumber); tcpListener.Start(); LogMessage("InitiateListener, tcpListener opened"); Console.WriteLine("InitiateListener, tcpListener opened"); // Accept will block until someone connects tcpClient = tcpListener.AcceptTcpClient(); // we have a connection. log it LogMessage("InitiateListener, client connection accepted"); Console.WriteLine("InitiateListener, client connection accepted"); // stop and remove the listener - we only accept one client tcpListener.Stop(); tcpListener = null; // now we send a message saying we have connected ServerClientData scData = new ServerClientData(ServerClientDataContentEnum.REMOTE_CONNECT); SendData(scData); } catch (Exception ex) { if (shutdownInProgress == true) { // ignore the exception. Probably the shutdown triggered it return; } LogMessage("InitiateListener, exception: " + ex.Message); Console.WriteLine("InitiateListener, exception: " + ex.Message); } }