2017-08-09 13:03:40 +00:00
|
|
|
#pragma once
|
|
|
|
#include "libp2p/conn/session.h"
|
|
|
|
#include "libp2p/utils/vector.h"
|
2017-10-23 20:21:50 +00:00
|
|
|
#include "libp2p/net/stream.h"
|
2017-08-09 13:03:40 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* An "interface" for different IPFS protocols
|
|
|
|
*/
|
|
|
|
struct Libp2pProtocolHandler {
|
|
|
|
/**
|
|
|
|
* A protocol dependent context (often an IpfsNode pointer, but libp2p doesn't know about that)
|
|
|
|
*/
|
|
|
|
void* context;
|
|
|
|
/**
|
|
|
|
* Determines if this protocol can handle the incoming message
|
|
|
|
* @param incoming the incoming data
|
|
|
|
* @param incoming_size the size of the incoming data buffer
|
|
|
|
* @returns true(1) if it can handle this message, false(0) if not
|
|
|
|
*/
|
2017-10-23 20:21:50 +00:00
|
|
|
int (*CanHandle)(const struct StreamMessage* msg);
|
2017-08-09 13:03:40 +00:00
|
|
|
/***
|
|
|
|
* Handles the message
|
|
|
|
* @param incoming the incoming data buffer
|
|
|
|
* @param incoming_size the size of the incoming data buffer
|
2017-11-08 15:51:43 +00:00
|
|
|
* @param stream the incoming stream
|
2017-08-09 13:03:40 +00:00
|
|
|
* @param protocol_context the protocol-dependent context
|
|
|
|
* @returns 0 if the caller should not continue looping, <0 on error, >0 on success
|
|
|
|
*/
|
2017-11-08 15:51:43 +00:00
|
|
|
int (*HandleMessage)(const struct StreamMessage* msg, struct Stream* stream, void* protocol_context);
|
2017-08-09 13:03:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shutting down. Clean up any memory allocations
|
|
|
|
* @param protocol_context the context
|
|
|
|
* @returns true(1)
|
|
|
|
*/
|
|
|
|
int (*Shutdown)(void* protocol_context);
|
|
|
|
};
|
|
|
|
|
2017-08-31 21:40:35 +00:00
|
|
|
/**
|
|
|
|
* Allocate resources for a new Libp2pProtocolHandler
|
|
|
|
* @returns an allocated struct
|
|
|
|
*/
|
|
|
|
struct Libp2pProtocolHandler* libp2p_protocol_handler_new();
|
|
|
|
|
2017-11-23 12:44:42 +00:00
|
|
|
/***
|
|
|
|
* Release resources of a protocol handler
|
|
|
|
* @param handler the handler to free
|
|
|
|
*/
|
|
|
|
void libp2p_protocol_handler_free(struct Libp2pProtocolHandler* handler);
|
|
|
|
|
2017-11-06 22:31:30 +00:00
|
|
|
/***
|
|
|
|
* Handle an incoming message
|
|
|
|
* @param message the incoming message
|
2017-11-08 15:51:43 +00:00
|
|
|
* @param stream the incoming connection
|
2017-11-06 22:31:30 +00:00
|
|
|
* @param handlers a Vector of protocol handlers
|
2017-11-08 15:51:43 +00:00
|
|
|
* @returns -1 on error, 0 on protocol upgrade, 1 on success
|
2017-11-06 22:31:30 +00:00
|
|
|
*/
|
2017-11-08 15:51:43 +00:00
|
|
|
int libp2p_protocol_marshal(struct StreamMessage* message, struct Stream* stream, struct Libp2pVector* protocol_handlers);
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Shut down all protocol handlers and free vector
|
|
|
|
* @param handlers vector of Libp2pProtocolHandler
|
|
|
|
* @returns true(1)
|
|
|
|
*/
|
|
|
|
int libp2p_protocol_handlers_shutdown(struct Libp2pVector* handlers);
|
2017-11-30 19:32:36 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Check to see if this is a valid protocol
|
|
|
|
* @param msg the message
|
|
|
|
* @param handlers the vector of handlers
|
|
|
|
*/
|
|
|
|
int libp2p_protocol_is_valid_protocol(struct StreamMessage* msg, struct Libp2pVector* handlers);
|
2017-12-01 01:58:47 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Retrieve the correct protocol handlder for a particular protocol id
|
|
|
|
* @param protocol_handlers the collection of protocol handlers
|
|
|
|
* @param id the protocol id
|
|
|
|
* @returns a protocol handler that can handle id (or NULL if none found)
|
|
|
|
*/
|
|
|
|
const struct Libp2pProtocolHandler* libp2p_protocol_get_handler(struct Libp2pVector* protocol_handlers, const char* id);
|