private async Task InterceptClientAsync(ushort port) { try { TcpListener listener = null; if (!_listeners.ContainsKey(port)) { listener = new TcpListener(IPAddress.Loopback, port); _listeners[port] = listener; } else { listener = _listeners[port]; } listener.Start(); while (!IsConnected) { Socket client = await listener .AcceptSocketAsync().ConfigureAwait(false); HNode remote = Remote; var local = new HNode(client); if (remote == null) { remote = await HNode.ConnectAsync( Address, port).ConfigureAwait(false); } await InterceptClientDataAsync( local, remote).ConfigureAwait(false); } } catch { /* Swallow exceptions. */ } }
public ExtensionBridge(HNode externalContractor, ExtensionForm extension) { _extension = extension; _externalContractor = externalContractor; RequestInformationAsync().Wait(); Task readTask = ReadMessageAsync(); }
/// <summary> /// Returns a <see cref="HNode"/> that was intercepted on the specified port in an asynchronous operation. /// </summary> /// <param name="port">The port to listen to for local connection attempts.</param> /// <returns></returns> public static async Task <HNode> InterceptAsync(int port) { var listener = new TcpListener(IPAddress.Loopback, port); listener.Start(); var node = new HNode(await listener.AcceptSocketAsync()); listener.Stop(); return(node); }
private async Task InterceptClientDataAsync(HNode local, HNode remote, ushort port) { try { byte[] buffer = await local.PeekAsync(6) .ConfigureAwait(false); if (buffer == null || buffer.Length < 4) { Remote = remote; return; } if (BigEndian.ToUInt16(buffer, 4) == Outgoing.CLIENT_CONNECT) { Port = port; Local = local; Remote = remote; IsConnected = true; OnConnected(EventArgs.Empty); Task readOutgoingTask = ReadOutgoingAsync(); Task readIncomingTask = ReadIncomingAsync(); } else { buffer = await local.ReceiveAsync(1024).ConfigureAwait(false); await remote.SendAsync(buffer).ConfigureAwait(false); buffer = await remote.ReceiveAsync(1024).ConfigureAwait(false); await local.SendAsync(buffer).ConfigureAwait(false); } } catch { /* Swallow all exceptions. */ } finally { if (Local != local) { local.Dispose(); } if (Remote != remote) { remote.Dispose(); } } }
/// <summary> /// Intercepts the attempted connection on the specified port, and establishes a connection with the host in an asynchronous operation. /// </summary> /// <param name="host">The host to establish a connection with.</param> /// <param name="port">The port to intercept the local connection attempt.</param> /// <returns></returns> public async Task ConnectAsync(string host, int port) { RestoreHosts(); while (true) { File.AppendAllText(_hostsFile, $"127.0.0.1\t\t{host}\t\t#Sulakore"); Local = await HNode.InterceptAsync(port).ConfigureAwait(true); RestoreHosts(); Remote = await HNode.ConnectAsync(host, port).ConfigureAwait(false); byte[] buffer = new byte[6]; int length = await Local.ReceiveAsync(buffer, 0, buffer.Length) .ConfigureAwait(false); if (BigEndian.DecypherShort(buffer, 4) == Outgoing.CLIENT_CONNECT) { IsConnected = true; OnConnected(EventArgs.Empty); byte[] packet = new byte[BigEndian.DecypherInt(buffer) + 4]; Buffer.BlockCopy(buffer, 0, packet, 0, 6); await Local.ReceiveAsync(packet, 6, packet.Length - 6) .ConfigureAwait(false); HandleOutgoing(packet, ++TotalOutgoing); ReadIncomingAsync(); break; } byte[] newBuffer = new byte[1000]; Buffer.BlockCopy(buffer, 0, newBuffer, 0, 6); length = await Local.ReceiveAsync(newBuffer, 6, newBuffer.Length - 6) .ConfigureAwait(false); await Remote.SendAsync(newBuffer, 0, length + 6) .ConfigureAwait(false); int inLength = await Remote.ReceiveAsync(newBuffer, 0, newBuffer.Length) .ConfigureAwait(false); await Local.SendAsync(newBuffer, 0, inLength) .ConfigureAwait(false); } }
private void HandleExecutions(IList <HMessage> executions) { var executeTaskList = new List <Task <int> >(executions.Count); for (int i = 0; i < executions.Count; i++) { byte[] executionData = executions[i].ToBytes(); HNode node = (executions[i].Destination == HDestination.Server ? Remote : Local); executeTaskList.Add( node.SendAsync(executionData)); } Task.WhenAll(executeTaskList).Wait(); }
private async Task<int> SendPacketInputAsync(HNode node) { try { MainUI.Cursor = Cursors.WaitCursor; string errorMessage = string.Empty; string packetTxt = MainUI.ITPacketTxt.Text; byte[] data = HMessage.ToBytes(packetTxt); if (data.Length < 6) { errorMessage = NOT_ENOUGH_DATA; errorMessage += $"\r\n\r\nYou're missing {6 - data.Length:#,##0} bytes."; } else { int realLength = data.Length - 4; int length = BigEndian.ToSI32(data); if (realLength != length) { bool tooSmall = length > realLength; int difference = Math.Abs(realLength - length); errorMessage = INVALID_PACKET_LENGTH; errorMessage += $"\r\n\r\nYou're {difference:#,##0} byte(s) too {(tooSmall ? "short" : "large")}."; } else { Task<int> sendTask = node.SendAsync(data); if (!MainUI.ITPacketTxt.Items.Contains(packetTxt)) MainUI.ITPacketTxt.Items.Add(packetTxt); return await sendTask; } } MessageBox.Show(errorMessage, "Tanji ~ Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return 0; } finally { MainUI.Cursor = Cursors.Default; } }
public static async Task <HNode> ConnectNewAsync(IPEndPoint endpoint) { HNode remote = null; try { remote = new HNode(); await remote.ConnectAsync(endpoint).ConfigureAwait(false); } catch { remote = null; } finally { if (!remote?.IsConnected ?? false) { remote = null; } } return(remote); }
private async Task InterceptClientAsync(ushort port) { try { TcpListener listener = _listeners[port]; listener.Start(); while (!IsConnected) { Socket client = await listener .AcceptSocketAsync().ConfigureAwait(false); var local = new HNode(client); var remote = Remote ?? (await HNode.ConnectAsync( Address, port).ConfigureAwait(false)); await InterceptClientDataAsync(local, remote, port).ConfigureAwait(false); } } catch { /* Swallow exceptions. */ } }
/// <summary> /// Returns a <see cref="HNode"/> that was intercepted on the specified port in an asynchronous operation. /// </summary> /// <param name="port">The port to listen to for local connection attempts.</param> /// <returns></returns> public static async Task<HNode> InterceptAsync(int port) { var listener = new TcpListener(IPAddress.Loopback, port); listener.Start(); var node = new HNode(await listener.AcceptSocketAsync()); listener.Stop(); return node; }
private void HandleMessage(HMessage packet, int count, Func <Task> continuation, HNode node, Action <DataInterceptedEventArgs> eventRaiser) { var args = new DataInterceptedEventArgs(packet, count, continuation); eventRaiser(args); if (!args.IsBlocked) { node.SendAsync(args.Packet).Wait(); HandleExecutions(args.Executions); } if (!args.HasContinued) { args.Continue(); } }
private async Task InterceptClientDataAsync(HNode local, HNode remote, ushort port) { try { byte[] buffer = await local.PeekAsync(6) .ConfigureAwait(false); if (buffer == null || buffer.Length < 4) { Remote = remote; return; } if (BigEndian.ToUInt16(buffer, 4) == Outgoing.CLIENT_CONNECT) { Port = port; Local = local; Remote = remote; IsConnected = true; OnConnected(EventArgs.Empty); Task readOutgoingTask = ReadOutgoingAsync(); Task readIncomingTask = ReadIncomingAsync(); } else { buffer = await local.ReceiveAsync(1024).ConfigureAwait(false); await remote.SendAsync(buffer).ConfigureAwait(false); buffer = await remote.ReceiveAsync(1024).ConfigureAwait(false); await local.SendAsync(buffer).ConfigureAwait(false); } } catch { /* Swallow all exceptions. */ } finally { if (Local != local) local.Dispose(); if (Remote != remote) remote.Dispose(); } }