Buildout of structures for node communication

yamux
John Jones 2017-02-22 10:55:39 -05:00
parent 9f0ee0cdb9
commit ddb1274596
4 changed files with 58 additions and 1 deletions

View File

@ -0,0 +1,37 @@
#pragma once
/**
* An interface in front of various streams
*/
struct Stream {
/**
* A generic socket descriptor
*/
void* socket_descriptor;
/**
* Reads from the stream
* @param stream the stream
* @param buffer where to put the results
* @param max_buffer_size don't read more than this many bytes
* @param bytes_read how many bytes were read
* @returns true(1) on success, false(0) otherwise
*/
int (*read)(struct Stream* stream, char* buffer, size_t max_buffer_size, size_t* bytes_read);
/**
* Writes to a stream
* @param stream the stream
* @param buffer what to write
* @param how much to write
* @returns true(1) on success, false(0) otherwise
*/
int (*write)(struct Stream* stream, char* buffer, size_t buffer_size);
/**
* Closes a stream
* @param stream the stream
* @returns true(1) on success, otherwise false(0)
*/
int (*close)(struct Stream* stream);
};

View File

@ -1,5 +1,6 @@
#pragma once
#include <stdint.h>
#include "libp2p/record/record.h"
/**

View File

@ -2,7 +2,7 @@ CC = gcc
CFLAGS = -O0 -I../include -I../../c-protobuf -I../../c-multihash/include -I../../c-multiaddr/include -g3
LFLAGS =
DEPS =
OBJS = record.o message.o
OBJS = record.o message.o message_handler.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)

19
record/message_handler.c Normal file
View File

@ -0,0 +1,19 @@
#include "libp2p/record/message.h"
#include "libp2p/peer/peer.h"
/**
* A generic handler for different types of messages
*/
int libp2p_record_handler_ping(struct Libp2pPeer* peer, struct Libp2pMessage* message) {
return 0;
}
int libp2p_record_message_handle(struct Libp2pPeer* peer, struct Libp2pMessage* message) {
switch (message->message_type) {
case (MESSAGE_TYPE_PING):
return libp2p_record_handler_ping(peer, message);
}
return 0;
}