-
Notifications
You must be signed in to change notification settings - Fork 21
/
Servlet.h
57 lines (48 loc) · 1.47 KB
/
Servlet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Servlet.h -- May 21, 2009
// by geohot
// part of "The Embedded Disassembler"
// released under GPLv3, see http://gplv3.fsf.org/
//
// hehe, it's like Java, except Java sucks
//
// For saving, EDA doesn't waste time trying to save data structures
// It just saves the server accesses
// My laziness is awesome
// Although this is probably mad slow
#include <string>
#include <vector>
#include <map>
#ifdef WIN32 //Windows sockets
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#define SHUT_RDWR SD_SEND
#define socklen_t int
#else //Linux sockets
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#endif
namespace eda {
class FactoryOwner;
template<class T>
class Servlet {
public:
typedef bool (T::*callback)(const std::vector<std::string>& argv, std::string* out);
void RegisterCommandHandler(const std::string& command, T* mem, callback handle);
bool StartServer(int port); // this is the main server run loop
bool EndServer();
private:
bool RunLoop();
bool AcceptClient(); // To be called by RunLoop
bool ProcessRequest(const std::string& in, std::string* out);
std::map<std::string, std::pair<T*, callback> > command_table_;
typedef typename std::map<std::string, std::pair<T*, callback> >::iterator CommandTableIterator;
//std::vector<int> client_sockets_;
int client_socket_;
struct sockaddr_in socket_address_;
int socket_;
};
};