correctly handle a stream that is unable to connect

This commit is contained in:
John Jones 2017-07-31 08:38:16 -05:00
parent 30250a1af0
commit 4542ebe1cb
2 changed files with 7 additions and 2 deletions

View file

@ -64,14 +64,15 @@ int libp2p_net_multistream_write(void* stream_context, const unsigned char* data
memcpy(&buffer[varint_size], data, data_length); memcpy(&buffer[varint_size], data, data_length);
// determine if this should run through the secio protocol or not // determine if this should run through the secio protocol or not
if (session_context->secure_stream == NULL) { if (session_context->secure_stream == NULL) {
int sd = *((int*)stream->socket_descriptor);
// do a "raw" write // do a "raw" write
num_bytes = socket_write(*((int*)stream->socket_descriptor), (char*)varint, varint_size, 0); num_bytes = socket_write(sd, (char*)varint, varint_size, 0);
if (num_bytes == 0) { if (num_bytes == 0) {
free(buffer); free(buffer);
return 0; return 0;
} }
// then send the actual data // then send the actual data
num_bytes += socket_write(*((int*)stream->socket_descriptor), (char*)data, data_length, 0); num_bytes += socket_write(sd, (char*)data, data_length, 0);
} else { } else {
// write using secio // write using secio
num_bytes = stream->write(stream_context, buffer, data_length + varint_size); num_bytes = stream->write(stream_context, buffer, data_length + varint_size);

View file

@ -93,6 +93,10 @@ int libp2p_peer_connect(struct RsaPrivateKey* privateKey, struct Libp2pPeer* pee
int port = multiaddress_get_ip_port(ma); int port = multiaddress_get_ip_port(ma);
peer->sessionContext = libp2p_session_context_new(); peer->sessionContext = libp2p_session_context_new();
peer->sessionContext->insecure_stream = libp2p_net_multistream_connect(ip, port); peer->sessionContext->insecure_stream = libp2p_net_multistream_connect(ip, port);
if (peer->sessionContext->insecure_stream == NULL) {
free(ip);
return 0;
}
if (peer->sessionContext->insecure_stream != NULL) { if (peer->sessionContext->insecure_stream != NULL) {
peer->sessionContext->default_stream = peer->sessionContext->insecure_stream; peer->sessionContext->default_stream = peer->sessionContext->insecure_stream;
peer->connection_type = CONNECTION_TYPE_CONNECTED; peer->connection_type = CONNECTION_TYPE_CONNECTED;