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