blob: 76fbd9e764f71935ddb4aa77bd60123401ca9e8b [file] [log] [blame]
Brian Silverman6ae77dd2013-03-29 22:28:08 -07001#ifndef FRC971_VISION_BINARY_SERVER_H_
2#define FRC971_VISION_BINARY_SERVER_H_
3#include "event2/buffer.h"
4#include "event2/event.h"
5#include "event2/listener.h"
6#include "event2/bufferevent.h"
7#include "aos/common/mutex.h"
8
9#include <sys/types.h>
10#include <sys/socket.h>
11#include "PacketNotifier.h"
12
13namespace frc971 {
14namespace vision {
15
16/* This runs the libevent loop and interfaces in with the sockets provided from the PacketNotifier
17 * to allow a secondary process to focus primarily on doing processing and then feeding this task.
18 */
19class BinaryServer {
20 public:
21 BinaryServer(uint16_t port,PacketNotifier *notify);
22
23
24 void StartServer(uint16_t port);
25 private:
26 event_base *const _base;
27 int _fd;
28 PacketNotifier *_notify;
29 bool have_id;
30 int _client_fd;
31 struct event *frame_notify;
32 struct evbuffer *_output;
33 struct bufferevent *_bufev;
34 struct evconnlistener *listener;
35 void Accept(evconnlistener *listener, evutil_socket_t fd,
36 struct sockaddr* /*address*/, int /*socklen*/);
37 void Notify(int /*fd*/, short /*what*/);
38 void ErrorEvent(struct bufferevent *bev,short events);
39 static void StaticAccept(evconnlistener *listener, evutil_socket_t fd,
40 struct sockaddr *address, int socklen,void *self){
41 ((BinaryServer *)(self))->Accept(listener, fd, address, socklen);
42 }
43 static void StaticNotify(int fd, short what, void *self){
44 ((BinaryServer *)(self))->Notify(fd, what);
45 }
46 static void StaticErrorEvent(struct bufferevent *bev,short events,void *self){
47 ((BinaryServer *)(self))->ErrorEvent(bev,events);
48 }
49};
50
51} // namespace vision
52} // namespace frc971
53
54#endif // FRC971_VISION_BINARY_SERVER_H_