コード例 #1
0
    public static Task <NetworkMessageMultiStream> Accept(
        int port,
        MonoBehaviour parent,
        MessageTransferLookUp lookUp
        )
    {
        IPAddress localAddr   = IPAddress.Parse("0.0.0.0");
        var       server      = new CancelableTcpListener(new ActivatableTCPListener(localAddr, port));
        var       taskSource  = new TaskCompletionSource <NetworkMessageMultiStream>();
        var       crossThread = new InboundCrossThread(taskSource, parent, server.GetCancellationTokenSource());

        Debug.Log("Delegating server port: " + port);
        Task.Factory
        .StartNew(() => Connect(server, crossThread, lookUp), server.GetCancellationTokenSource().Token)
        .ContinueWith((task) => {
            if (task.IsCanceled)
            {
                Debug.Log("Server dispatch has been shutdown");
            }
            else if (task.IsFaulted)
            {
                Debug.LogError("Exception in connection accept thread");
                Debug.LogError(task.Exception);
            }
            else
            {
                Debug.Log("not sure how this task shutdown");
            }
        });
        return(taskSource.Task);
    }
コード例 #2
0
    private static void Connect(
        CancelableTcpListener server,
        InboundCrossThread crossThread,
        MessageTransferLookUp lookUp
        )
    {
        InboundMultiMessageSender       sender       = new InboundMultiMessageSender();
        InboundMultiMessageSubscription subscription = new InboundMultiMessageSubscription(server.GetCancellationTokenSource());

        server.Start();
        Debug.Log("Server dispatching connections");
        crossThread.AcceptStream(new NetworkMessageMultiStream(sender.GetSender(), subscription));
        while (true)
        {
            var guid = server.Accept();
            Debug.Log("Server connection established: " + guid);
            var client = server.GetClient(guid);
            crossThread.Schedule((parent) => {
                var clientSender       = ConnectionWrite.Write(client, lookUp);
                var clientSubscription = ConnectionRead.Read(client, parent, lookUp);
                sender.AddClientSender(guid, clientSender);
                subscription.AddClientSubscription(guid, clientSubscription);
            });
        }
    }