public TmpControlConnection(TlsSettings tlsSettings, IPEndPoint remoteEndPoint, Socket socket, IDataHandler dataSender, IDataFilter receiveDataFilter) { this.tlsSettings = tlsSettings; this.remoteEndPoint = remoteEndPoint; this.dataSender = dataSender; this.receiveDataFilter = receiveDataFilter; //this.frameAndHeartbeatReceiveHandler = new FrameAndHeartbeatReceiverHandler(HandleCommand, HandleHeartbeat, null); this.frameProtocolReceiveHandler = new FrameProtocolReceiverHandler(HandleCommand, null); }
public Boolean TryConnectAndInitialize(TlsSettings tlsSettings, Buf sendBuffer, ServerInfo serverInfo /*, SelectTunnelsThread tunnelsThread*/) { if (Connected) { throw new InvalidOperationException(String.Format( "You called Connect() on accessor '{0}' but its already connected", accessorHostString)); } try { accessorSocket = NewSocketConnection(ref accessorHost); // // Send initial connection information // Boolean setupTls = tlsSettings.requireTlsForTmpConnections; Byte[] connectionInfoPacket = new Byte[] { Tmp.CreateConnectionInfoFromTmpServerToAccessor( tlsSettings.requireTlsForTmpConnections, false) }; accessorSocket.Send(connectionInfoPacket); // // Only receive packet if tls was not required // if (!tlsSettings.requireTlsForTmpConnections) { int bytesRead = accessorSocket.Receive(connectionInfoPacket, 0, 1, SocketFlags.None); if (bytesRead <= 0) { throw new SocketException(); } setupTls = Tmp.ReadTlsRequirementFromAccessorToTmpServer(connectionInfoPacket[0]); } DataHandler accessorSendHandler = new SocketSendDataHandler(accessorSocket).HandleData; this.accessorReceiveHandler = new DataFilterHandler(new FrameProtocolFilter(), new TmpServerHandler(this, tlsSettings /*, tunnelsThread*/)); // // Initiate a tls connection if required // if (setupTls) { throw new NotImplementedException("Tls not yet implemented"); } // // Send server info // UInt32 commandLength = Tmp.SerializeCommand(ServerInfo.Serializer, Tmp.ToAccessorServerInfoID, serverInfo, sendBuffer, 0); accessorSendHandler(sendBuffer.array, 0, commandLength); return(true); } catch (Exception) { this.accessorSocket = null; this.accessorReceiveHandler = null; return(false); } }
//readonly SelectTunnelsThread tunnelsThread; public TmpServerHandler(AccessorConnection accessorConnection, TlsSettings tlsSettings /*, SelectTunnelsThread tunnelsThread*/) { this.accessorConnection = accessorConnection; this.tlsSettings = tlsSettings; //this.tunnelsThread = tunnelsThread; }
static void Main(string[] args) { Options options = new Options(); List <String> nonOptionArgs = options.Parse(args); if (nonOptionArgs.Count != 2) { options.ErrorAndUsage("Expected 2 non option argument but got none"); return; } String serverName = nonOptionArgs[0]; String accessorConnectorString = nonOptionArgs[1]; TlsSettings tlsSettings; if (!options.authenticationFile.set) { tlsSettings = new TlsSettings(false); } else { throw new NotImplementedException("Authentication file not yet implemented"); } /* * Run(tlsSettings, serverName, accessorConnector, * options.heartbeatSeconds.ArgValue, * options.reconnectWaitSeconds.ArgValue, options.receiveBufferLength.ArgValue); * } * public static void Run(TlsSettings tlsSettings, String serverName, String accessorConnectorString, * UInt16 heartbeatSeconds, UInt16 reconnectWaitSeconds, UInt32 receiveBufferLength) * {*/ ServerInfo serverInfo = new ServerInfo( Encoding.ASCII.GetBytes(serverName), options.heartbeatSeconds.ArgValue, options.reconnectWaitSeconds.ArgValue); //SelectTunnelsThread tunnelsThread = new SelectTunnelsThread(receiveBufferLength); AccessorConnection accessorConnection = new AccessorConnection(accessorConnectorString); //TunnelControlServer tunnelServer = new TunnelControlServer(tunnelsThread, accessor); //NpcReflector npcReflector = new NpcReflector( // new NpcExecutionObject(tunnelServer, "TunnelServer", null, null) // ); Int32 heartbeatMillis = serverInfo.SecondsPerHeartbeat * 1000; Int32 reconnectWaitMillis = serverInfo.SecondsPerReconnect * 1000; Buf safeBuffer = new Buf(options.receiveBufferLength.ArgValue); SelectServer selectServer = new SelectServer(true, safeBuffer); accessorConnection.StartConnect(ref selectServer.control, safeBuffer); selectServer.Run(); /* * // * // TmpHiddenServer Loop * // * Byte[] receiveBuffer = new Byte[receiveBufferLength]; * Buf sendBuffer = new Buf(256, 256); * Int64 lastHearbeatTime = 0; * SingleObjectList singleAccessorSocket = new SingleObjectList(); * * while (true) * { * // * // While the accessor is not connected * // * while (!accessorConnection.Connected) * { * Console.WriteLine("{0} [{1}] Attempting reconnect...", DateTime.Now, accessorConnection.accessorHostString); * if (accessorConnection.TryConnectAndInitialize(tlsSettings, sendBuffer, serverInfo)) * { * Console.WriteLine("{0} [{1}] Connected", DateTime.Now, accessorConnection.accessorHostString); * lastHearbeatTime = Stopwatch.GetTimestamp(); * } * else * { * Console.WriteLine("{0} [{1}] Failed to connect, waiting {2} seconds for reconnect", * DateTime.Now, accessorConnection.accessorHostString, reconnectWaitMillis / 1000); * Thread.Sleep(reconnectWaitMillis); * } * } * * // * // While the accessor is connected * // * while (accessorConnection.Connected) * { * // * // Calculate the next heartbeat timeout * // * Int64 now = Stopwatch.GetTimestamp(); * Int32 nextHeartbeatTimeoutMillis = heartbeatMillis - StopwatchExtensions.StopwatchTicksAsInt32Milliseconds(now - lastHearbeatTime); * if (nextHeartbeatTimeoutMillis <= 0) * { * Console.WriteLine("{0} [{1}] Sending hearbeat...", DateTime.Now, accessorConnection.accessorHostString); * * accessorConnection.SendHeartbeat(); * if (!accessorConnection.Connected) break; * * lastHearbeatTime = now; * nextHeartbeatTimeoutMillis = heartbeatMillis; * } * * accessorConnection.ReceiveWithTimeout(receiveBuffer, nextHeartbeatTimeoutMillis); * } * } */ }