forked from TRPB/vlc-htsp-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.cpp
206 lines (171 loc) · 4.83 KB
/
helper.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*****************************************************************************
* Copyright (C) 2012
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#define __STDC_CONSTANT_MACROS 1
#include <ctime>
#include "helper.h"
#include "htsmessage.h"
#include <vlc_common.h>
#include <vlc_network.h>
sys_common_t::~sys_common_t()
{
if(netfd >= 0)
net_Close(netfd);
}
uint32_t HTSPNextSeqNum(sys_common_t *sys)
{
uint32_t res = sys->nextSeqNum++;
if(sys->nextSeqNum > 2147483647)
sys->nextSeqNum = 1;
return res;
}
bool TransmitMessageEx(vlc_object_t *obj, sys_common_t *sys, HtsMessage m)
{
if(sys->netfd < 0)
{
msg_Dbg(obj, "Invalid netfd in TransmitMessage");
return false;
}
void *buf;
uint32_t len;
if(!m.Serialize(&len, &buf))
{
msg_Dbg(obj, "Serialising message failed");
return false;
}
if(net_Write(obj, sys->netfd, buf, len) != (ssize_t)len)
{
msg_Dbg(obj, "net_Write failed");
return false;
}
free(buf);
return true;
}
HtsMessage ReadMessageEx(vlc_object_t *obj, sys_common_t *sys)
{
char *buf;
uint32_t len;
ssize_t readSize;
if(sys->queue.size())
{
HtsMessage res = sys->queue.front();
sys->queue.pop_front();
return res;
}
if(sys->netfd < 0)
{
msg_Dbg(obj, "ReadMessage on closed netfd");
return HtsMessage();
}
if((readSize = net_Read(obj, sys->netfd, &len, sizeof(len))) != sizeof(len))
{
net_Close(sys->netfd);
sys->netfd = -1;
if(readSize == 0)
{
msg_Err(obj, "Size Read EOF!");
return HtsMessage();
}
else if(readSize < 0)
{
msg_Err(obj, "Data Read ERROR!");
return HtsMessage();
}
msg_Err(obj, "Error reading size: %m");
return HtsMessage();
}
len = ntohl(len);
if(len == 0)
return HtsMessage();
buf = (char*)malloc(len);
if((readSize = net_Read(obj, sys->netfd, buf, len)) != (ssize_t)len)
{
net_Close(sys->netfd);
sys->netfd = -1;
if(readSize == 0)
{
msg_Err(obj, "Data Read EOF!");
return HtsMessage();
}
else if(readSize < 0)
{
msg_Err(obj, "Data Read ERROR!");
return HtsMessage();
}
msg_Err(obj, "Error reading data: %m");
return HtsMessage();
}
HtsMessage result = HtsMessage::Deserialize(len, buf);
free(buf);
return result;
}
HtsMessage ReadResultEx(vlc_object_t *obj, sys_common_t *sys, HtsMessage m, bool sequence)
{
uint32_t iSequence = 0;
if(sequence)
{
iSequence = HTSPNextSeqNum(sys);
m.getRoot()->setData("seq", iSequence);
}
if(!TransmitMessageEx(obj, sys, m))
{
msg_Err(obj, "TransmitMessage failed!");
return HtsMessage();
}
std::deque<HtsMessage> queue;
sys->queue.swap(queue);
while((m = ReadMessageEx(obj, sys)).isValid())
{
if(!sequence)
break;
if(m.getRoot()->contains("seq") && m.getRoot()->getU32("seq") == iSequence)
break;
queue.push_back(m);
if(queue.size() >= MAX_QUEUE_SIZE)
{
msg_Err(obj, "Max queue size reached!");
sys->queue.swap(queue);
return HtsMessage();
}
}
sys->queue.swap(queue);
if(!m.isValid())
{
msg_Err(obj, "ReadMessage failed!");
return HtsMessage();
}
if(m.getRoot()->contains("error"))
{
msg_Err(obj, "HTSP Error: %s", m.getRoot()->getStr("error").c_str());
return HtsMessage();
}
if(m.getRoot()->getU32("noaccess") != 0)
{
msg_Err(obj, "Access Denied");
return HtsMessage();
}
return m;
}
bool ReadSuccessEx(vlc_object_t *obj, sys_common_t *sys, HtsMessage m, const std::string &action, bool sequence)
{
if(!ReadResultEx(obj, sys, m, sequence).isValid())
{
msg_Err(obj, "ReadSuccess - failed to %s", action.c_str());
return false;
}
return true;
}