public void Start(bool fromTimer = false) { if (IsConnected) { return; } lock (_sync) { MainSocket = null; _lastConnected = _lastPing = _lastPong = DateTimeOffset.Now; try { var socket = TryConnectTcp(RelayAddress, RelayPort); if (socket == null) { return; } MainSocket = new ProtocolSocket(socket); MainSocket.S2C_OnStartNewConnection = HandleStartNewConnection; MainSocket.S2C_OnKeepAlive = HandleServerKeepAlive; MainSocket.Initialize(); } catch { } finally { if (!fromTimer) { _reconnectTimer.Change(100, Timeout.Infinite); } } } }
private void HandleNewInternalSocket(Socket socket) { var protoSocket = new ProtocolSocket(socket); protoSocket.Disconnected += ProtoSocket_Disconnected; protoSocket.C2S_OnStartRelay = HandleStartRelay; protoSocket.Initialize(); Console.WriteLine($"New internal socket from {socket.RemoteEndPoint}"); }
public void SetProxy(ProtocolSocket proxy) { if (IsActive) { return; } _settingUp.Reset(); Proxy = proxy; Proxy.OnSendPayload = HandleSendPayload; Proxy.Disconnected += Proxy_Disconnected; proxy.Initialize(); TryInitialize(); _settingUp.Set(); }
private void HandleNewSocket(Socket socket) { if (MainSocket == null || !MainSocket.IsConnected || (DateTimeOffset.Now - _lastPing).TotalSeconds > 15) { Console.WriteLine($"Replacing the old socket with one from {socket.RemoteEndPoint}"); if (MainSocket != null) { MainSocket.Close(); } _lastPing = DateTimeOffset.Now; MainSocket = new ProtocolSocket(socket); MainSocket.C2S_OnStartNewConnectionReply = HandleConnectionReply; MainSocket.C2S_OnKeepAlive = HandleKeepAlive; MainSocket.Initialize(); } else { socket.Close(); } }