Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
liangdong committed Oct 23, 2012
0 parents commit 013ac65
Show file tree
Hide file tree
Showing 26 changed files with 1,306 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
server:src/server.o src/listener.o src/client.o src/main.o src/plugin.o
g++ -o $@ src/server.o src/listener.o src/client.o src/main.o src/plugin.o -levent -ldl

src/server.o:src/server.cpp include/server.h
g++ -o $@ -c $< -I./include

src/listener.o:src/listener.cpp include/listener.h include/util.h
g++ -o $@ -c $< -I./include

src/client.o:src/client.cpp include/client.h include/util.h
g++ -o $@ -c $< -I./include

src/main.o:src/main.cpp include/server.h
g++ -o $@ -c $< -I./include

src/plugin.o:src/plugin.cpp include/plugin.h
g++ -o $@ -c $< -I./include

dist:all
tar --exclude=server.tar.gz -czvf server.tar.gz .
clean:
rm -f src/*.o server
73 changes: 73 additions & 0 deletions include/client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef _CLIENT_H
#define _CLIENT_H

#include "event2/event.h"
#include "event2/util.h"
#include "util.h"
#include "plugin.h"

#include <string>

enum ClientStatus
{
BEFORE_REQUEST,
ON_REQUEST,
AFTER_REQUEST,
BEFORE_RESPONSE,
ON_RESPONSE,
AFTER_RESPONSE,
};

class Server;

struct Client
{
Server *m_server;

evutil_socket_t m_sockfd;
struct sockaddr_in m_client_addr;
struct event *m_event;

std::string m_inbuf;
std::string m_intemp;
std::string m_outbuf;

std::string m_request;
std::string m_response;

ClientStatus m_status;

void* *m_plugin_data_slots;
int m_plugin_count;

bool m_want_write;
bool m_want_read;

Client();
~Client();

bool InitClient(Server *server);

void WantRead();
void NotWantRead();
void WantWrite();
void NotWantWrite();
void SetWriteEvent();
void UnsetWriteEvent();
bool StatusMachine();
void SetStatus(ClientStatus status);

bool MakePluginDataSlots();
void DelPluginDataSlots();

bool PluginBeforeRequest();
bool PluginOnRequest();
bool PluginAfterRequest();
bool PluginBeforeResponse();
PluginStatus PluginOnResponse();
bool PluginAfterResponse();

static void ClientEventCallback(evutil_socket_t sockfd, short event, void *userdata);
};

#endif
27 changes: 27 additions & 0 deletions include/listener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _LISTENER_H
#define _LISTENER_H

#include "event2/event.h"
#include "event2/util.h"
#include "util.h"

#include <string>

class Server;

struct Listener
{
Server *m_server;
evutil_socket_t m_sockfd;
struct sockaddr_in m_listen_addr;
struct event *m_event;
uint64_t m_count_client;

Listener(const std::string &ip, short port);
~Listener();
void InitListener(Server *server);

static void ListenerEventCallback(evutil_socket_t sockfd, short event, void *userdata);
};

#endif
44 changes: 44 additions & 0 deletions include/plugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef _PLUGIN_H
#define _PLUGIN_H

class Server;
class Client;

enum PluginStatus
{
OK,
NOT_OK,
ERROR,
};

class Plugin
{
public:
typedef Plugin* (*SetupPlugin)();
typedef void (*RemovePlugin)(Plugin *plugin);

virtual bool OnLoad(Server *server, int plugin_index);
virtual bool OnInit(Client *client, int plugin_index);
virtual bool BeforeRequest(Client *client, int plugin_index);
virtual bool OnRequest(Client *client, int plugin_index);
virtual bool AfterRequest(Client *client, int plugin_index);
virtual bool BeforeResponse(Client *client, int plugin_index);
virtual PluginStatus OnResponse(Client *client, int plugin_index);
virtual bool AfterResponse(Client *client, int plugin_index);
virtual void OnClose(Client *client, int plugin_index);
virtual void OnDestroy(Server *server, int plugin_index);
Plugin();
virtual ~Plugin();

void *m_plugin_data;
void *m_plugin_so;
int m_plugin_index;
bool m_is_loaded;

SetupPlugin m_setup_plugin;
RemovePlugin m_remove_plugin;
};

extern const char * plugin_config[]; //add plugin in plugin.cpp

#endif
44 changes: 44 additions & 0 deletions include/server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef _SERVER_H
#define _SERVER_H

#include "event2/event.h"
#include "event2/util.h"

#include "listener.h"

#include <map>
#include <string>

class Client;
class Plugin;

class Server
{
public:
typedef std::map<evutil_socket_t, Client*> ClientMap;
typedef ClientMap::iterator ClientMapIter;

Server(const std::string &ip, short port);
~Server();

bool StartServer();

static void ServerExitSignal(evutil_socket_t signo, short, void *userdata);

Plugin* *m_plugins;
int m_plugin_count;

struct Listener m_listener;
struct event_base *m_server_base;
struct event *m_exit_event;
ClientMap m_client_map;

private:
bool SetupPlugins();
void RemovePlugins();

bool LoadPlugins();
void UnloadPlugins();
};

#endif
13 changes: 13 additions & 0 deletions include/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _UTIL_H
#define _UTIL_H

#include <signal.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <errno.h>
#include <stdint.h>

#endif
87 changes: 87 additions & 0 deletions plugin/fake_mysql/'
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "plugin.h"
#include "client.h"

#include <stdlib.h>
#include <string.h>

#include <pthread.h>

#include <iostream>
#include <queue>

#define PNAME "[PluginMysql] "

#define TRACE() do { std::cerr << PNAME << __FUNCTION__ << std::endl; } while(0)

class PluginMysql: public Plugin
{
public:
virtual bool OnLoad(Server *server, int plugin_index)
{
TRACE();

pthread_mutex_init(&m_mutex, NULL);
pthread_cond_init(&m_cond, NULL);
pthread_create(&m_tid, NULL, MysqlThread, this);

return true;
}
virtual void OnDestroy(Server *server, int plugin_index)
{
TRACE();
pthread_join(m_tid, NULL);
}
virtual bool OnInit(Client *client, int plugin_index)
{
TRACE();
return true;
}
virtual void OnClose(Client *client, int plugin_index)
{
TRACE();
}
virtual bool BeforeRequest(Client *client, int plugin_index)
{
TRACE();
return true;
}
virtual bool AfterRequest(Client *client, int plugin_index)
{
TRACE();
return true;
}
virtual PluginStatus OnResponse(Client *client, int plugin_index)
{
TRACE();
return OK;
}

static void * MysqlThread(void *arg)
{
TRACE();

PluginMysql *plugin = (PluginMysql*)arg;

pthread_mutex_lock();
while ()

return NULL;
}

private:
pthread_t m_tid;
pthread_mutex_t m_mutex;
pthread_cond_t m_cond;
std::queue<Client*> m_task_queue;
};

extern "C" Plugin* SetupPlugin()
{
TRACE();
return new PluginMysql();
}
extern "C" void RemovePlugin(Plugin *plugin)
{
TRACE();
delete plugin;
}
11 changes: 11 additions & 0 deletions plugin/fake_mysql/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugin_fake_mysql.so:fake_mysql.o ../../src/plugin.o
g++ -o $@ fake_mysql.o ../../src/plugin.o -shared -lpthread -levent

fake_mysql.o:fake_mysql.cpp ../../include/plugin.h
g++ -fPIC -o $@ -c $< -I../../include

../../src/plugin.o:../../src/plugin.cpp ../../include/plugin.h
g++ -fPIC -o $@ -c $< -I../../include

clean:
rm -rf ../../src/plugin.o *.o *.so
Loading

0 comments on commit 013ac65

Please sign in to comment.