got vision code actually working (and somewhat tuned) but it crashes
diff --git a/vision/BinaryServer.cpp b/vision/BinaryServer.cpp
index 29deee9..18cf85b 100644
--- a/vision/BinaryServer.cpp
+++ b/vision/BinaryServer.cpp
@@ -18,7 +18,7 @@
namespace frc971 {
namespace vision {
-static void echo_read_cb(struct bufferevent *bev, void * /*ctx*/){
+static void echo_read_cb(struct bufferevent *bev, void * /*ctx*/) {
struct evbuffer *input = bufferevent_get_input(bev);
struct evbuffer *output = bufferevent_get_output(bev);
@@ -32,94 +32,98 @@
evbuffer_add_buffer(output, input);
}
-void BinaryServer::ErrorEvent(struct bufferevent *bev, short events){
- if (events & BEV_EVENT_ERROR)
- perror("Error from bufferevent");
+void BinaryServer::ErrorEvent(struct bufferevent *bev, short events) {
+ if (events & BEV_EVENT_ERROR) perror("Error from bufferevent");
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
have_id = false;
bufferevent_free(bev);
}
}
-void BinaryServer::Accept(struct evconnlistener *listener,
- evutil_socket_t fd, struct sockaddr * /*address*/, int /*socklen*/){
+void BinaryServer::Accept(struct evconnlistener *listener, evutil_socket_t fd,
+ struct sockaddr * /*address*/, int /*socklen*/) {
struct event_base *base = evconnlistener_get_base(listener);
- if(!have_id){
- struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
+ if (!have_id) {
+ struct bufferevent *bev =
+ bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
_output = bufferevent_get_output(bev);
_bufev = bev;
have_id = true;
int no_delay_flag = 1;
- setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &no_delay_flag, sizeof(no_delay_flag));
+ setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &no_delay_flag,
+ sizeof(no_delay_flag));
bufferevent_setcb(bev, echo_read_cb, NULL, StaticErrorEvent, this);
bufferevent_enable(bev, EV_READ | EV_WRITE);
}
}
-static void accept_error_cb(struct evconnlistener *listener, void * /*ctx*/)
-{
+static void accept_error_cb(struct evconnlistener *listener, void * /*ctx*/) {
struct event_base *base = evconnlistener_get_base(listener);
int err = EVUTIL_SOCKET_ERROR();
fprintf(stderr, "Got an error %d (%s) on the listener. "
- "Shutting down.\n", err, evutil_socket_error_to_string(err));
+ "Shutting down.\n",
+ err, evutil_socket_error_to_string(err));
event_base_loopexit(base, NULL);
}
-void BinaryServer::StartServer(uint16_t port){
+void BinaryServer::StartServer(uint16_t port) {
_fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
- sin.sin_port = htons( port );
+ sin.sin_port = htons(port);
sin.sin_addr.s_addr = inet_addr("0.0.0.0");
- listener = evconnlistener_new_bind(_base, StaticAccept, this,
- LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1,
- (struct sockaddr *) (void *)&sin, sizeof(sin));
+ listener = evconnlistener_new_bind(
+ _base, StaticAccept, this, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1,
+ (struct sockaddr *)(void *)&sin, sizeof(sin));
if (!listener) {
- fprintf(stderr,"%s:%d: Couldn't create listener\n", __FILE__, __LINE__);
+ fprintf(stderr, "%s:%d: Couldn't create listener\n", __FILE__, __LINE__);
exit(-1);
}
evconnlistener_set_error_cb(listener, accept_error_cb);
}
-void BinaryServer::Notify(int fd,short /*what*/){
+void BinaryServer::Notify(int fd, short /*what*/) {
char notes[4096];
- int count = read(fd,notes,4096);
- if(count == 0){
+ int count = read(fd, notes, 4096);
+ if (count == 0) {
close(fd);
- fprintf(stderr,"%s:%d: Error No cheeze from OpenCV task!!!\n",__FILE__,__LINE__);
+ fprintf(stderr, "%s:%d: Error No cheeze from OpenCV task!!!\n", __FILE__,
+ __LINE__);
exit(-1);
}
- printf("notified!: %d\n",count);
- if(have_id){
+ printf("notified!: %d\n", count);
+ if (have_id) {
printf("got someone to read my stuff!\n");
char *binary_data;
size_t len;
- if(_notify->GetData(&binary_data,&len)){
+ if (_notify->GetData(&binary_data, &len)) {
printf("here is for sending\n");
- evbuffer_add_reference(_output, binary_data, len, PacketNotifier::StaticDataSent,_notify);
- printf("this is how sending went %d\n",bufferevent_flush(_bufev,EV_WRITE, BEV_FLUSH));
+ evbuffer_add_reference(_output, binary_data, len,
+ PacketNotifier::StaticDataSent, _notify);
+ printf("this is how sending went %d\n",
+ bufferevent_flush(_bufev, EV_WRITE, BEV_FLUSH));
}
}
}
-//Constructor
-BinaryServer::BinaryServer(uint16_t port,
- frc971::vision::PacketNotifier *notify) :
- _base(event_base_new()) {
- have_id = false;
- StartServer(port);
- _notify = notify;
- frame_notify = event_new(_base, notify->RecieverFD(),
- EV_READ|EV_PERSIST, StaticNotify, this);
- event_add(frame_notify,NULL);
- event_base_dispatch(_base);
- }
+// Constructor
+BinaryServer::BinaryServer(uint16_t port,
+ frc971::vision::PacketNotifier *notify)
+ : _base(event_base_new()) {
+ have_id = false;
+ StartServer(port);
+ _notify = notify;
+ frame_notify = event_new(_base, notify->RecieverFD(), EV_READ | EV_PERSIST,
+ StaticNotify, this);
+ event_add(frame_notify, NULL);
+ event_base_dispatch(_base);
+}
} // namespace vision
} // namespace frc971