c-libp2p/include/libp2p/conn/connection.h

45 lines
1.4 KiB
C
Raw Normal View History

2017-02-13 22:41:31 +00:00
#pragma once
2017-02-13 18:26:41 +00:00
/**
* Implements an interface to connect and talk to different nodes.
* A Dialer will connect, and return a Connection structure
*/
#include "libp2p/conn/transport_dialer.h"
#include "multiaddr/multiaddr.h"
struct Connection {
int socket_handle;
2017-02-13 18:26:41 +00:00
/**
* Read from the stream
* @param socket_handle the socket to read from
* @param in what was read in NOTE: this allocates memory
* @param in_size the number of bytes read in
2017-02-13 22:41:31 +00:00
* @returns number of bytes written or negative number on error
2017-02-13 18:26:41 +00:00
*/
2017-02-13 22:41:31 +00:00
int (*read)(const struct Connection* conn, char** in, size_t* in_size);
2017-02-13 18:26:41 +00:00
/**
* Write to the stream
* @param socket_handle the socket to write to
* @param out the bytes to write to the stream
* @param out_size the number of bytes to write
* @returns 0 on success, otherwise an error code
*/
int (*write)(const struct Connection* conn, const char* out, size_t out_size);
2017-02-13 18:26:41 +00:00
};
2017-02-13 22:41:31 +00:00
/**
* creates a new connection
* @param transport_dialer the TransportDialer to use
* @param multiaddress the destination
* @returns a connection that is ready to be read from / written to
*/
struct Connection* libp2p_conn_connection_new(struct TransportDialer* transport_dialer, struct MultiAddress* multiaddress);
/***
* close a connection and dispose of struct
* @param connection the resource to clean up
*/
void libp2p_conn_connection_free(struct Connection* connection);