Handle swarm connect
This commit is contained in:
parent
1565b78fcd
commit
56e301df8d
4 changed files with 31 additions and 0 deletions
|
@ -6,3 +6,10 @@
|
|||
* Build a handler that can handle the yamux protocol
|
||||
*/
|
||||
struct Libp2pProtocolHandler* yamux_build_protocol_handler();
|
||||
/***
|
||||
* Send the yamux protocol out the default stream
|
||||
* NOTE: if we initiate the connection, we should expect the same back
|
||||
* @param context the SessionContext
|
||||
* @returns true(1) on success, false(0) otherwise
|
||||
*/
|
||||
int yamux_send_protocol(struct SessionContext* context);
|
||||
|
|
10
peer/peer.c
10
peer/peer.c
|
@ -8,6 +8,7 @@
|
|||
#include "libp2p/secio/secio.h"
|
||||
#include "libp2p/utils/linked_list.h"
|
||||
#include "libp2p/utils/logger.h"
|
||||
#include "libp2p/yamux/yamux.h"
|
||||
|
||||
/**
|
||||
* create a new Peer struct
|
||||
|
@ -116,6 +117,8 @@ int libp2p_peer_connect(const struct RsaPrivateKey* privateKey, struct Libp2pPee
|
|||
libp2p_session_context_free(peer->sessionContext);
|
||||
}
|
||||
peer->sessionContext = libp2p_session_context_new();
|
||||
peer->sessionContext->host = ip;
|
||||
peer->sessionContext->port = port;
|
||||
peer->sessionContext->datastore = datastore;
|
||||
peer->sessionContext->insecure_stream = libp2p_net_multistream_connect_with_timeout(ip, port, timeout);
|
||||
if (peer->sessionContext->insecure_stream == NULL) {
|
||||
|
@ -127,11 +130,18 @@ int libp2p_peer_connect(const struct RsaPrivateKey* privateKey, struct Libp2pPee
|
|||
peer->sessionContext->default_stream = peer->sessionContext->insecure_stream;
|
||||
peer->connection_type = CONNECTION_TYPE_CONNECTED;
|
||||
}
|
||||
// switch to secio
|
||||
if (libp2p_secio_initiate_handshake(peer->sessionContext, privateKey, peerstore) <= 0) {
|
||||
libp2p_logger_error("peer", "Attempted secio handshake, but failed for peer %s.\n", libp2p_peer_id_to_string(peer));
|
||||
free(ip);
|
||||
return 0;
|
||||
}
|
||||
// switch to yamux
|
||||
if (!yamux_send_protocol(peer->sessionContext)) {
|
||||
libp2p_logger_error("peer", "Attempted yamux handshake, but could not send protocol header for peer %s.\n", libp2p_peer_id_to_string(peer));
|
||||
free(ip);
|
||||
return 0;
|
||||
}
|
||||
free(ip);
|
||||
} // is IP
|
||||
now = time(NULL);
|
||||
|
|
1
swarm/swarm.c
Normal file
1
swarm/swarm.c
Normal file
|
@ -0,0 +1 @@
|
|||
|
|
@ -50,6 +50,19 @@ void yamux_read_stream(struct yamux_stream* stream, ssize_t incoming_size, uint8
|
|||
return;
|
||||
}
|
||||
|
||||
/***
|
||||
* Send the yamux protocol out the default stream
|
||||
* NOTE: if we initiate the connection, we should expect the same back
|
||||
* @param context the SessionContext
|
||||
* @returns true(1) on success, false(0) otherwise
|
||||
*/
|
||||
int yamux_send_protocol(struct SessionContext* context) {
|
||||
char* protocol = "/yamux/1.0.0\n";
|
||||
if (!context->default_stream->write(context, (uint8_t*)protocol, strlen(protocol)))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/***
|
||||
* Handles the message
|
||||
* @param incoming the incoming data buffer
|
||||
|
|
Loading…
Reference in a new issue