public async Task <LoopSession> Start() { int?port; ReceiverService loop = new ReceiverBuilder() .WithDefinition(new LoopMessages()) .Build(hooks); TcpSocket client = pool.New(); TcpSocket server = pool.New(); client.Bind(); server.Bind(out port); server.Listen(1); IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, port.Value); Task <TcpSocketAccept> accept = server.Accept(); client.Connect(endpoint, null); PeerHash peer = PeerHash.Random(); TcpSocketAccept accepted = await accept; NetworkConnection receiver = pool.Create(accepted.Connection, NetworkDirection.Incoming, accepted.GetRemote()); loop.StartProcessing(peer, receiver); return(new LoopSession(client, loop)); }
public async Task CanHandleTerminatedStream() { IPAddress localhost = IPAddress.Loopback; IPEndPoint endpoint = new IPEndPoint(localhost, 1234); using (CompletionThread worker = new CompletionThread()) { SocketFactory factory = new SocketFactory(worker); using (TcpSocket server = factory.Tcp()) using (TcpSocket socket = factory.Tcp()) { socket.Bind(); worker.Start(); server.Bind(endpoint.Port); server.Listen(1); Task <TcpSocketAccept> acceptable = server.Accept(); await socket.Connect(endpoint); TcpSocketAccept accepted = await acceptable; accepted.Connection.Dispose(); byte[] buffer = new byte[10]; TcpSocketSend sent = await socket.Send(buffer); Assert.That(sent.Status, Is.EqualTo(SocketStatus.OK)); } } }
public async Task ShouldTriggerConnectionSentWhenSentSomeBytes() { NetworkDirection direction = NetworkDirection.Outgoing; NetworkOutgoingMessage message = new RandomMessage(113); using (NetworkFixture fixture = new NetworkFixture()) using (TcpSocket host = fixture.Pool.New()) using (TcpSocket socket = fixture.Pool.New()) { TcpSocketInfo info = host.BindAndInfo(); int port = info.Endpoint.Port; IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, port); socket.Bind(); host.Listen(10); host.Accept(null); await socket.Connect(endpoint); NetworkConnection connection = fixture.Pool.Create(socket, direction, endpoint); Trigger handler = Trigger.Bind(ref fixture.Hooks.OnConnectionSent, data => { data.Remote.Should().Be(NetworkAddress.Parse(endpoint)); data.Connection.Should().Be(connection); data.Bytes.Should().Be(message.Length); }); connection.Send(message); handler.Wait().Should().BeTrue(); } }
void Update() { try { var remoteSocket = tcpSocket.Accept(); if (remoteSocket != null) { print($"remoteSocket: {remoteSocket.Socket.RemoteEndPoint}"); this.remoteSocket = remoteSocket; } } catch (TcpSocketException e) { print(e.Message); } //if (this.remoteSocket != null) //{ // byte[] message; // while (messageBuffer.TryReceiveMessage(this.remoteSocket, out message)) // { // var viewerStateJson = Encoding.ASCII.GetString(message); // var viewerState = JsonUtility.FromJson<ViewerState>(viewerStateJson); // //print($"this.remoteSocket.Socket.RemoteEndPoint: {this.remoteSocket.Socket.RemoteEndPoint}"); // viewerStateText.text = $"end point: {this.remoteSocket.Socket.RemoteEndPoint}\nuser ID: {viewerState.userId}"; // } //} }
public async Task CanObtainAcceptedRemoteEndpointUsingTasks() { using (CompletionThread worker = new CompletionThread()) { IPEndPoint endpoint; SocketFactory factory = new SocketFactory(worker); using (TcpSocket server = factory.Tcp()) using (TcpSocket client = factory.Tcp()) { worker.Start(); client.Bind(); server.Bind(IPAddress.Loopback); server.Listen(1); endpoint = server.Info().Endpoint; Task <TcpSocketAccept> accepting = server.Accept(); await client.Connect(endpoint); TcpSocketAccept accepted = await accepting; IPEndPoint remote = accepted.GetRemote(); Assert.That(remote.Address, Is.EqualTo(endpoint.Address)); Assert.That(remote.Port, Is.Not.EqualTo(endpoint.Port)); Assert.That(remote.Port, Is.Not.Zero); } } }
public async void StartListening() { AcceptConnectionCancellationTokenSource = new CancellationTokenSource(); AcceptConnectionCancellationToken = AcceptConnectionCancellationTokenSource.Token; while (AcceptConnectionCancellationToken.IsCancellationRequested == false) { Socket clientSocket = TcpSocket.Accept(); Client client = await CreateNewInstance(clientSocket); Task.Factory.StartNew(() => base.ReceiveMessage()).ContinueWith(client.CommandReceived); } }
void Update() { try { var remoteSocket = tcpSocket.Accept(); if (remoteSocket != null) { print($"remoteSocket: {remoteSocket.RemoteEndPoint}"); var serverSocket = new ControllerServerSocket(remoteSocket); serverSockets.Add(serverSocket); viewerScenes.Add(serverSocket, new ViewerScene()); socketTimers.Add(serverSocket, Stopwatch.StartNew()); } } catch (TcpSocketException e) { print(e.Message); } foreach (var serverSocket in serverSockets) { while (true) { var viewerState = serverSocket.ReceiveViewerState(); if (viewerState == null) { break; } viewerStates[serverSocket] = viewerState; socketTimers[serverSocket] = Stopwatch.StartNew(); } } var timedOutSockets = new List <ControllerServerSocket>(); foreach (var socketTimerPair in socketTimers) { if (socketTimerPair.Value.Elapsed.TotalSeconds > TIME_OUT_SEC) { timedOutSockets.Add(socketTimerPair.Key); } } foreach (var timedOutSocket in timedOutSockets) { serverSockets.Remove(timedOutSocket); viewerStates.Remove(timedOutSocket); viewerScenes.Remove(timedOutSocket); socketTimers.Remove(timedOutSocket); } }
public async Task CanHandleNotBoundSocket() { using (CompletionThread worker = new CompletionThread()) { SocketFactory factory = new SocketFactory(worker); using (TcpSocket socket = factory.Tcp()) { TcpSocketAccept accepted = await socket.Accept(); Assert.That(accepted.Status, Is.Not.EqualTo(SocketStatus.OK)); } } }
public void Start() { assignedPort = configuration.Port.Bind(socket); if (assignedPort == null) { hooks.CallListenerFailed(configuration, assignedPort.Value, $"Binding to the requested port failed."); } else { socket.Listen(8); socket.Accept(OnAccept); hooks.CallListenerStarted(configuration, assignedPort.Value); } }
public async Task ShouldTriggerConnectionReceivedWhenReceivedSomeBytes() { NetworkDirection direction = NetworkDirection.Outgoing; NetworkOutgoingMessage message = new RandomMessage(113); using (NetworkFixture fixture = new NetworkFixture()) using (TcpSocket host = fixture.Pool.New()) using (TcpSocket socket = fixture.Pool.New()) { TcpSocketInfo info = host.BindAndInfo(); int port = info.Endpoint.Port; IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, port); socket.Bind(); host.Listen(10); Task <TcpSocketAccept> task = host.Accept(); await socket.Connect(endpoint); TcpSocketAccept accept = await task; NetworkConnection connection = fixture.Pool.Create(socket, direction, endpoint); NetworkBlock block = new NetworkBlock(new byte[1024], 0, message.Length); Trigger handler = Trigger.Bind(ref fixture.Hooks.OnConnectionReceived, data => { data.Remote.Should().Be(NetworkAddress.Parse(endpoint)); data.Connection.Should().NotBeNull(); data.Bytes.Should().Be(message.Length); }); connection.Receive(new NullReceiver()); message.ToBytes(block); block.With((buffer, offset, count) => { accept.Connection.Send(new SocketBuffer(buffer, offset, count), null); }); handler.Wait().Should().BeTrue(); } }
public async Task ShouldTriggerConnectionTerminatedWhenSending() { NetworkDirection direction = NetworkDirection.Outgoing; using (NetworkFixture fixture = new NetworkFixture()) using (TcpSocket host = fixture.Pool.New()) using (TcpSocket socket = fixture.Pool.New()) { TcpSocketInfo info = host.BindAndInfo(); int port = info.Endpoint.Port; IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, port); socket.Bind(); host.Listen(10); Task <TcpSocketAccept> task = host.Accept(); TcpSocketConnect connect = await socket.Connect(endpoint); TcpSocketAccept accept = await task; connect.Status.Should().Be(SocketStatus.OK); accept.Status.Should().Be(SocketStatus.OK); accept.Connection.Dispose(); NetworkConnection connection = fixture.Pool.Create(socket, direction, endpoint); Trigger handler = Trigger.Bind(ref fixture.Hooks.OnConnectionTerminated, data => { data.Remote.Should().Be(NetworkAddress.Parse(endpoint)); data.Connection.Should().NotBeNull(); }); for (int i = 0; i < 10; i++) { connection.Send(new OneByteMessage()); } handler.Wait().Should().BeTrue(); } }
public async Task <NegotiatorFixturePair> Create() { int?port; TcpSocket host = pool.New(); TcpSocket client = pool.New(); client.Bind(); host.Bind(out port); host.Listen(1); Task <TcpSocketAccept> accept = host.Accept(); Task <TcpSocketConnect> connect = client.Connect(port.Value); TcpSocketAccept accepted = await accept; TcpSocketConnect connected = await connect; NetworkConnection local = pool.Create(connected.Socket, NetworkDirection.Outgoing, connected.Endpoint); NetworkConnection remote = pool.Create(accepted.Connection, NetworkDirection.Incoming, accepted.GetRemote()); return(new NegotiatorFixturePair(local, remote)); }
public void Start() { socket.Listen(10); socket.Accept(OnAccepted); }