tiptorrent/src/core.h

98 lines
2.2 KiB
C

#ifndef _TIP_CORE_H
#define _TIP_CORE_H
#include <ev.h>
#include "list.h"
#include <stdbool.h>
#include <netinet/in.h>
#define MAX_CHUNKS 4
#define TIP_MSG_REQUEST_MAXLEN 131072
extern const char *root;
#define DEFAULT_MAX_CLIENTS 3
extern int max_clients;
extern int num_clients;
extern bool redirect;
/* max_client logic only applies for files larger than 1024 bytes. */
#define FILE_SIZE_THRESHOLD 1024ULL
enum tip_client_state {
TIP_CLIENT_PENDING = 0,
TIP_CLIENT_RECEIVING_HEADER,
TIP_CLIENT_RECEIVING_PAYLOAD,
TIP_CLIENT_PROCESSING_REQUEST,
TIP_CLIENT_PROCESSING_REQUEST_2,
TIP_CLIENT_PROCESSING_REQUEST_3,
TIP_CLIENT_CLOSE_WAIT,
};
enum tip_http_method {
TIP_METHOD_GET = 0,
TIP_METHOD_HEAD,
TIP_METHOD_POST,
};
struct tip_client {
struct list_head list;
struct ev_io io;
struct ev_timer timer;
struct sockaddr_in addr;
enum tip_client_state state;
char buf[TIP_MSG_REQUEST_MAXLEN];
unsigned int buf_len;
unsigned int msg_len;
int content_length;
char auth_token[64];
/* for file serving. */
enum tip_http_method method;
const char *uri;
const char *path;
uint32_t chunk;
off_t size;
off_t left;
int fd;
off_t offset;
/* for redirection. */
bool redirect;
struct sockaddr_in redirect_addr;
bool allow_redirect;
};
static inline int tip_client_socket(const struct tip_client *cli)
{
return cli->io.fd;
}
static inline bool tip_client_large_file(const struct tip_client *cli)
{
return cli->size > FILE_SIZE_THRESHOLD;
}
void tip_client_pending(struct tip_client *cli);
bool tip_client_redirect(struct tip_client *cli);
int tip_client_redirect_create(const struct tip_client *cli);
void tip_client_activate_pending(bool redirect_only);
extern struct ev_loop *tip_main_loop;
int tip_socket_server_init(const char *port);
void tip_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events);
int tip_client_state_process_payload(struct tip_client *cli);
int tip_client_state_process_payload_reply(struct tip_client *cli);
int tip_client_state_process_payload_bulk(struct tip_client *cli);
struct tip_client_redirect {
struct list_head list;
struct sockaddr_in addr;
const char *uri;
struct ev_timer timer;
uint32_t users;
};
#endif