2020-12-04 08:06:26 +09:00
|
|
|
#ifndef _SOCKET_WRAPPER_H_
|
|
|
|
#define _SOCKET_WRAPPER_H_
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2020-12-04 15:56:21 +09:00
|
|
|
#include <netdb.h>
|
2020-12-04 08:06:26 +09:00
|
|
|
|
|
|
|
#ifdef USE_SENDFILE
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
const size_t SEND_FILE_CHUNK_SIZE = 0x100000; /*1MB*/
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const in_port_t SERVER_PORT = 9091;
|
|
|
|
static const size_t DEFAULT_BUF_SIZE = 4096;
|
|
|
|
static const size_t MINIMUM_BUF_SIZE = 1024;
|
|
|
|
/*0 < x < MAX_PATH_SIZE*/
|
|
|
|
static const uint16_t MAX_PATH_SIZE = 256;
|
|
|
|
static const int TIMEOUT = 5;
|
|
|
|
|
|
|
|
enum{
|
|
|
|
RES_OK = 0,
|
|
|
|
RES_ERR = 1,
|
|
|
|
RES_USR_ERR = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(push,1)
|
|
|
|
struct ReadOp{
|
|
|
|
uint16_t file_url_size;/* strlen(data) */
|
|
|
|
uint16_t padding0;
|
|
|
|
};
|
|
|
|
struct TransferResult{
|
|
|
|
int16_t res;
|
|
|
|
int16_t error_msg_size;
|
|
|
|
int64_t file_size; /**/
|
|
|
|
int32_t err_number; /*errno*/
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
#ifdef USE_GETADDRINFO
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-12-04 14:25:10 +09:00
|
|
|
/**
|
|
|
|
* find buffer size from sock
|
|
|
|
* thread safe
|
|
|
|
*/
|
2020-12-04 08:06:26 +09:00
|
|
|
int getBufferSizeFrom(int sock);
|
|
|
|
|
2020-12-04 14:25:10 +09:00
|
|
|
/**
|
|
|
|
* return -2 if timeout occur.
|
|
|
|
* otherwise, implement equal to 'recv'.
|
|
|
|
* `timeout` unit is second.
|
|
|
|
* thread safe
|
2020-12-04 08:06:26 +09:00
|
|
|
*/
|
|
|
|
ssize_t timeout_recv(int fd,void * buf,size_t n,int timeout);
|
2020-12-04 15:56:21 +09:00
|
|
|
/**
|
2020-12-04 14:25:10 +09:00
|
|
|
* recieve data to buf until all bytes.
|
|
|
|
* thread safe
|
2020-12-04 08:06:26 +09:00
|
|
|
*/
|
|
|
|
ssize_t recv_until_byte(int fd,void * buf, size_t n,int timeout);
|
|
|
|
|
2020-12-04 15:56:21 +09:00
|
|
|
/**
|
|
|
|
* find sockaddr by hostname
|
|
|
|
* it perform like `gethostbyname`
|
|
|
|
* hostname could not be greater than _SC_HOST_NAME_MAX.
|
|
|
|
* return 0 on success, otherwise return ecode.
|
|
|
|
* ecode can be converted to string by `gai_strerror`.
|
|
|
|
*/
|
|
|
|
int getsockaddrbyname(int domain,int type, int protocol, const char * hostname_str, struct sockaddr * retaddr);
|
2020-12-04 08:06:26 +09:00
|
|
|
#endif
|