-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLirc.cpp
More file actions
90 lines (76 loc) · 2.33 KB
/
Lirc.cpp
File metadata and controls
90 lines (76 loc) · 2.33 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
/**
* (C) 2016, Richard G. Roman <richard.roman@gmx.de>
* Created on 2016-01-31 18:14:35
*
*/
#include <boost/asio/buffer.hpp>
#include <boost/system/system_error.hpp>
#include <chrono>
#include "Lirc.h"
#include "Logger.h"
const char* Lirc::DefaultSocket_ = "/var/run/lirc/lircd";
const uint16_t Lirc::DefaultInetPort_ = 8765;
Lirc::Lirc(boost::asio::io_service* io_service)
: socket_(*io_service),
peer_(DefaultSocket_),
shutdownTimer_(*io_service)
{
}
void
Lirc::send(const std::string& bytes)
{
using namespace boost::asio;
if (!socket_.is_open()) {
L("Connecting to " << peer_);
try {
socket_.connect(peer_); // TODO exceptions or errors?? Test for non-existent socket file
} catch (boost::system::system_error ex) {
L("Connect error to " << peer_ << ": " << ex.what());
return;
}
startReceive();
}
L("Sending " << bytes);
socket_.send(boost::asio::buffer(bytes));
shutdownTimer_.cancel();
shutdownTimer_.expires_from_now(std::chrono::seconds(2));
shutdownTimer_.async_wait(std::bind(&Lirc::shutdownSocket, this,
std::placeholders::_1));
}
void
Lirc::startReceive()
{
socket_.async_read_some(boost::asio::buffer(receiveBuffer_),
std::bind(&Lirc::handleReceive,
this,
std::placeholders::_1 /* error_code */,
std::placeholders::_2 /* bytes_transferred */));
}
void
Lirc::handleReceive(const boost::system::error_code& error,
std::size_t bytes_transferred)
{
using namespace boost::asio;
if (error) {
L("Lirc: receive error: " << error.message());
return;
}
L("LIRC response: " << std::string(receiveBuffer_.data(), bytes_transferred));
startReceive();
}
void
Lirc::shutdownSocket(const boost::system::error_code& error)
{
if (error == boost::asio::error::operation_aborted) {
return; // happens upon cancel()
} else if (error) {
L("Lirc: shutdownSocket timer error: " << error.message());
return;
} else if (!socket_.is_open()) {
L("shutdownSocket: socket not open");
return;
}
L("Shutting down socket");
socket_.shutdown(boost::asio::local::stream_protocol::socket::shutdown_both);
socket_.close();
}