private void HandleSendPayload(ProtocolSocket proxy, byte[] buffer, int offset, int count) { int sent = 0; _settingUp.Wait(); while (IsActive && count > 0) { try { sent = Client.Send(buffer, offset, count, SocketFlags.None); } catch (Exception ex) { Close(ex); } if (sent == 0) { Close(); } else { offset += sent; count -= sent; } } }
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 HandleServerKeepAlive(ProtocolSocket obj) { lock (_sync) { _lastPong = DateTimeOffset.Now; Console.WriteLine($"Roundtrip ping: {(_lastPong - _lastPing).TotalMilliseconds:0.00}ms"); } }
private void HandleStartRelay(ProtocolSocket socket, int id) { if (!Channels.TryGetValue(id, out var channel)) { return; } channel.SetProxy(socket); }
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 HandleStartNewConnection(ProtocolSocket socket, int id, ushort targetPort, ushort internalPort) { Console.WriteLine($"HandleStartNewConnection #{id} target={targetPort} internal={internalPort}"); var localSocket = TryConnect(BoundInterface.ToString(), targetPort); var remoteSocket = TryConnect(RelayAddress, internalPort); var success = localSocket != null && remoteSocket != null; if (IsConnected) { MainSocket.C2S_StartNewConnectionReply(id, internalPort, success); } var protoSocket = new ProtocolSocket(remoteSocket); protoSocket.C2S_StartRelay(id); var proxy = new ChannelProxy(localSocket); proxy.ChannelId = id; proxy.SetProxy(protoSocket); }
private void HandleConnectionReply(ProtocolSocket socket, int id, ushort internalPort, bool success) { Console.WriteLine($"HandleConnectionReply id={id}, internal={internalPort}, success={success}"); if (!Relays.TryGetValue(internalPort, out var relay)) { return; } if (!relay.Channels.TryGetValue(id, out var channel)) { return; } if (channel.IsActive) { return; } if (!success) { channel.Close(); } }
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(); } }
public ChannelProxy(ProtocolSocket proxy) { SetProxy(proxy); }
private void Proxy_Disconnected(ProtocolSocket socket, Exception exception) { Close(exception); }
private void HandleKeepAlive(ProtocolSocket obj) { Console.WriteLine("Sending keep alive"); _lastPing = DateTimeOffset.Now; obj.S2C_KeepAlive(); }
private void ProtoSocket_Disconnected(ProtocolSocket socket, Exception exception) { TemporarySockets.TryRemove(socket, out _); }