Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 1 | #ifndef FRC971_VISION_BINARY_SERVER_H_ |
| 2 | #define FRC971_VISION_BINARY_SERVER_H_ |
Brian Silverman | 5b3e51e | 2013-03-29 22:53:44 -0700 | [diff] [blame] | 3 | |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/socket.h> |
| 6 | |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 7 | #include "event2/buffer.h" |
| 8 | #include "event2/event.h" |
| 9 | #include "event2/listener.h" |
| 10 | #include "event2/bufferevent.h" |
Brian Silverman | 5b3e51e | 2013-03-29 22:53:44 -0700 | [diff] [blame] | 11 | |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 12 | #include "aos/common/mutex.h" |
| 13 | |
Brian Silverman | 5b3e51e | 2013-03-29 22:53:44 -0700 | [diff] [blame] | 14 | #include "vision/PacketNotifier.h" |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 15 | |
| 16 | namespace frc971 { |
| 17 | namespace 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 | */ |
| 22 | class 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_ |