Conscience Core
HttpServer.h
Go to the documentation of this file.
1 #ifndef MJPG_STREAMER_HttpServer_H
2 #define MJPG_STREAMER_HttpServer_H
3 
4 #include "CscCommon.h"
5 #include "FrameSource.h"
6 #include "utils.h"
7 using std::weak_ptr;
8 
9 #define IO_BUFFER 256
10 #define BUFFER_SIZE 1024
11 
12 /* the boundary is used for the M-JPEG stream, it separates the multipart stream of pictures */
13 #define HTTP_SERVER_BOUNDARY "_mjpg_boundarydonotcross"
14 
15 /*
16  * this defines the buffer size for a JPG-frame
17  * selecting to large values will allocate much wasted RAM for each buffer
18  * selecting to small values will lead to crashes due to to small buffers
19  */
20 #define MAX_FRAME_SIZE (256*1024)
21 #define TEN_K (10*1024)
22 
23 /*
24  * Standard header to be send along with other header information like mimetype.
25  *
26  * The parameters should ensure the browser does not cache our answer.
27  * A browser should connect for each file and not serve files from his cache.
28  * Using cached pictures would lead to showing old/outdated pictures
29  * Many browser seem to ignore, or at least not always obey those headers
30  * since i observed caching of files from time to time.
31  */
32 #define STD_HEADER "Connection: close\r\n" \
33  "Server: MJPG-Streamer/0.2\r\n" \
34  "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" \
35  "Pragma: no-cache\r\n" \
36  "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"
37 
38 /*
39  * Maximum number of server sockets (i.e. protocol families) to listen.
40  */
41 #define MAX_SD_LEN 50
42 
44  /*
45  * Only the following fileypes are supported.
46  *
47  * Other filetypes are simply ignored!
48  * This table is a 1:1 mapping of files extension to a certain mimetype.
49  */
50  static const struct {
51  const char *dot_extension;
52  const char *mimetype;
53  } mimetypes[] = {
54  { ".html", "text/html" },
55  { ".htm", "text/html" },
56  { ".css", "text/css" },
57  { ".js", "text/javascript" },
58  { ".txt", "text/plain" },
59  { ".jpg", "image/jpeg" },
60  { ".jpeg", "image/jpeg" },
61  { ".png", "image/png"},
62  { ".gif", "image/gif" },
63  { ".ico", "image/x-icon" },
64  { ".swf", "application/x-shockwave-flash" },
65  { ".cab", "application/x-shockwave-flash" },
66  { ".jar", "application/java-archive" },
67  { ".json", "application/json" }
68  };
69 
70  /* the webserver determines between these values for an answer */
71  typedef enum {
80  } answer_t;
81 
82  /*
83  * the client sends information with each request
84  * this structure is used to store the important parts
85  */
86  typedef struct {
88  char *parameter;
89  char *client;
90  char *credentials;
91  } request;
92 
93  /* the iobuffer structure is used to read from the HTTP-client */
94  typedef struct {
95  int level; /* how full is the buffer */
96  char buffer[IO_BUFFER]; /* the data */
97  } iobuffer;
98 
100 
102  public:
106  HttpServer(unsigned short port, FrameSourceProvider * frameSourceProvider);
107  ~HttpServer();
108 
112  void stop();
113  private:
114  bool running = true;
115  void cleanup();
116  void run();
117 
118  unique_ptr<CscLogger> logger = nullptr;
119  unsigned short port;
120  vector<int> sockets;
121  FrameSourceProvider *const frameSourceProvider;
122 
123  vector<weak_ptr<ClientContext>> clients;
124  void handleClient(ptr<ClientContext> clientContext);
125  void sendSnapshot(ptr<ClientContext> clientContext, ptr<FrameSource> frameSource);
126  void sendStream(ptr<ClientContext> clientContext, ptr<FrameSource> frameSource);
127  void sendError(int fd, int which, const string &message);
128  };
129 
130  /*
131  * this struct is just defined to allow passing all necessary details to a worker thread
132  * "cfd" is for connected/accepted filedescriptor
133  */
135  const int fd;
136  bool stopped = false;
137  ClientContext(int fd): fd(fd) {
138  }
139  };
140 }
141 
142 #endif
conscience_core::mjpg_streamer::iobuffer
Definition: HttpServer.h:94
conscience_core::mjpg_streamer::iobuffer::level
int level
Definition: HttpServer.h:95
conscience_core::mjpg_streamer::request::type
answer_t type
Definition: HttpServer.h:87
conscience_core::mjpg_streamer::FrameSourceProvider
Definition: FrameSource.h:94
conscience_core::mjpg_streamer::answer_t
answer_t
Definition: HttpServer.h:71
IO_BUFFER
#define IO_BUFFER
Definition: HttpServer.h:9
conscience_core::mjpg_streamer::mimetype
const char * mimetype
Definition: HttpServer.h:52
CSC_DLL_IMPORTEXPORT
#define CSC_DLL_IMPORTEXPORT
Definition: os.h:34
conscience_core::mjpg_streamer::A_COMMAND
@ A_COMMAND
Definition: HttpServer.h:75
conscience_core::mjpg_streamer::A_INPUT_JSON
@ A_INPUT_JSON
Definition: HttpServer.h:77
conscience_core::mjpg_streamer::request
Definition: HttpServer.h:86
conscience_core::mjpg_streamer::ClientContext
Definition: HttpServer.h:134
conscience_core::mjpg_streamer::A_STREAM
@ A_STREAM
Definition: HttpServer.h:74
conscience_core::mjpg_streamer::dot_extension
const char * dot_extension
Definition: HttpServer.h:51
logger
static std::unique_ptr< CscLogger > logger
Definition: gltfHelpers.cpp:6
conscience_core::mjpg_streamer::request::parameter
char * parameter
Definition: HttpServer.h:88
conscience_core::mjpg_streamer::request::client
char * client
Definition: HttpServer.h:89
utils.h
conscience_core::mjpg_streamer::ClientContext::fd
const int fd
Definition: HttpServer.h:135
conscience_core::mjpg_streamer
Definition: CscMJPGStreamer.cpp:32
CscCommon.h
conscience_core::mjpg_streamer::A_FILE
@ A_FILE
Definition: HttpServer.h:76
conscience_core::mjpg_streamer::A_PROGRAM_JSON
@ A_PROGRAM_JSON
Definition: HttpServer.h:79
conscience_core::mjpg_streamer::A_OUTPUT_JSON
@ A_OUTPUT_JSON
Definition: HttpServer.h:78
conscience_core::mjpg_streamer::A_UNKNOWN
@ A_UNKNOWN
Definition: HttpServer.h:72
FrameSource.h
conscience_core::mjpg_streamer::A_SNAPSHOT
@ A_SNAPSHOT
Definition: HttpServer.h:73
conscience_core::mjpg_streamer::HttpServer
Definition: HttpServer.h:101
conscience_core::mjpg_streamer::mimetypes
static const struct conscience_core::mjpg_streamer::@0 mimetypes[]
conscience_core::mjpg_streamer::request::credentials
char * credentials
Definition: HttpServer.h:90
ptr
std::shared_ptr< T > ptr
Definition: CscCommon.h:29
conscience_core::mjpg_streamer::ClientContext::ClientContext
ClientContext(int fd)
Definition: HttpServer.h:137