forked from FernetMenta/vdr-plugin-vnsiserver
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvnsisocket.c
155 lines (133 loc) · 2.95 KB
/
vnsisocket.c
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
/*
* vnsisocket.c
*
* Created on: 26.12.2020
* Author: mdrechsler
*/
#include "config.h"
#include "vnsicommand.h"
#include "vnsisocket.h"
#include "requestpacket.h"
#include "ICommandQueue.h"
#include "StatusCommands.h"
#include <memory>
#include <arpa/inet.h>
VNSISocket::VNSISocket(int fd,
ICommandQueue& queue)
: m_socket(fd)
, m_queue( queue )
{
SetDescription("VNSI Socket %d", fd);
Start();
}
VNSISocket::~VNSISocket()
{
m_socket.Shutdown();
Cancel(10);
}
void
VNSISocket::Shutdown()
{
m_socket.Shutdown();
}
void
VNSISocket::lock()
{
m_socket.lock();
}
void
VNSISocket::unlock()
{
m_socket.unlock();
}
void
VNSISocket::Invalidate()
{
Cancel( -1 );
m_socket.Invalidate();
}
int
VNSISocket::GetHandle() const
{
return m_socket.GetHandle();
}
ssize_t
VNSISocket::read( void* buffer,
size_t size,
int timeout_ms )
{
return m_socket.read( buffer, size, timeout_ms );
}
ssize_t
VNSISocket::write( const void* buffer,
size_t size,
int timeout_ms,
bool more_data )
{
return m_socket.write(buffer, size, timeout_ms, more_data);
}
void
VNSISocket::Action()
{
struct Header
{
uint32_t channelID;
uint32_t requestID;
uint32_t opcode;
uint32_t dataLength;
};
try
{
while (Running())
{
// Read the header. It is fixed site and contains the remaining
// bytes to read.
Header header;
if ( sizeof(header) != m_socket.read(&header, sizeof(header)) )
{
throw std::runtime_error( "Failed to read header" );
}
// Header values are given in network byte order
header.channelID = ntohl(header.channelID);
header.requestID = ntohl(header.requestID);
header.opcode = ntohl(header.opcode);
header.dataLength = ntohl(header.dataLength);
// This was meant to multiplex multiple streams over a single connection.
// For now ignore every channel not equal 1
if (header.channelID != 1)
{
throw std::runtime_error("Incoming channel number unknown");
}
// a random sanity limit
if (header.dataLength > 200000)
{
throw std::runtime_error("dataLength > 200000!");
}
std::unique_ptr<uint8_t[]> data;
if (header.dataLength)
{
data.reset( new uint8_t[header.dataLength] );
if (header.dataLength != m_socket.read(data.get(), header.dataLength, 10000))
{
throw std::runtime_error("Could not read data");
}
}
DEBUGLOG("Received chan=%u, ser=%u, op=%u, edl=%u", header.channelID,
header.requestID, header.opcode, header.dataLength);
auto command = std::make_shared<cRequestPacket>(
header.requestID, header.opcode, data.release(), header.dataLength
);
m_queue.enqueue( command );
}
}
catch ( const std::exception& error )
{
ERRORLOG( "Socket error: '%s'. Dropping connection", error.what() );
m_queue.enqueue( std::make_shared<SocketError>() );
}
catch ( ... )
{
ERRORLOG( "Socket error: 'Undetermined exception'. Dropping connection" );
m_queue.enqueue( std::make_shared<SocketError>() );
}
}