ffead.server.doc
Server.h
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * Server.h
18  *
19  * Created on: Jan 2, 2010
20  * Author: sumeet
21  */
22 
23 #ifndef SERVER_H_
24 #define SERVER_H_
25 #include "iostream"
26 #include <unistd.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <arpa/inet.h>
33 #include <sys/wait.h>
34 #include <signal.h>
35 #include "string"
36 #include "cstring"
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "vector"
40 #include "sstream"
41 #include <fcntl.h>
42 #include <sys/ioctl.h>
43 /*Fix for Windows Cygwin*///#include <sys/epoll.h>
44 #include <sys/resource.h>
45 #include "Thread.h"
46 #include "Logger.h"
47 #define MAXEPOLLSIZES 10000
48 #define BACKLOGM 500
49 using namespace std;
50 
51 typedef void* (*Service)(void*);
52 class Server {
53  Logger logger;
54  int sock;
55  Service service;
56  struct sockaddr_storage their_addr;
57  static void* servicing(void* arg);
58  bool runn;
59 public:
60  Server();
61  Server(string,bool,int,Service,int);
62  Server(string port,int waiting,Service serv);
63  virtual ~Server();
64  int Accept();
65  int Send(int,string);
66  int Send(int,vector<char>);
67  int Send(int,vector<unsigned char>);
68  int Send(int,char*);
69  int Send(int,unsigned char*);
70  int Receive(int,string&,int);
71  int Receive(int,vector<char>&,int);
72  int Receive(int,vector<unsigned char>&,int);
73  int Receive(int,char *data,int);
74  int Receive(int,unsigned char *data,int);
75  int Receive(int,vector<string>&,int);
76  void stop()
77  {
78  runn = false;
79  }
80  static int createListener(string port,bool block);
81  static int createListener(string ipAddress,string port,bool block);
82 };
83 
84 #endif /* SERVER_H_ */