2017-02-22 15:55:39 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-10-12 17:37:40 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2017-02-22 15:55:39 +00:00
|
|
|
/**
|
|
|
|
* An interface in front of various streams
|
|
|
|
*/
|
|
|
|
struct Stream {
|
|
|
|
/**
|
|
|
|
* A generic socket descriptor
|
|
|
|
*/
|
|
|
|
void* socket_descriptor;
|
2017-10-12 17:37:40 +00:00
|
|
|
pthread_mutex_t socket_mutex;
|
2017-04-04 01:54:41 +00:00
|
|
|
struct MultiAddress *address;
|
2017-02-22 15:55:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads from the stream
|
2017-03-30 18:58:53 +00:00
|
|
|
* @param stream the stream context (usually a SessionContext pointer)
|
2017-02-22 15:55:39 +00:00
|
|
|
* @param buffer where to put the results
|
|
|
|
* @param bytes_read how many bytes were read
|
2017-04-17 19:03:27 +00:00
|
|
|
* @param timeout_secs number of seconds before a timeout
|
2017-02-22 15:55:39 +00:00
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
2017-04-17 19:03:27 +00:00
|
|
|
int (*read)(void* stream_context, unsigned char** buffer, size_t* bytes_read, int timeout_secs);
|
2017-02-22 15:55:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes to a stream
|
2017-08-30 16:09:28 +00:00
|
|
|
* @param stream the stream context (usually a SessionContext pointer)
|
2017-02-22 15:55:39 +00:00
|
|
|
* @param buffer what to write
|
|
|
|
* @param how much to write
|
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
2017-03-09 15:00:45 +00:00
|
|
|
int (*write)(void* stream_context, const unsigned char* buffer, size_t buffer_size);
|
2017-02-22 15:55:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes a stream
|
2017-07-27 19:32:42 +00:00
|
|
|
*
|
|
|
|
* NOTE: This is also responsible for deallocating the Stream struct
|
2017-03-30 18:58:53 +00:00
|
|
|
* @param stream the stream context
|
2017-02-22 15:55:39 +00:00
|
|
|
* @returns true(1) on success, otherwise false(0)
|
|
|
|
*/
|
2017-03-09 15:00:45 +00:00
|
|
|
int (*close)(void* stream_context);
|
2017-08-03 16:15:40 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Checks to see if something is waiting on the stream
|
|
|
|
*
|
|
|
|
* @param stream the stream context
|
|
|
|
* @returns true(1) if something is waiting, false(0) otherwise
|
|
|
|
*/
|
|
|
|
int (*peek)(void* stream_context);
|
2017-02-22 15:55:39 +00:00
|
|
|
};
|
2017-10-12 17:37:40 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Attempt to lock a stream for personal use. Does not block.
|
|
|
|
* @param stream the stream to lock
|
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
|
|
|
int libp2p_stream_try_lock(struct Stream* stream);
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Attempt to lock a stream for personal use. Blocks until the lock is acquired
|
|
|
|
* @param stream the stream to lock
|
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
|
|
|
int libp2p_stream_lock(struct Stream* stream);
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Attempt to unlock the mutex for this stream
|
|
|
|
* @param stream the stream to unlock
|
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
|
|
|
int libp2p_stream_unlock(struct Stream* stream);
|