/// <summary> /// Initiates a connection to the router. /// </summary> /// <returns></returns> private static Task <SimpleSocket> InitiateConnectionToRouter() { var socketContextTCS = new TaskCompletionSource <SimpleSocket>(); var socketContext = new SimpleSocket(); socketContext.Connected = async context => { socketContextTCS.TrySetResult(context); }; Task.Run(async() => { try { // If connecting as a client, try once, otherwise try to listen multiple time (in case port is shared) switch (ConnectionMode) { case RouterConnectionMode.Connect: await socketContext.StartClient("127.0.0.1", DefaultPort); break; case RouterConnectionMode.Listen: await socketContext.StartServer(DefaultListenPort, true, 10); break; case RouterConnectionMode.ConnectThenListen: bool clientException = false; try { await socketContext.StartClient("127.0.0.1", DefaultPort); } catch (Exception) // Ideally we should filter SocketException, but not available on some platforms (maybe it should be wrapped in a type available on all paltforms?) { clientException = true; } if (clientException) { await socketContext.StartServer(DefaultListenPort, true, 10); } break; default: throw new ArgumentOutOfRangeException(); } } catch (Exception e) { Log.Error("Could not connect to connection router using mode {0}: {1}", ConnectionMode, e.Message); throw; } }); // Wait for server to connect to us (as a Task) return(socketContextTCS.Task); }
private SimpleSocket CreateSocketContext() { var socketContext = new SimpleSocket(); socketContext.Connected = (clientSocketContext) => { Task.Run(async () => { try { // Register service server await socketContext.WriteStream.WriteInt16Async((short)RouterMessage.ServiceProvideServer); await socketContext.WriteStream.WriteStringAsync(serverUrl); await socketContext.WriteStream.FlushAsync(); while (true) { var routerMessage = (RouterMessage)await socketContext.ReadStream.ReadInt16Async(); switch (routerMessage) { case RouterMessage.ServiceRequestServer: { var requestedUrl = await clientSocketContext.ReadStream.ReadStringAsync(); var guid = await clientSocketContext.ReadStream.ReadGuidAsync(); // Spawn actual server var realServerSocketContext = new SimpleSocket(); realServerSocketContext.Connected = async (clientSocketContext2) => { // Write connection string await clientSocketContext2.WriteStream.WriteInt16Async((short)RouterMessage.ServerStarted); await clientSocketContext2.WriteStream.WriteGuidAsync(guid); // Delegate next steps to actual server HandleClient(clientSocketContext2, requestedUrl); }; // Start connection await realServerSocketContext.StartClient(address, port); break; } default: Console.WriteLine("Router: Unknown message: {0}", routerMessage); throw new ArgumentOutOfRangeException(); } } } catch (Exception e) { // TODO: Ideally, separate socket-related error messages (disconnection) from real errors // Unfortunately, it seems WinRT returns Exception, so it seems we can't filter with SocketException/IOException only? Log.Info("Client {0}:{1} disconnected with exception: {2}", clientSocketContext.RemoteAddress, clientSocketContext.RemotePort, e.Message); clientSocketContext.Dispose(); } }); }; return socketContext; }
/// <summary> /// Initiates a connection to the router. /// </summary> /// <returns></returns> private static Task<SimpleSocket> InitiateConnectionToRouter() { var socketContextTCS = new TaskCompletionSource<SimpleSocket>(); var socketContext = new SimpleSocket(); socketContext.Connected = async context => { socketContextTCS.TrySetResult(context); }; Task.Run(async () => { // If connecting as a client, try once, otherwise try to listen multiple time (in case port is shared) switch (ConnectionMode) { case RouterConnectionMode.Connect: await socketContext.StartClient("127.0.0.1", DefaultPort); break; case RouterConnectionMode.Listen: await socketContext.StartServer(DefaultListenPort, true, 10); break; case RouterConnectionMode.ConnectThenListen: bool clientException = false; try { await socketContext.StartClient("127.0.0.1", DefaultPort); } catch (Exception) // Ideally we should filter SocketException, but not available on some platforms (maybe it should be wrapped in a type available on all paltforms?) { clientException = true; } if (clientException) { await socketContext.StartServer(DefaultListenPort, true, 10); } break; default: throw new ArgumentOutOfRangeException(); } }); // Wait for server to connect to us (as a Task) return socketContextTCS.Task; }
private SimpleSocket CreateSocketContext() { var socketContext = new SimpleSocket(); socketContext.Connected = async (clientSocketContext) => { // Register service server await socketContext.WriteStream.WriteInt16Async((short)RouterMessage.ServiceProvideServer); await socketContext.WriteStream.WriteStringAsync(serverUrl); await socketContext.WriteStream.FlushAsync(); while (true) { var routerMessage = (RouterMessage)await socketContext.ReadStream.ReadInt16Async(); switch (routerMessage) { case RouterMessage.ServiceRequestServer: { var requestedUrl = await clientSocketContext.ReadStream.ReadStringAsync(); var guid = await clientSocketContext.ReadStream.ReadGuidAsync(); // Spawn actual server var realServerSocketContext = new SimpleSocket(); realServerSocketContext.Connected = async (clientSocketContext2) => { // Write connection string await clientSocketContext2.WriteStream.WriteInt16Async((short)RouterMessage.ServerStarted); await clientSocketContext2.WriteStream.WriteGuidAsync(guid); // Delegate next steps to actual server HandleClient(clientSocketContext2, requestedUrl); }; // Start connection await realServerSocketContext.StartClient(address, port); break; } default: Console.WriteLine("Router: Unknown message: {0}", routerMessage); throw new ArgumentOutOfRangeException(); } } }; return socketContext; }
/// <summary> /// Initiates a connection to the router. /// </summary> /// <returns></returns> private static Task <SimpleSocket> InitiateConnectionToRouter() { var socketContextTCS = new TaskCompletionSource <SimpleSocket>(); var socketContext = new SimpleSocket(); socketContext.Connected = async context => { socketContextTCS.TrySetResult(context); }; Task.Run(async() => { // Keep trying to establish connections until no errors bool hasErrors; do { try { hasErrors = false; if (PlatformIsPortForward) { await socketContext.StartServer(DefaultListenPort, true); } else { await socketContext.StartClient("127.0.0.1", DefaultPort); } } catch (Exception) { hasErrors = true; } if (hasErrors) { // Wait a little bit before next try await Task.Delay(100); } } while (hasErrors); }); // Wait for server to connect to us (as a Task) return(socketContextTCS.Task); }
/// <summary> /// Initiates a connection to the router. /// </summary> /// <returns></returns> private static Task<SimpleSocket> InitiateConnectionToRouter() { var socketContextTCS = new TaskCompletionSource<SimpleSocket>(); var socketContext = new SimpleSocket(); socketContext.Connected = async context => { socketContextTCS.TrySetResult(context); }; Task.Run(async () => { // Keep trying to establish connections until no errors bool hasErrors; do { try { hasErrors = false; if (PlatformIsPortForward) await socketContext.StartServer(DefaultListenPort, true); else await socketContext.StartClient("127.0.0.1", DefaultPort); } catch (Exception) { hasErrors = true; } if (hasErrors) { // Wait a little bit before next try await Task.Delay(100); } } while (hasErrors); }); // Wait for server to connect to us (as a Task) return socketContextTCS.Task; }