Example #1
0
        public static async Task <TcpTwoPartyNetworkSession[]> AcceptAsync(Party localParty, IPEndPoint localEndPoint, int numberOfSessions)
        {
            TcpListener listener = new TcpListener(localEndPoint)
            {
                ExclusiveAddressUse = true
            };

            listener.Start();
            try
            {
                TcpTwoPartyNetworkSession[] sessions = new TcpTwoPartyNetworkSession[numberOfSessions];
                for (int i = 0; i < numberOfSessions; ++i)
                {
                    TcpClient client = await listener.AcceptTcpClientAsync();

                    sessions[i] = CreateFromPartyInformationExchange(localParty, client);
                }

                return(sessions);
            }
            finally
            {
                listener.Stop();
            }
        }
        public static async Task <TcpMultiPartyNetworkSession> EstablishAsync(Party localParty, IPAddress address, int startPort, int numberOfParties)
        {
            TcpTwoPartyNetworkSession[] remotePartySessions = new TcpTwoPartyNetworkSession[numberOfParties - 1];

            for (int i = 0; i < localParty.Id; ++i)
            {
                remotePartySessions[i] = await TcpTwoPartyNetworkSession.ConnectAsync(localParty, address, startPort + i);
            }

            TcpListener listener = new TcpListener(IPAddress.Any, startPort + localParty.Id)
            {
                ExclusiveAddressUse = true
            };

            listener.Start();

            for (int j = localParty.Id + 1; j < numberOfParties; ++j)
            {
                remotePartySessions[j - 1] = await TcpTwoPartyNetworkSession.AcceptAsync(localParty, listener);
            }

            listener.Stop();

            for (int i = 0; i < numberOfParties; ++i)
            {
                if (i != localParty.Id && !remotePartySessions.Any(session => session.RemoteParty.Id == i))
                {
                    throw new NetworkConsistencyException("Inconsistent TCP connection graph.");
                }
            }

            return(new TcpMultiPartyNetworkSession(localParty, remotePartySessions));
        }
Example #3
0
        public static async Task <TcpMultiPartyNetworkSession> EstablishAsync(Party localParty, IPEndPoint[] endPoints)
        {
            if (localParty.Id < 0 || localParty.Id >= endPoints.Length)
            {
                throw new ArgumentException($"Local party id {localParty.Id} is out of range.");
            }

            IPEndPoint localEndPoint = endPoints[localParty.Id];

            TcpTwoPartyNetworkSession[] outgoingSessions = await TcpTwoPartyNetworkSession.ConnectAsync(
                localParty,
                endPoints,
                localParty.Id
                );

            TcpTwoPartyNetworkSession[] incomingSessions = await TcpTwoPartyNetworkSession.AcceptAsync(
                localParty,
                localEndPoint,
                endPoints.Length - localParty.Id - 1
                );

            List <ITwoPartyNetworkSession> remotePartySessions = new List <ITwoPartyNetworkSession>(endPoints.Length - 1);

            remotePartySessions.AddRange(outgoingSessions);
            remotePartySessions.AddRange(incomingSessions);

            for (int i = 0; i < endPoints.Length; ++i)
            {
                if (i != localParty.Id && remotePartySessions.All(session => session.RemoteParty.Id != i))
                {
                    throw new NetworkConsistencyException("Inconsistent TCP connection graph.");
                }
            }

            return(new TcpMultiPartyNetworkSession(localParty, remotePartySessions));
        }