-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialization.h
More file actions
100 lines (79 loc) · 2.61 KB
/
Serialization.h
File metadata and controls
100 lines (79 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef PRACTICE6_SERIALIZATION_H
#define PRACTICE6_SERIALIZATION_H
#include <iostream>
#include <unistd.h>
#include <string>
#include <sstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "src/sockets/Tcp.h"
#include "Driver.h"
#include "DetailsDriver.h"
using namespace std;
//receive.
template <class T>
T* receive(Tcp* tcp, int descriptor = 0) {
T* obj;
char buffer[65536];
tcp->receiveData(buffer, sizeof(buffer), descriptor);
//cout << buffer << endl;
string str(buffer, sizeof(buffer));
//cout << str;
boost::iostreams::basic_array_source<char> device(str.c_str(), str.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char> > s2(device);
boost::archive::binary_iarchive ia(s2);
ia >> obj;
return obj;
}
//receive.
template <class T>
T receive(int i, Tcp* tcp, int descriptor = 0) {
T obj;
char buffer[1024];
tcp->receiveData(buffer, sizeof(buffer), descriptor);
//cout << buffer << endl;
string str(buffer, sizeof(buffer));
//cout << str;
boost::iostreams::basic_array_source<char> device(str.c_str(), str.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char> > s2(device);
boost::archive::binary_iarchive ia(s2);
ia >> obj;
return obj;
}
//send.
template <class T>
void sendTo(T *c, Tcp* tcp, int descriptor = 0){
std::string serial_str;
boost::iostreams::back_insert_device<std::string> inserter(serial_str);
boost::iostreams::stream<boost::iostreams::back_insert_device<std::string> > s(inserter);
boost::archive::binary_oarchive oa(s);
oa << c;
s.flush();
tcp->sendData(serial_str, descriptor);
}
//send.
template <class T>
void sendTo(T c, Tcp* tcp, int descriptor = 0){
std::string serial_str;
boost::iostreams::back_insert_device<std::string> inserter(serial_str);
boost::iostreams::stream<boost::iostreams::back_insert_device<std::string> > s(inserter);
boost::archive::binary_oarchive oa(s);
oa << c;
s.flush();
tcp->sendData(serial_str, descriptor);
}
//get Location From Driver
Node* getLocationFromDriver(DetailsDriver* detailsDrivers);
//send trip.
void sendTrip(DetailsDriver* detailsDrivers, Trip* trip);
//send taxi.
void sendTaxi(DetailsDriver* detailsDrivers, Taxi* taxi);
//send move.
string sendMove(DetailsDriver* dd);
//if success.
void ifSuccess(Tcp* tcp, int socketId);
#endif //PRACTICE6_SERIALIZATION_H