Cleanup of needless Stream attached to things it shouldn't have

This commit is contained in:
John Jones 2017-07-27 12:06:27 -05:00
parent 93bb78169a
commit 1971e60438
5 changed files with 30 additions and 12 deletions

View file

@ -63,6 +63,18 @@ struct SessionContext {
size_t remote_ephemeral_public_key_size; size_t remote_ephemeral_public_key_size;
}; };
/***
* Allocate resources for a new SessionContext struct
* @returns the newly allocated SessionContext, or NULL
*/
struct SessionContext* libp2p_session_context_new();
/**
* Free resources of a SessionContext struct
* @param context the SessionContext
* @returns true(1)
*/
int libp2p_session_context_free(struct SessionContext* session);
/*** /***
* Compare 2 SessionContext structs for equality * Compare 2 SessionContext structs for equality
* @param a side A * @param a side A

View file

@ -19,7 +19,7 @@ struct Libp2pPeer {
size_t id_size; // the length of id size_t id_size; // the length of id
struct Libp2pLinkedList* addr_head; // protobuf field 2 of multiaddr bytes (repeatable) (stored here as a struct MultiAddr) struct Libp2pLinkedList* addr_head; // protobuf field 2 of multiaddr bytes (repeatable) (stored here as a struct MultiAddr)
enum ConnectionType connection_type; // protobuf field 3 (a varint) enum ConnectionType connection_type; // protobuf field 3 (a varint)
struct Stream *connection; // not protobuf'd, the current connection to the peer struct SessionContext *sessionContext; // not protobuf'd, the current connection to the peer
}; };
/** /**

View file

@ -30,7 +30,7 @@ int libp2p_net_multistream_close(void* stream_context) {
/** /**
* Write to an open multistream host * Write to an open multistream host
* @param socket_fd the socket file descriptor * @param stream_context the session context
* @param data the data to send * @param data the data to send
* @param data_length the length of the data * @param data_length the length of the data
* @returns the number of bytes written * @returns the number of bytes written

View file

@ -19,7 +19,7 @@ struct Libp2pPeer* libp2p_peer_new() {
out->id_size = 0; out->id_size = 0;
out->addr_head = NULL; out->addr_head = NULL;
out->connection_type = CONNECTION_TYPE_NOT_CONNECTED; out->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
out->connection = NULL; out->sessionContext = NULL;
} }
return out; return out;
} }
@ -60,8 +60,10 @@ int libp2p_peer_connect(struct Libp2pPeer* peer, int timeout) {
if (!multiaddress_get_ip_address(ma, &ip)) if (!multiaddress_get_ip_address(ma, &ip))
continue; continue;
int port = multiaddress_get_ip_port(ma); int port = multiaddress_get_ip_port(ma);
peer->connection = libp2p_net_multistream_connect(ip, port); peer->sessionContext = libp2p_session_context_new();
if (peer->connection != NULL) { peer->sessionContext->insecure_stream = libp2p_net_multistream_connect(ip, port);
if (peer->sessionContext->insecure_stream != NULL) {
peer->sessionContext->default_stream = peer->sessionContext->insecure_stream;
peer->connection_type = CONNECTION_TYPE_CONNECTED; peer->connection_type = CONNECTION_TYPE_CONNECTED;
} }
free(ip); free(ip);
@ -112,9 +114,10 @@ void libp2p_peer_free(struct Libp2pPeer* in) {
} }
if (in->id != NULL) if (in->id != NULL)
free(in->id); free(in->id);
if (in->connection != NULL) { if (in->sessionContext != NULL) {
libp2p_net_multistream_stream_free(in->connection); libp2p_session_context_free(in->sessionContext);
in->connection = NULL; //libp2p_net_multistream_stream_free(in->connection);
in->sessionContext = NULL;
} }
// free the memory in the linked list // free the memory in the linked list
struct Libp2pLinkedList* current = in->addr_head; struct Libp2pLinkedList* current = in->addr_head;
@ -130,6 +133,9 @@ void libp2p_peer_free(struct Libp2pPeer* in) {
/** /**
* Make a copy of a peer * Make a copy of a peer
*
* NOTE: SessionContext is not copied.
*
* @param in what is to be copied * @param in what is to be copied
* @returns a new struct, that does not rely on the old * @returns a new struct, that does not rely on the old
*/ */
@ -159,7 +165,7 @@ struct Libp2pPeer* libp2p_peer_copy(const struct Libp2pPeer* in) {
current_out = copy_item; current_out = copy_item;
current_in = current_in->next; current_in = current_in->next;
} }
out->connection = in->connection; out->sessionContext = in->sessionContext;
} }
return out; return out;
} }
@ -185,7 +191,7 @@ int libp2p_peer_matches_id(struct Libp2pPeer* in, const unsigned char* peer_id)
*/ */
int libp2p_peer_is_connected(struct Libp2pPeer* in) { int libp2p_peer_is_connected(struct Libp2pPeer* in) {
if (in->connection_type == CONNECTION_TYPE_CONNECTED) { if (in->connection_type == CONNECTION_TYPE_CONNECTED) {
if (in->connection == NULL) { if (in->sessionContext == NULL || in->sessionContext->default_stream == NULL) {
in->connection_type = CONNECTION_TYPE_NOT_CONNECTED; in->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
} }
} }

View file

@ -478,7 +478,7 @@ int libp2p_secio_unencrypted_write(struct SessionContext* session, unsigned char
int written = 0; int written = 0;
int written_this_time = 0; int written_this_time = 0;
do { do {
written_this_time = socket_write(*((int*)session->insecure_stream->socket_descriptor), &size_as_char[written], left, 0); written_this_time = socket_write(*((int*)session->default_stream->socket_descriptor), &size_as_char[written], left, 0);
if (written_this_time < 0) { if (written_this_time < 0) {
written_this_time = 0; written_this_time = 0;
if ( (errno == EAGAIN) || (errno == EWOULDBLOCK)) { if ( (errno == EAGAIN) || (errno == EWOULDBLOCK)) {
@ -493,7 +493,7 @@ int libp2p_secio_unencrypted_write(struct SessionContext* session, unsigned char
left = data_length; left = data_length;
written = 0; written = 0;
do { do {
written_this_time = socket_write(*((int*)session->insecure_stream->socket_descriptor), (char*)&bytes[written], left, 0); written_this_time = socket_write(*((int*)session->default_stream->socket_descriptor), (char*)&bytes[written], left, 0);
if (written_this_time < 0) { if (written_this_time < 0) {
written_this_time = 0; written_this_time = 0;
if ( (errno == EAGAIN) || (errno == EWOULDBLOCK)) { if ( (errno == EAGAIN) || (errno == EWOULDBLOCK)) {