-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscapi_socket_session.cpp
54 lines (46 loc) · 1.56 KB
/
scapi_socket_session.cpp
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
#include "scapi_socket_session.hpp"
#include "scapi_messages_asn1c.hpp"
#include "utils.hpp"
#include <libsocket/exception.hpp>
#include <vector>
#include <stdexcept>
#include <iostream>
using namespace ::std;
using namespace ::scapi::socket;
inet_stream::inet_stream(const char* const host, const char* port)
: ::libsocket::inet_stream(host, port, LIBSOCKET_BOTH) {
}
inet_stream::~inet_stream(void) noexcept {
try {
shutdown(LIBSOCKET_READ | LIBSOCKET_WRITE);
} catch (const ::libsocket::socket_exception& e) {
cerr << __FILE__ << ':' << __LINE__ << '@' << __func__ << " Internal exception suppressed (errno: " << e.err << ")\n" << e.mesg << endl;
}
destroy();
}
vector<unsigned char>
Session::exch(const vector<unsigned char> req) {
const int sf = MSG_NOSIGNAL;
if (stream.snd(req.data(), req.size(), sf) != integer_cast<ssize_t>(req.size())) {
throw runtime_error("Couldn't send full message");
}
vector<unsigned char> buf(2 * 1024);
const ssize_t received = stream.rcv(buf.data(), buf.capacity(), 0);
if (received < 0) {
throw runtime_error("No message");
}
buf.resize(static_cast<size_t>(received));
return buf;
}
Session::Session(void)
: stream("localhost", "50153") {
decode(exch(encode(RegistrationRequest{})));
}
::scapi::Response
Session::interaction(const ::scapi::Request& r, std::chrono::milliseconds) {
return get<0>(decode(exch(encode(Request(r)))));
}
::scapi::Notification
Session::notification(void) {
return get<1>(decode(exch(encode(NotificationRequest{}))));
}