forked from javorcd/cppplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.h
More file actions
168 lines (142 loc) · 3.45 KB
/
protocol.h
File metadata and controls
168 lines (142 loc) · 3.45 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once
#include "commands.h"
#include "config.h"
#include <cstring>
#include <string>
#include <ostream>
#include <istream>
#ifdef _NAMED_PIPE
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#elif _TCP_SOCKET
#include <boost/asio.hpp>
#else
#error At least we need one protocol to use
#endif
#ifdef _NAMED_PIPE
//This should only be used for debugging/scripting pourpose
// or if only one client are in use.
class NamedPipe {
public:
NamedPipe(Config& c)
: conf(c)
{
//Delete pipes, if exist (the program exit abnormaly)
if(unlink(conf.GetDaemonPipe().c_str()) == -1)
throw std::runtime_error(strerror(errno));
if(unlink(conf.GetClientPipe().c_str()) == -1)
throw std::runtime_error(strerror(errno));
if(mkfifo(conf.GetDaemonPipe().c_str(), 0666) == -1)
throw std::runtime_error(strerror(errno));
if(mkfifo(conf.GetClientPipe().c_str(), 0666) == -1)
throw std::runtime_error(strerror(errno));
}
~NamedPipe() {
//Delete pipes
if(unlink(conf.GetDaemonPipe().c_str()) == -1)
throw std::runtime_error(strerror(errno));
if(unlink(conf.GetClientPipe().c_str()) == -1)
throw std::runtime_error(strerror(errno));
}
Command ReadCommand() {
CheckDaemon();
return static_cast<Command>(fdaemon.get());
}
std::string GetLine() {
CheckDaemon();
std::string s;
getline(fdaemon, s);
return s;
}
template <typename T>
std::ostream& operator<<(const T& obj) {
CheckClient();
fclient << obj << std::endl;
return fclient;
}
template <typename T>
std::istream& operator>>(T& obj) {
CheckDaemon();
fdaemon >> obj;
return fdaemon;
}
private:
void CheckClient() {
fclient.close();
fclient.open(conf.GetClientPipe());
if(!fclient.is_open()) {
throw std::runtime_error("Client pipe could not be opened");
}
}
void CheckDaemon() {
//XXX: Workaround
if(fdaemon.peek() == -1) fdaemon.ignore();
if(!fdaemon.good() || !fdaemon.is_open()) {
fdaemon.close();
fdaemon.open(conf.GetDaemonPipe());
if(!fdaemon.is_open()) {
throw std::runtime_error("Daemon pipe could not be opened");
}
}
}
Config& conf;
std::ofstream fclient;
std::ifstream fdaemon;
};
#elif _TCP_SOCKET
//TODO: Accept multiple connections simultaneously
using boost::asio::ip::tcp;
using namespace boost::asio;
class Tcp {
public:
Tcp(Config& c)
: conf(c)
{
auto ip = ip::address_v4().from_string(conf.GetBindAddress());
acceptor = new tcp::acceptor(io_service, tcp::endpoint(ip, 6600));
}
~Tcp() {
delete acceptor;
}
Command ReadCommand() {
delete socket;
socket = new tcp::socket(io_service);
acceptor->accept(*socket);
char buffer[1];
read(*socket, boost::asio::buffer(buffer, 1));
return static_cast<Command>(buffer[0]);
}
std::string GetLine() {
auto bytes = read_until(*socket, buffer, '\n');
buffer.commit(bytes);
std::string line;
std::getline(is, line);
return line;
}
template <typename T>
std::ostream& operator<<(const T& obj) {
os << obj << std::endl;
auto bytes = write(*socket, buffer);
buffer.consume(bytes);
return os;
}
template <typename T>
std::istream& operator>>(T& obj) {
auto bytes = boost::asio::read_until(*socket, buffer, '\n');
buffer.commit(bytes);
is >> obj;
return is;
}
private:
Config& conf;
boost::asio::io_service io_service;
tcp::acceptor* acceptor;
tcp::socket* socket;
streambuf buffer;
std::istream is{&buffer};
std::ostream os{&buffer};
};
#else
#error At least we need one protocol to use
#endif