2017-10-11 16:23:25 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <time.h>
|
2017-11-06 18:36:11 +00:00
|
|
|
#include <sys/types.h>
|
2017-10-11 16:23:25 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "frame.h"
|
|
|
|
#include "stream.h"
|
2017-11-06 18:36:11 +00:00
|
|
|
#include "libp2p/net/stream.h"
|
2017-11-06 21:38:55 +00:00
|
|
|
//#include "libp2p/yamux/yamux.h"
|
2017-10-11 16:23:25 +00:00
|
|
|
|
|
|
|
enum yamux_session_type
|
|
|
|
{
|
|
|
|
yamux_session_client,
|
|
|
|
yamux_session_server
|
|
|
|
};
|
|
|
|
enum yamux_error
|
|
|
|
{
|
|
|
|
yamux_error_normal = 0x00,
|
|
|
|
yamux_error_protoc = 0x01,
|
|
|
|
yamux_error_intern = 0x02
|
|
|
|
};
|
|
|
|
|
2017-11-19 18:37:03 +00:00
|
|
|
// forward declarations
|
2017-10-11 16:23:25 +00:00
|
|
|
struct yamux_session;
|
|
|
|
struct yamux_stream;
|
2017-11-19 18:37:03 +00:00
|
|
|
struct YamuxContext;
|
|
|
|
struct Stream;
|
|
|
|
struct StreamMessage;
|
2017-10-11 16:23:25 +00:00
|
|
|
|
|
|
|
typedef void* (*yamux_session_get_str_ud_fn)(struct yamux_session* session, yamux_streamid newid );
|
|
|
|
typedef void (*yamux_session_ping_fn )(struct yamux_session* session, uint32_t val );
|
|
|
|
typedef void (*yamux_session_pong_fn )(struct yamux_session* session, uint32_t val, struct timespec dt);
|
|
|
|
typedef void (*yamux_session_go_away_fn )(struct yamux_session* session, enum yamux_error err );
|
2017-11-19 18:37:03 +00:00
|
|
|
typedef void (*yamux_session_new_stream_fn)(struct YamuxContext* context, struct Stream* stream, struct StreamMessage* msg);
|
2017-10-11 16:23:25 +00:00
|
|
|
typedef void (*yamux_session_free_fn )(struct yamux_session* sesssion );
|
|
|
|
|
|
|
|
struct yamux_session_stream
|
|
|
|
{
|
|
|
|
struct yamux_stream* stream;
|
|
|
|
int alive;
|
|
|
|
};
|
2017-11-19 18:37:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A yamux session. This keeps all the streams related to a yamux session
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
struct yamux_session
|
|
|
|
{
|
2017-11-19 18:37:03 +00:00
|
|
|
struct yamux_config* config; // configuration of size of windows and max number of streams
|
2017-10-11 16:23:25 +00:00
|
|
|
|
2017-11-19 18:37:03 +00:00
|
|
|
size_t num_streams; // number of streams
|
|
|
|
size_t cap_streams; // capacity of stream array
|
|
|
|
struct yamux_session_stream* streams; // array of streams
|
2017-10-11 16:23:25 +00:00
|
|
|
|
2017-11-19 18:37:03 +00:00
|
|
|
/**
|
|
|
|
* Get user data
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
yamux_session_get_str_ud_fn get_str_ud_fn;
|
2017-11-19 18:37:03 +00:00
|
|
|
/**
|
|
|
|
* Ping
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
yamux_session_ping_fn ping_fn ;
|
2017-11-19 18:37:03 +00:00
|
|
|
/**
|
|
|
|
* Respond to ping
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
yamux_session_pong_fn pong_fn ;
|
2017-11-19 18:37:03 +00:00
|
|
|
/**
|
|
|
|
* Hanging up
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
yamux_session_go_away_fn go_away_fn ;
|
2017-11-19 18:37:03 +00:00
|
|
|
/**
|
|
|
|
* A new stream is coming in
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
yamux_session_new_stream_fn new_stream_fn;
|
2017-11-19 18:37:03 +00:00
|
|
|
/**
|
|
|
|
* Free resources
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
yamux_session_free_fn free_fn ;
|
|
|
|
|
2017-11-19 18:37:03 +00:00
|
|
|
/**
|
|
|
|
* User data
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
void* userdata;
|
|
|
|
|
2017-11-19 18:37:03 +00:00
|
|
|
/**
|
|
|
|
* for heartbeat
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
struct timespec since_ping;
|
|
|
|
|
2017-11-19 18:37:03 +00:00
|
|
|
/**
|
|
|
|
* Session type (client or server)
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
enum yamux_session_type type;
|
|
|
|
|
2017-11-19 18:37:03 +00:00
|
|
|
/***
|
|
|
|
* The parent stream
|
|
|
|
*/
|
2017-11-06 18:36:11 +00:00
|
|
|
struct Stream* parent_stream;
|
2017-10-11 16:23:25 +00:00
|
|
|
|
2017-11-19 18:37:03 +00:00
|
|
|
/***
|
|
|
|
* The next id to use
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
yamux_streamid nextid;
|
|
|
|
|
2017-11-19 18:37:03 +00:00
|
|
|
/**
|
|
|
|
* Determine if this session is closed
|
|
|
|
*/
|
2017-10-11 16:23:25 +00:00
|
|
|
int closed;
|
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Create a new yamux session
|
|
|
|
* @param config the configuration
|
|
|
|
* @param sock the socket
|
|
|
|
* @param type session type (yamux_session_server or yamux_session_client)
|
|
|
|
* @param userdata user data
|
|
|
|
* @returns the yamux_session struct
|
|
|
|
*/
|
2017-11-06 18:36:11 +00:00
|
|
|
struct yamux_session* yamux_session_new(struct yamux_config* config, struct Stream* parent_stream, enum yamux_session_type type, void* userdata);
|
2017-10-11 16:23:25 +00:00
|
|
|
|
|
|
|
// does not close the socket, but does close the session
|
2017-11-06 18:36:11 +00:00
|
|
|
void yamux_session_free(struct yamux_session* session);
|
2017-10-11 16:23:25 +00:00
|
|
|
|
|
|
|
// does not free used memory
|
|
|
|
ssize_t yamux_session_close(struct yamux_session* session, enum yamux_error err);
|
2017-11-06 18:36:11 +00:00
|
|
|
|
2017-10-11 16:23:25 +00:00
|
|
|
inline ssize_t yamux_session_go_away(struct yamux_session* session, enum yamux_error err)
|
|
|
|
{
|
|
|
|
return yamux_session_close(session, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t yamux_session_ping(struct yamux_session* session, uint32_t value, int pong);
|
|
|
|
|
|
|
|
// defers to stream read handlers
|
|
|
|
ssize_t yamux_session_read(struct yamux_session* session);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode an incoming message
|
2017-11-19 18:37:03 +00:00
|
|
|
* @param context a YamuxChannelContext or YamuxContext
|
2017-10-11 16:23:25 +00:00
|
|
|
* @param incoming the incoming bytes
|
|
|
|
* @param incoming_size the size of the incoming bytes
|
2017-11-19 18:37:03 +00:00
|
|
|
* @param return_message the return message (usually the bytes after the frame)
|
2017-10-11 16:23:25 +00:00
|
|
|
* @returns true(1) on success, false(0) otherwise
|
|
|
|
*/
|
2017-11-19 18:37:03 +00:00
|
|
|
int yamux_decode(void* context, const uint8_t* incoming, size_t incoming_size, struct StreamMessage** return_message);
|