// we come here when we succesfully authenticated to a peer server private void OnServerAuthenticated(NodeSession session, IPEndPoint local, IPEndPoint remote) { if (session.Persona.Thumbprint.Equals(nodeManager.Self.Thumbprint)) { var selfPeer = peerManager.GetOrCreatePeerByPersona(session.Persona, remote); nodeManager.UpdateSelfEndpoint(selfPeer.Peer.Node.EndPoint); CloseSession(session, DisconnectReason.ConnectedToSelf); return; } var peer = peerManager.GetOrCreatePeerByPersona(session.Persona, remote); //TODO as far as peerManager created a new peer everytime, we need to update it in serverManager. May be it's not the best place nodeManager.UpdateNode(remote, peer); if (!TrySetSession(session, peer)) { return; } // send the url of the server OpenDiscoveryChannel(peer); // TODO we should only send that when requested by the peer SendPort(peer, remote); }
public void Disconnect() { using (locker.CreateLock()) { Debug.Assert(PeerState == PeerState.Disconnected); nodeSession = null; } }
public Client(IPEndPoint endpoint, X509Certificate2 certificate, ILogger logger) { this.logger = logger; this.endpoint = endpoint; client = new TcpClient(); TcpSession = new TcpSession(client, certificate, Authenticate, logger, false); NodeSession = new NodeSession(TcpSession, logger); }
private bool TrySetSession(NodeSession session, PeerSession peer) { if (peer.TrySetSession(session)) { return(true); } session.Disconnect(DisconnectReason.CannotSetSession); return(false); }
private void ServerListenLoop() { while (true) { var client = listener.AcceptTcpClient(); tcp = new TcpSession(client, configuration.Certificate, Authenticate, logger, true); var session = new NodeSession(tcp, logger); session.OnAuthenticated += (local, remote) => OnClientAuthenticated(session); // TODO handle connection state changed session.Run(); } }
// we come here when we receive a client authenticated on our server private void OnClientAuthenticated(NodeSession session) { if (connectionsManager.IsReachedMaxPeerLimit()) { CloseSession(session, DisconnectReason.TooManyPeers); return; } if (session.Persona.Thumbprint.Equals(nodeManager.Self.Thumbprint)) { CloseSession(session, DisconnectReason.ClientWithSameThumbprint); return; } // get identity of the peer // get or create peer session var peer = peerManager.GetOrCreatePeerByPersona(session.Persona, null); if (peer.Peer.Node.HasServer) // If it's someone whom we don't know, then we need to update the state on HandleServerPort() { nodeManager.UpdateNode(peer.Peer.Node.EndPoint, peer); var node = nodeManager.GetByEndpoint(peer.Peer.Node.EndPoint); session.OnDisconnected += node.Release; if (node.IsAvailable) { node.Acquire(); } } if (peer.PeerState == PeerState.Connected) { CloseSession(session, DisconnectReason.PeerAlreadyConnected); return; } // we will register peer end point when he sends it // if already connected, close connection if (!TrySetSession(session, peer)) { return; } OpenDiscoveryChannel(peer); var endpoints = nodeManager.GetEndPointTable(session.RemoteEndPoint); if (endpoints.Count > 0) { peer.Send(DISCOVER_CHANNEL, discovery.EndPointTable(endpoints)); } }
// set websocket when it is already opened internal bool TrySetSession(NodeSession session) { using (locker.CreateLock()) { if (nodeSession != null) { return(false); } Debug.Assert(PeerState != PeerState.Connected); Debug.Assert(nodeSession == null); nodeSession = session; nodeSession.Connect(this); OnConnected(); return(true); } }
private void CloseSession(NodeSession session, DisconnectReason reason) { session.Disconnect(reason); }