#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <unistd.h>
#include <csignal>
#include <pthread.h>
#include <event.h>
#include <fcntl.h>
#include <cerrno>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cmath>
#include <cstdio>
#include <cassert>
#include <byteswap.h>
#include "config.h"
#include "Cache.h"
#include "ServerLogger.h"
#include "functions.h"
Go to the source code of this file.
Classes | |
struct | Thread |
stores information about a single thread More... | |
struct | Info |
stores information about a single connection with one client. More... | |
struct | workerData |
Task information structure sent to worker threads via pipes. More... | |
struct | raw_request_header |
Generic request header. More... | |
struct | set_request_header |
Set request header. More... | |
struct | increment_request_header |
Increment request header. More... | |
union | request_header |
Request header. More... | |
struct | raw_response_header |
Generic response header. More... | |
struct | hit_response_header |
Header for get response returning object. More... | |
struct | increment_response_header |
Header for increment response. More... | |
union | response_header |
Response header used for stats response. More... | |
Defines | |
#define | HAVE_CONFIG_H 1 |
Determines whether config.h file is available. | |
#define | DEBUG_BINARY 0 |
Debug switch for binary handler. | |
#define | DEBUG_ASCII 0 |
Debug switch for ascii handler. | |
#define | DEBUG_EV_SERVER 1 |
Turns on and off the debug prints. | |
#define | MAX_HEADER_LENGTH 300 |
maximal header length | |
#define | REPORT_ERROR |
registers writeCallback() to send an ERROR response to the client | |
#define | sweepConnectionTemplate(i, event) |
closes client socket and frees memory related to that connection | |
#define | sweepConnection(i) sweepConnectionTemplate(i, i->readEvent) |
Specialization of sweepConnectionTemplate(i, event) for readEvent. | |
#define | sweepConnectionWrite(i) sweepConnectionTemplate(i, i->writeEvent) |
Specialization of sweepConnectionTemplate(i, event) for writeEvent. | |
Enumerations | |
enum | State { SET_HEADER, SET_VALUE, CAS_HEADER, CAS_VALUE, ADD_HEADER, ADD_VALUE, REPLACE_HEADER, REPLACE_VALUE, APPEND_HEADER, APPEND_VALUE, PREPEND_HEADER, PREPEND_VALUE, UNDEFINED, QUIT, VERSION_CMD, GET_HEADER, GETS_HEADER, ERROR_STATE, GET_RESPONSE_END, STATS_HEADER, STATS_RESPONSE, INCR_HEADER, DECR_HEADER, INCR_DECR_RESPONSE } |
all connection states | |
Functions | |
void | loadConfFile (const string &serverConfFilename) |
reads configuration file and stored data into global variables | |
void * | threadWorker (void *arg) |
threads main body (which is simply event_base_loop()) | |
void | acceptCallback (int serverSocket, short event, void *arg) |
accepting callback | |
void | pipeCallback (int fd, short event, void *arg) |
hollow callback. | |
void | readCallback (int socket, short event, void *arg) |
whole parsing of the clients' input. | |
void | writeCallback (int socket, short event, void *arg) |
function that cares about sending response to clients. | |
void | binaryHandler (Info *i) |
Provides binary protocol parsing and executes methods from libdreamcache library as well as generates response and tries to send it immediately. | |
void | asciiHandler (Info *i) |
Provides text protocol parsing and executes methods from libdreamcache library as well as generates response and tries to send it immediately. | |
void | parseState (Info *i) |
reads i->buffer at i->cursor position and determines the command which was sent by a client. | |
void | resetBuffer (Info *i) |
when many requests are sent in one package and there is too little place for a new request pending ones are copied to the beginning of i->buffer. | |
void | printBinaryBuffer (void *buffer, int n, const char *label=NULL) |
For debug purpose only. | |
void | printBinaryCommand (uint8_t cmd) |
For debug purpose only. | |
void | signalHandler (int n) |
This method is called when server receives signals. | |
void | prepareStatBinaryResponse (request_header *header, Info *i) |
Prepares binary stats request response. | |
int | getStat (Stats *stats, const char *arg, char *buffer, int maxLength) |
Prepares stats request response. | |
void | freeQueue (Info *i) |
function that frees the reponse queue | |
void | daemonize () |
demonizes server | |
Variables | |
const char | STORED_MSG [] = "STORED\r\n" |
"STORED\r\n" | |
const char | NOT_STORED_MSG [] = "NOT_STORED\r\n" |
"NOT_STORED\r\n" | |
const char | EXISTS_MSG [] = "EXISTS\r\n" |
"EXISTS\r\n" | |
const char | NOT_FOUND_MSG [] = "NOT_FOUND\r\n" |
"NOT_FOUND\r\n" | |
const char | ERROR_MSG [] = "ERROR\r\n" |
"ERROR\r\n" | |
const char | LINE_TERMINATOR [] = "\r\n" |
"\r\n" | |
const char | REQUEST_TERMINATOR [] = "END\r\n" |
"END\r\n" | |
struct workerData | __packed__ |
const int | QUEUE_CHUNK_LENGTH = 1024 * 1024 |
queue reallocation step | |
unsigned | port |
Server's listening port. | |
std::string | userName = SINGLE_USER |
user used by server | |
std::string | spaceName = SINGLE_SPACE |
space used by server | |
unsigned | softQuota |
used space's soft quota | |
unsigned | hardQuota |
used space's hard quota | |
unsigned | serverLoggerInterval |
server logger's interval in seconds | |
bool | logSwitch |
flag to indicate logging or not logging | |
std::string | serverLogsDirectory |
server logs directory path | |
int | threadCount |
server threads count | |
timeval | connectionTimeout |
if a connection is idle for connectionTimeout miliseconds the connection is closed | |
Thread * | threads |
list of threads | |
time_t | startTime |
time to count uptime | |
Cache * | cache |
reference to cache | |
Space * | space |
reference to space | |
ServerLogger * | serverLogger |
reference to server logger |
Definition in file EventServer.h.
#define REPORT_ERROR |
Value:
{ \ i->cursor = i->offset = i->tokenCount = i->writeOffset = 0; \ i->sendIt = ERROR_MSG; \ i->length = sizeof(ERROR_MSG) - 1; \ \ i->state = ERROR_STATE; \ \ event_del(&i->readEvent); \ event_add(&i->writeEvent, &connectionTimeout); \ /*cout << "error in line " << __LINE__ << endl;*/ \ }
Definition at line 170 of file EventServer.h.
#define sweepConnectionTemplate | ( | i, | |||
event | ) |
Value:
{ \
i->t->connections--; \
event_del(&event); \
close(i->clientSocket); \
if (i->queueBuffer) { \
free(i->queueBuffer); \
} \
free(i); \
}
Defines general process of cleaning connection and client related data and closing connection.
Definition at line 612 of file EventServer.h.
void parseState | ( | Info * | i | ) | [inline] |
reads i->buffer at i->cursor position and determines the command which was sent by a client.
It modifies i->state and i->cursor
Definition at line 1475 of file EventServer.cpp.
void pipeCallback | ( | int | fd, | |
short | event, | |||
void * | arg | |||
) | [inline] |
hollow callback.
Used to avoid event_base_loop from ever returning
Definition at line 250 of file EventServer.cpp.
void printBinaryBuffer | ( | void * | buffer, | |
int | n, | |||
const char * | label = NULL | |||
) |
For debug purpose only.
Prints binary buffer's content in programmer - friendly form.
Definition at line 1583 of file EventServer.cpp.
void printBinaryCommand | ( | uint8_t | cmd | ) |
For debug purpose only.
Prints request type in human-readable form.
Definition at line 1617 of file EventServer.cpp.
void readCallback | ( | int | socket, | |
short | event, | |||
void * | arg | |||
) | [inline] |
whole parsing of the clients' input.
90% of server's job is done in this function. To lessen the number of calls to event_del and event_add we invoke one send() at the end of readCallback. If this will send whole response we do not have to register writeCallback (which is almost always)
Definition at line 292 of file EventServer.cpp.
void resetBuffer | ( | Info * | i | ) | [inline] |
when many requests are sent in one package and there is too little place for a new request pending ones are copied to the beginning of i->buffer.
If the amount of free space in i->buffer is sufficient copying is not done. In case if whole recieved message was parsed i->cursor and i->offset are reset.
Definition at line 1462 of file EventServer.cpp.
void writeCallback | ( | int | socket, | |
short | event, | |||
void * | arg | |||
) | [inline] |
function that cares about sending response to clients.
It is used only when send() at the end of the readCallback() will not send whole message.
Definition at line 1310 of file EventServer.cpp.