added comments
This commit is contained in:
parent
1e49d8f7ab
commit
343748bc0f
3 changed files with 61 additions and 35 deletions
|
@ -1,39 +1,49 @@
|
|||
#ifndef P2PNET_H
|
||||
#define P2PNET_H
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int socket_open4();
|
||||
int socket_bind4(int s, uint32_t ip, uint16_t port);
|
||||
int socket_bind4_reuse(int s, uint32_t ip, uint16_t port);
|
||||
int socket_read_select4(int socket_fd, int num_seconds);
|
||||
int socket_accept4(int s, uint32_t *ip, uint16_t *port);
|
||||
int socket_local4(int s, uint32_t *ip, uint16_t *port);
|
||||
int socket_connect4(int s, uint32_t ip, uint16_t port);
|
||||
int socket_listen(int s, uint32_t *localip, uint16_t *localport);
|
||||
ssize_t socket_read(int s, char *buf, size_t len, int flags, int timeout_secs);
|
||||
ssize_t socket_write(int s, const char *buf, size_t len, int flags);
|
||||
/**
|
||||
* Used to send the size of the next transmission for "framed" transmissions. NOTE: This will send in big endian format
|
||||
* @param s the socket descriptor
|
||||
* @param size the size to send
|
||||
* @param flags socket flags
|
||||
* @returns number of bytes sent
|
||||
*/
|
||||
ssize_t socket_write_size(int s, unsigned long size, int flags);
|
||||
int socket_open4();
|
||||
int socket_bind4(int s, uint32_t ip, uint16_t port);
|
||||
int socket_bind4_reuse(int s, uint32_t ip, uint16_t port);
|
||||
int socket_read_select4(int socket_fd, int num_seconds);
|
||||
int socket_accept4(int s, uint32_t *ip, uint16_t *port);
|
||||
int socket_local4(int s, uint32_t *ip, uint16_t *port);
|
||||
int socket_connect4(int s, uint32_t ip, uint16_t port);
|
||||
int socket_listen(int s, uint32_t *localip, uint16_t *localport);
|
||||
|
||||
int socket_tcp4(void);
|
||||
/***
|
||||
* Reads data from a socket, used instead of recv so if a protocol needs
|
||||
* to use something else before or after it can be done here instead of
|
||||
* outside the lib.
|
||||
*
|
||||
* @param s the socket
|
||||
* @param buf what to send
|
||||
* @param len the length of buf
|
||||
* @param flags network flags
|
||||
* @param num_secs the number of seconds before a timeout
|
||||
* @returns number of bytes, 0, or negative number on error (i.e. EAGAIN or EWOULDBLOCK)
|
||||
*/
|
||||
ssize_t socket_read(int s, char *buf, size_t len, int flags, int timeout_secs);
|
||||
ssize_t socket_write(int s, const char *buf, size_t len, int flags);
|
||||
/**
|
||||
* Used to send the size of the next transmission for "framed" transmissions. NOTE: This will send in big endian format
|
||||
* @param s the socket descriptor
|
||||
* @param size the size to send
|
||||
* @param flags socket flags
|
||||
* @returns number of bytes sent
|
||||
*/
|
||||
ssize_t socket_write_size(int s, unsigned long size, int flags);
|
||||
|
||||
int socket_stream_sctp4(void);
|
||||
int socket_tcp4(void);
|
||||
|
||||
int socket_udp4(void);
|
||||
int socket_stream_sctp4(void);
|
||||
|
||||
/**
|
||||
* convert a hostname into an ip address
|
||||
* @param hostname the name of the host. i.e. www.jmjatlanta.com
|
||||
* @returns the ip address as an uint32_t
|
||||
*/
|
||||
uint32_t hostname_to_ip(const char* hostname);
|
||||
int socket_udp4(void);
|
||||
|
||||
#endif // P2PNET_H
|
||||
/**
|
||||
* convert a hostname into an ip address
|
||||
* @param hostname the name of the host. i.e. www.jmjatlanta.com
|
||||
* @returns the ip address as an uint32_t
|
||||
*/
|
||||
uint32_t hostname_to_ip(const char* hostname);
|
||||
|
|
|
@ -94,6 +94,7 @@ int libp2p_net_multistream_read(void* stream_context, unsigned char** results, s
|
|||
struct SessionContext* session_context = (struct SessionContext*)stream_context;
|
||||
struct Stream* stream = session_context->default_stream;
|
||||
int bytes = 0;
|
||||
|
||||
// TODO: this is arbitrary, and should be dynamic
|
||||
size_t buffer_size = 362144;
|
||||
char buffer[buffer_size];
|
||||
|
@ -101,11 +102,15 @@ int libp2p_net_multistream_read(void* stream_context, unsigned char** results, s
|
|||
size_t num_bytes_requested = 0, left = 0, already_read = 0;
|
||||
|
||||
if (session_context->secure_stream == NULL) {
|
||||
int socketDescriptor = *( (int*) stream->socket_descriptor);
|
||||
// first read the varint
|
||||
while(1) {
|
||||
unsigned char c = '\0';
|
||||
bytes = socket_read(*((int*)stream->socket_descriptor), (char*)&c, 1, 0, timeout_secs);
|
||||
if (bytes <= 0) { // timeout
|
||||
bytes = socket_read(socketDescriptor, (char*)&c, 1, 0, timeout_secs);
|
||||
if (bytes <= 0) {
|
||||
// possible error
|
||||
if (bytes < 0)
|
||||
libp2p_logger_error("multistream", "socket_read returned %d reading socket %d\n", bytes, socketDescriptor);
|
||||
return 0;
|
||||
}
|
||||
pos[0] = c;
|
||||
|
@ -116,17 +121,20 @@ int libp2p_net_multistream_read(void* stream_context, unsigned char** results, s
|
|||
}
|
||||
pos++;
|
||||
}
|
||||
if (num_bytes_requested <= 0)
|
||||
if (num_bytes_requested <= 0) {
|
||||
libp2p_logger_debug("multistream", "Reading the varint returned %d on socket %d\n", num_bytes_requested, socketDescriptor);
|
||||
return 0;
|
||||
}
|
||||
|
||||
left = num_bytes_requested;
|
||||
do {
|
||||
bytes = socket_read(*((int*)stream->socket_descriptor), &buffer[already_read], left, 0, timeout_secs);
|
||||
bytes = socket_read(socketDescriptor, &buffer[already_read], left, 0, timeout_secs);
|
||||
if (bytes < 0) {
|
||||
bytes = 0;
|
||||
if ( errno == EAGAIN ) {
|
||||
// do something intelligent
|
||||
} else {
|
||||
libp2p_logger_error("multistream", "socket read returned error %d on socket descriptor %d.\n", errno, socketDescriptor);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
10
net/socket.c
10
net/socket.c
|
@ -137,9 +137,17 @@ int socket_listen(int s, uint32_t *localip, uint16_t *localport)
|
|||
return s;
|
||||
}
|
||||
|
||||
/* Reads data from a socket, used instead of recv so if a protocol needs
|
||||
/***
|
||||
* Reads data from a socket, used instead of recv so if a protocol needs
|
||||
* to use something else before or after it can be done here instead of
|
||||
* outside the lib.
|
||||
*
|
||||
* @param s the socket
|
||||
* @param buf what to send
|
||||
* @param len the length of buf
|
||||
* @param flags network flags
|
||||
* @param num_secs the number of seconds before a timeout
|
||||
* @returns number of bytes, 0, or negative number on error (i.e. EAGAIN or EWOULDBLOCK)
|
||||
*/
|
||||
ssize_t socket_read(int s, char *buf, size_t len, int flags, int num_secs)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue