Commit 8607e4b3 authored by /jdi/'s avatar /jdi/
Browse files

receive handler

parent 1ec3b6c4
......@@ -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) {
......
......@@ -8,37 +8,39 @@
#ifndef TYPES_H_
#define TYPES_H_
#include <functional>
#include <vector>
#include <map>
template <typename T>
std::vector<T> operator+(const std::vector<T> &A, const std::vector<T> &B)
{
std::vector<T> 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<typename T>
std::vector<T> operator+(const std::vector<T> &A, const std::vector<T> &B) {
std::vector<T> 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 <typename T>
std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &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<typename T>
std::vector<T> &operator+=(std::vector<T> &A, const std::vector<T> &B) {
A.reserve(A.size() + B.size());
A.insert(A.end(), B.begin(), B.end());
return A;
}
typedef std::vector<unsigned char> bytes;
typedef unsigned char byte;
struct dataset{
short type;
short len;
bytes value;
struct dataset {
short type;
short len;
bytes value;
};
//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_ */
......@@ -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] <command>\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++;
}
}
......
......@@ -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();
}
}
});
......
......@@ -9,6 +9,7 @@
#define SOCKET_H_
#include <asio.hpp>
#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_;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment