public async Task InterceptAsync(HotelEndPoint endpoint) { _isIntercepting = true; int interceptCount = 0; while (!IsConnected && _isIntercepting) { try { Local = await HNode.AcceptNodeAsync(ListenPort).ConfigureAwait(false); if (!_isIntercepting) { break; } if (++interceptCount == SocketSkip) { interceptCount = 0; continue; } byte[] buffer = await Local.PeekAsync(6).ConfigureAwait(false); if (!_isIntercepting) { break; } if (buffer.Length == 0) { interceptCount--; continue; } Remote = new HNode(); if (HFormat.EvaWire.ReadUInt16(buffer, 4) != 4000) { buffer = await Local.ReceiveAsync(512).ConfigureAwait(false); if (!_isIntercepting) { break; } if (Encoding.UTF8.GetString(buffer) == CROSS_DOMAIN_POLICY_REQUEST) { await Local.SendAsync(Encoding.UTF8.GetBytes(CROSS_DOMAIN_POLICY_RESPONSE)).ConfigureAwait(false); } else { throw new Exception("Expected cross-domain policy request."); } continue; } var args = new ConnectedEventArgs(endpoint); OnConnected(args); endpoint = (args.HotelServer ?? endpoint); endpoint ??= await args.HotelServerSource.Task.ConfigureAwait(false); if (args.IsFakingPolicyRequest) { using var tempRemote = await HNode.ConnectNewAsync(endpoint).ConfigureAwait(false); await tempRemote.SendAsync(Encoding.UTF8.GetBytes(CROSS_DOMAIN_POLICY_REQUEST)).ConfigureAwait(false); } if (!await Remote.ConnectAsync(endpoint).ConfigureAwait(false)) { break; } IsConnected = true; TotalIncoming = 0; TotalOutgoing = 0; Task interceptOutgoingTask = InterceptOutgoingAsync(); Task interceptIncomingTask = InterceptIncomingAsync(); } finally { if (!IsConnected) { Local?.Dispose(); Remote?.Dispose(); } } } HNode.StopListeners(ListenPort); _isIntercepting = false; }
protected virtual void OnConnected(ConnectedEventArgs e) { Connected?.Invoke(this, e); }