/// <summary> /// Creates a new proxy between 2 targets. /// </summary> /// <param name="a">Point A.</param> /// <param name="b">Point B.</param> public Proxy(HostTarget a, HostTarget b) { A = a; B = b; ProxyServer = new ActiveEndPoint <T>(B); ProxyServer.SessionSpawned += ProxyServer_SessionSpawned; }
/// <summary> /// Handles this server sessions spawn. /// </summary> /// <param name="sender">Local session.</param> /// <param name="e">Formal junk.</param> private void RoutingServer_SessionSpawned(object sender, EventArgs e) { var session = sender as ActiveSession <T>; session.End += Session_End; session.IsClientSide = true; session.ExceptionThrown += AllTargets_ExceptionThrown; session.MessageReceived += Session_MessageReceived; var servers = new List <ActiveEndPoint <T> >(); for (int i = 0, n = Targets.Length; i < n; i++) { var remote = new ActiveEndPoint <T>(Targets[i], session) { Index = i }; remote.ExceptionThrown += AllTargets_ExceptionThrown; remote.MessageReceived += Remote_MessageReceived; remote.SessionClosed += Remote_SessionClosed; servers.Add(remote); } session.Remotes = servers.ToArray(); session.Route = RouteDefault; session.IsBroadcast = IsBroadcast; SessionStarted?.Invoke(session, EventArgs.Empty); foreach (var server in session.Remotes) { server.Connect(); } if (!session.Remotes.All(i => i.IsConnected)) { session.Close(); } }
/// <summary> /// Creates a new session. /// </summary> /// <param name="owner">Owner end point.</param> /// <param name="socket">Session socket.</param> /// <param name="id">Session identifier.</param> public ActiveSession(ActiveEndPoint <T> owner, Socket socket, int id) { Owner = owner; Id = id; Socket = socket; Stream = new NetworkStreamEx(socket, Owner.Host.AuthenticationData); Stream.Authenticate(); // TODO: make authentication optional for StartTLS support if (typeof(T) == typeof(byte[])) { Transceiver = new BinaryTransceiver() as ITransceiver <T>; } else if (typeof(T) == typeof(BinaryPacket)) { Transceiver = new FastBinaryTransceiver() as ITransceiver <T>; } else if (typeof(T) == typeof(X690.Message)) { Transceiver = new X690Transceiver() as ITransceiver <T>; } else if (typeof(T) == typeof(string)) { Transceiver = new StringTransceiver() as ITransceiver <T>; } else { throw new InvalidDataException("Unsupported message type."); } }
/// <summary> /// Creates a new end point router from common target to multiple remote targets. /// </summary> /// <param name="common">Common end.</param> /// <param name="targets">Remote targets.</param> public Router(HostTarget common, params HostTarget[] targets) { Common = common; Targets = targets; RoutingServer = new ActiveEndPoint <T>(Common) { Index = -1 }; RoutingServer.ExceptionThrown += AllTargets_ExceptionThrown; RoutingServer.SessionSpawned += RoutingServer_SessionSpawned; }
/// <summary> /// Handles this server sessions spawn. /// </summary> /// <param name="sender">Local session.</param> /// <param name="e">Formal junk.</param> private void ProxyServer_SessionSpawned(object sender, EventArgs e) { var session = sender as ActiveSession <T>; session.End += Session_End; session.IsClientSide = true; session.ExceptionThrown += Session_ExceptionThrown; session.MessageReceived += Session_MessageReceived; var remote = new ActiveEndPoint <T>(A, session); remote.MessageReceived += Remote_MessageReceived; remote.ExceptionThrown += Session_ExceptionThrown; session.Remotes = new[] { remote }; SessionStarted?.Invoke(this, EventArgs.Empty); remote.Connect(); if (!remote.IsConnected) { session.Close(); } }