コード例 #1
0
 private void HandlePong(PeerSession session)
 {
     if (!session.OnPong())
     {
         ; // problem
     }
 }
コード例 #2
0
        public void HandleMessage(PeerSession peer, byte[] message)
        {
            using (var stream = new MemoryStream(message))
            {
                switch (stream.ReadByte())
                {
                // get the server endpoint of the peer
                case (int)MessageType.ServerPort:
                    var port = ReadInt(stream);
                    if (port <= 0)
                    {
                        return;
                    }
                    OnServerPort.Call(peer, port);
                    break;

                // get the server endpoints known by the peer
                case (int)MessageType.EndPointTable:
                    var endpoints = ReadEndPointTable(stream);
                    OnEndPointTable.Call(peer, endpoints);
                    break;

                case (int)MessageType.Ping:
                    OnPing.Call(peer);
                    break;

                case (int)MessageType.Pong:
                    OnPong.Call(peer);
                    break;

                default:
                    throw new ProtocolViolationException("Discovery Protocol doesnt know this message type");
                }
            }
        }
コード例 #3
0
 private void Ping(PeerSession session)
 {
     if (session.OnPing())
     {
         session.Send(DISCOVER_CHANNEL, discovery.Ping());
     }
 }
コード例 #4
0
        private void HandleServerPort(PeerSession peer, int port)
        {
            var address  = peer.RemoteEndPoint.Address;
            var endpoint = new IPEndPoint(address, port);

            peer.SetNodeEndPoint(endpoint);
            nodeManager.UpdateNode(endpoint, peer);
        }
コード例 #5
0
        private PeerSession CreatePeer(IPEndPoint server, Persona persona)
        {
            var peerSession = new PeerSession(new Peer(new Node(server), persona), channels);

            peerSession.OnConnected += () => OnPeerConnected(peerSession);
            peerSession.OnClosed    += () => OnPeerDisconnected(peerSession);
            peerSession.OnReceived  += (channel, data) => OnReceived(peerSession, channel, data);

            return(peerSession);
        }
コード例 #6
0
        private bool TrySetSession(NodeSession session, PeerSession peer)
        {
            if (peer.TrySetSession(session))
            {
                return(true);
            }

            session.Disconnect(DisconnectReason.CannotSetSession);
            return(false);
        }
コード例 #7
0
 private void OpenDiscoveryChannel(PeerSession peer)
 {
     foreach (var channel in channels.Values)
     {
         if (channel == DISCOVER_CHANNEL)
         {
             peer.OpenChannel(channel);
         }
     }
 }
コード例 #8
0
 private void OnReceived(PeerSession peer, IChannel channel, byte[] message)
 {
     // we cheat
     if (channel.GetChannelByte() == DISCOVER_CHANNEL.GetChannelByte())
     {
         discovery.HandleMessage(peer, message);
     }
     else
     {
         channel.OnReceived(peer, message);
     }
 }
コード例 #9
0
        private void HandleEndPointTable(PeerSession session, List <IPEndPoint> endpoints)
        {
            IEnumerable <IPEndPoint> list = endpoints;

            if (nodeManager.ForwardedPort > 0)
            {
                // TODO THIS works only on local networks. Need to resolve the public u
                // nodeManager.UpdateSelfEndpoint(new IPEndPoint(session.LocalEndPoint.Address, nodeManager.ServerPort));
            }

            nodeManager.RegisterEndPoints(list);
        }
コード例 #10
0
 private void SendPort(PeerSession peer, IPEndPoint remote)
 {
     // handle the case where we are on local network and need to send the local port instead of the forwarded port
     if (remote.Address.IsPrivate())
     {
         peer.Send(DISCOVER_CHANNEL, discovery.ServerPort(nodeManager.ServerPort));
         return;
     }
     else
     {
         var port = nodeManager.ForwardedPort;
         if (port != 0)
         {
             peer.Send(DISCOVER_CHANNEL, discovery.ServerPort(port));
         }
     }
 }
コード例 #11
0
 // TODO there is a problem when you have a connection with a node that did not register yet
 // the current node session needs to acquire the new node entry and register callback to release it when disconnected
 public void UpdateNode(IPEndPoint endpoint, PeerSession peerSession)
 {
     using (locker.CreateLock())
     {
         if (nodes.ContainsKey(endpoint))
         {
             if (peerSession.Peer.Node.HasServer)
             {
                 nodes[endpoint].Node.SetEndPoint(peerSession.Peer.Node.EndPoint);
             }
         }
         else
         {
             RegisterEndPoint(endpoint);
         }
     }
 }
コード例 #12
0
 private void HandlePing(PeerSession session)
 {
     session.Send(DISCOVER_CHANNEL, discovery.Pong());
 }