private void HandlePong(PeerSession session) { if (!session.OnPong()) { ; // problem } }
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"); } } }
private void Ping(PeerSession session) { if (session.OnPing()) { session.Send(DISCOVER_CHANNEL, discovery.Ping()); } }
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); }
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); }
private bool TrySetSession(NodeSession session, PeerSession peer) { if (peer.TrySetSession(session)) { return(true); } session.Disconnect(DisconnectReason.CannotSetSession); return(false); }
private void OpenDiscoveryChannel(PeerSession peer) { foreach (var channel in channels.Values) { if (channel == DISCOVER_CHANNEL) { peer.OpenChannel(channel); } } }
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); } }
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); }
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)); } } }
// 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); } } }
private void HandlePing(PeerSession session) { session.Send(DISCOVER_CHANNEL, discovery.Pong()); }