From 8607e4b370f9b189478ffbbcf867304367640531 Mon Sep 17 00:00:00 2001 From: /jdi/ Date: Sat, 26 Sep 2015 01:24:03 +0200 Subject: [PATCH] receive handler --- src/Program.cpp | 16 ++++++++++++++++ src/Types.h | 40 +++++++++++++++++++++------------------- src/smrtlink.cpp | 7 ++++--- src/transfer/Socket.cpp | 17 +++-------------- src/transfer/Socket.h | 8 +++++++- 5 files changed, 51 insertions(+), 37 deletions(-) diff --git a/src/Program.cpp b/src/Program.cpp index a9f6d24..cd60dab 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -39,6 +39,22 @@ int Program::list() { asio::io_service io_service; Socket s(io_service); s.init(dst_port, src_port); + s.callback = [](Packet a) { + utils::printSets(a.getPayload()); + + /* + sleep(1); + bytes b = {255, 255, 0, 0}; + Host h = Host(); + p = Packet(Packet::DISCOVERY); + p.setBody(b); + p.setHostMac(h.getMac()); + bytes a = p.getBytes(); + p.encode(a); + send(a); + */ + return 1; + }; s.send(a); io_service.run(); } catch (std::exception& e) { diff --git a/src/Types.h b/src/Types.h index cbf5d60..f90487f 100644 --- a/src/Types.h +++ b/src/Types.h @@ -8,37 +8,39 @@ #ifndef TYPES_H_ #define TYPES_H_ +#include #include #include -template -std::vector operator+(const std::vector &A, const std::vector &B) -{ - std::vector AB; - AB.reserve( A.size() + B.size() ); // preallocate memory - AB.insert( AB.end(), A.begin(), A.end() ); // add A; - AB.insert( AB.end(), B.begin(), B.end() ); // add B; - return AB; +template +std::vector operator+(const std::vector &A, const std::vector &B) { + std::vector AB; + AB.reserve(A.size() + B.size()); // preallocate memory + AB.insert(AB.end(), A.begin(), A.end()); // add A; + AB.insert(AB.end(), B.begin(), B.end()); // add B; + return AB; } -template -std::vector &operator+=(std::vector &A, const std::vector &B) -{ - A.reserve( A.size() + B.size() ); // preallocate memory without erase original data - A.insert( A.end(), B.begin(), B.end() ); // add B; - return A; // here A could be named AB +template +std::vector &operator+=(std::vector &A, const std::vector &B) { + A.reserve(A.size() + B.size()); + A.insert(A.end(), B.begin(), B.end()); + return A; } typedef std::vector bytes; typedef unsigned char byte; -struct dataset{ - short type; - short len; - bytes value; +struct dataset { + short type; + short len; + bytes value; }; //typedef std::vector datasets; -typedef std::map datasets; +typedef std::map datasets; + +//std::function; +//typedef int receiveCallback; #endif /* TYPES_H_ */ diff --git a/src/smrtlink.cpp b/src/smrtlink.cpp index ba4dcc7..db523c1 100644 --- a/src/smrtlink.cpp +++ b/src/smrtlink.cpp @@ -17,7 +17,7 @@ #include "Program.h" -#define USAGE "Usage: smrtlink [-p n|--port n] [-h|--help] [-v|--version] name\n" +#define USAGE "Usage: smrtlink [-p n|--port n] [-h|--help] [-v|--version] \n" #define flag_version 1 #define flag_help 2 @@ -77,19 +77,20 @@ int main(int argc, char *argv[]) { if (optind < argc) { if (strcmp(argv[optind], "list") == 0) { + optind++; if (p.list()) exit(EXIT_SUCCESS); else exit(EXIT_FAILURE); } else { - printf("Unknown Command: %s\n", argv[optind]); + printf("Unknown command: %s\n", argv[optind]); + optind++; while (optind < argc) { printf("->%s\n", argv[optind]); optind++; } exit(EXIT_FAILURE); } - optind++; } } diff --git a/src/transfer/Socket.cpp b/src/transfer/Socket.cpp index 7dd5d7a..00a3bb1 100644 --- a/src/transfer/Socket.cpp +++ b/src/transfer/Socket.cpp @@ -67,20 +67,9 @@ void Socket::listen() { p.encode(data_); p.parse(data_); datasets l = p.getPayload(); - utils::printSets(l); - - /* - sleep(1); - bytes b = {255, 255, 0, 0}; - Host h = Host(); - p = Packet(Packet::DISCOVERY); - p.setBody(b); - p.setHostMac(h.getMac()); - bytes a = p.getBytes(); - p.encode(a); - send(a); - */ - listen(); + if(!callback(p)) { + listen(); + } } }); diff --git a/src/transfer/Socket.h b/src/transfer/Socket.h index d0332dd..a057a09 100644 --- a/src/transfer/Socket.h +++ b/src/transfer/Socket.h @@ -9,6 +9,7 @@ #define SOCKET_H_ #include +#include "Packet.h" #include "../Types.h" #define MAX_LENGTH 1024 @@ -16,10 +17,15 @@ class Socket { public: Socket(asio::io_service&); - virtual ~Socket(){}; + virtual ~Socket() { + } void init(short, short); void send(bytes); void listen(); + int (*callback)(Packet)=[](Packet a) { + return 0; + }; + private: asio::ip::udp::socket send_socket_; asio::ip::udp::socket receive_socket_;