示例#1
0
        public async Task <P2PConnectionImpl> Run()
        {
            try
            {
                m_Socket = new UdpSocket();
                m_Socket.Bind(m_RemoteEP.AddressFamily);
                m_SelfId = Random.GenInt();
                if (m_Cookie == null)
                {
                    var buf = new PeerToPeerHello(m_SelfId, null).Pack();
                    var res = await new TimeoutRetryableRequester <PeerToPeerHello>(WaitHello(), () => Send(buf), m_Token).Run();
                    m_OwnerId = res.ConnectionId;
                    m_Cookie  = res.Cookie;
                }
                {
                    var buf  = new PeerToPeerRequest(m_SelfId, m_Cookie).Pack();
                    var res  = await new TimeoutRetryableRequester <HandshakeResult>(WaitHandshakeAccept(), () => Send(buf), m_Token).Run();
                    var peer = new PeerEntry(m_OwnerId, 0, res.Encryptor, m_RemoteEP);

                    m_Token.ThrowIfCancellationRequested();
                    var impl = new P2PConnectionImpl(m_SelfId, m_Socket, peer, m_EncryptorGenerator);
                    impl.P2PTask.UpdateList(res.List.Peers, true);
                    return(impl);
                }
            }
            catch (Exception)
            {
                m_Socket.Dispose();
                throw;
            }
        }
示例#2
0
        public static P2PHostConnection StartLocalHost(LocalHostConfig config)
        {
            var impl = new P2PConnectionImpl(config);

            return(new P2PHostConnection(impl));
        }