receive handler

This commit is contained in:
/jdi/ 2015-09-26 01:24:03 +02:00
parent 1ec3b6c4f0
commit 8607e4b370
5 changed files with 51 additions and 37 deletions

View file

@ -39,6 +39,22 @@ int Program::list() {
asio::io_service io_service; asio::io_service io_service;
Socket s(io_service); Socket s(io_service);
s.init(dst_port, src_port); 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); s.send(a);
io_service.run(); io_service.run();
} catch (std::exception& e) { } catch (std::exception& e) {

View file

@ -8,12 +8,12 @@
#ifndef TYPES_H_ #ifndef TYPES_H_
#define TYPES_H_ #define TYPES_H_
#include <functional>
#include <vector> #include <vector>
#include <map> #include <map>
template<typename T> template<typename T>
std::vector<T> operator+(const std::vector<T> &A, const std::vector<T> &B) std::vector<T> operator+(const std::vector<T> &A, const std::vector<T> &B) {
{
std::vector<T> AB; std::vector<T> AB;
AB.reserve(A.size() + B.size()); // preallocate memory AB.reserve(A.size() + B.size()); // preallocate memory
AB.insert(AB.end(), A.begin(), A.end()); // add A; AB.insert(AB.end(), A.begin(), A.end()); // add A;
@ -22,11 +22,10 @@ std::vector<T> operator+(const std::vector<T> &A, const std::vector<T> &B)
} }
template<typename T> template<typename T>
std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &B) std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &B) {
{ A.reserve(A.size() + B.size());
A.reserve( A.size() + B.size() ); // preallocate memory without erase original data A.insert(A.end(), B.begin(), B.end());
A.insert( A.end(), B.begin(), B.end() ); // add B; return A;
return A; // here A could be named AB
} }
typedef std::vector<unsigned char> bytes; typedef std::vector<unsigned char> bytes;
@ -41,4 +40,7 @@ struct dataset{
//typedef std::vector<dataset> datasets; //typedef std::vector<dataset> datasets;
typedef std::map<short, dataset> datasets; typedef std::map<short, dataset> datasets;
//std::function<int()>;
//typedef int receiveCallback;
#endif /* TYPES_H_ */ #endif /* TYPES_H_ */

View file

@ -17,7 +17,7 @@
#include "Program.h" #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] <command>\n"
#define flag_version 1 #define flag_version 1
#define flag_help 2 #define flag_help 2
@ -77,19 +77,20 @@ int main(int argc, char *argv[]) {
if (optind < argc) { if (optind < argc) {
if (strcmp(argv[optind], "list") == 0) { if (strcmp(argv[optind], "list") == 0) {
optind++;
if (p.list()) if (p.list())
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
else else
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else { } else {
printf("Unknown Command: %s\n", argv[optind]); printf("Unknown command: %s\n", argv[optind]);
optind++;
while (optind < argc) { while (optind < argc) {
printf("->%s\n", argv[optind]); printf("->%s\n", argv[optind]);
optind++; optind++;
} }
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
optind++;
} }
} }

View file

@ -67,21 +67,10 @@ void Socket::listen() {
p.encode(data_); p.encode(data_);
p.parse(data_); p.parse(data_);
datasets l = p.getPayload(); datasets l = p.getPayload();
utils::printSets(l); if(!callback(p)) {
/*
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(); listen();
} }
}
}); });
//printf("Listen\n"); //printf("Listen\n");

View file

@ -9,6 +9,7 @@
#define SOCKET_H_ #define SOCKET_H_
#include <asio.hpp> #include <asio.hpp>
#include "Packet.h"
#include "../Types.h" #include "../Types.h"
#define MAX_LENGTH 1024 #define MAX_LENGTH 1024
@ -16,10 +17,15 @@
class Socket { class Socket {
public: public:
Socket(asio::io_service&); Socket(asio::io_service&);
virtual ~Socket(){}; virtual ~Socket() {
}
void init(short, short); void init(short, short);
void send(bytes); void send(bytes);
void listen(); void listen();
int (*callback)(Packet)=[](Packet a) {
return 0;
};
private: private:
asio::ip::udp::socket send_socket_; asio::ip::udp::socket send_socket_;
asio::ip::udp::socket receive_socket_; asio::ip::udp::socket receive_socket_;