public void Stop() { if (socketRelay != null) { socketRelay.KillSession(session); socketRelay = null; } }
private void RelayThread(object state) { TcpClient extConn = new TcpClient(); try { object[] args = state as object[]; if (args == null) { log.Write(TraceLevel.Error, "CODING ERROR: RelayThread started with invalid arguments!"); ErrorDescription = "Internal error. Please contact MiracleSticks technical support."; return; } IPEndPoint remoteEP = args[0] as IPEndPoint; if (remoteEP == null) { log.Write(TraceLevel.Error, "CODING ERROR: RelayThread started with invalid arguments!"); ErrorDescription = "Internal error. Please contact MiracleSticks technical support."; return; } try { extConn.Connect(remoteEP); } catch (Exception ex) { log.Write(TraceLevel.Error, String.Format("Failed to connect to relay: {0}:{1}", remoteEP.Address, remoteEP.Port), ex); ErrorDescription = "Failed to connect to MiracleSticks relay service"; return; } session = new RelaySession(Guid.NewGuid().ToString()) { ServerIP = remoteEP.Address, ClientIP = IPAddress.Loopback, ClientReserveTime = DateTime.Now, ClientSocket = null, ServerReserveTime = DateTime.Now, ServerSocket = extConn }; Dictionary <string, RelaySession> sessions = new Dictionary <string, RelaySession>(); sessions.Add(session.SessionId, session); socketRelay = new DelayedConnectRelay(sessions); socketRelay.ClientConnected += OnClientConnected; socketRelay.ClientDisconnected += OnClientDisconnected; if (AppMain.DebugMode) { socketRelay.DebugLog = log; } socketRelay.AddClient(session, extConn); } catch (Exception ex) { log.Write(TraceLevel.Error, "Relay connection error", ex); ErrorDescription = "Relay connection error: " + ex.Message; } finally { relayStarted.Set(); } }