]> git.vomp.tv Git - vompclient.git/blob - vdrrequestpacket.cc
Change to thread sleep type waits in VDR
[vompclient.git] / vdrrequestpacket.cc
1 /*
2     Copyright 2007 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include <arpa/inet.h>
22
23 #include "vdrrequestpacket.h"
24
25 #include "vdr.h"
26
27 /* Packet format for an RR channel request:
28
29 4 bytes = channel ID = 1 (request/response channel)
30 4 bytes = request ID (from serialNumber)
31 4 bytes = opcode
32 4 bytes = length of the rest of the packet
33 ? bytes = rest of packet. depends on packet
34 */
35
36 ULONG VDR_RequestPacket::serialNumberCounter = 1;
37
38 VDR_RequestPacket::VDR_RequestPacket()
39 {
40   buffer = NULL;
41   bufSize = 0;
42   bufUsed = 0;
43   lengthSet = false;
44   serialNumber = 0;
45 }
46
47 VDR_RequestPacket::~VDR_RequestPacket()
48 {
49   free(buffer);
50 }
51
52 bool VDR_RequestPacket::init(ULONG opcode, bool setUserDataLength, ULONG userDataLength)
53 {
54   if (buffer) return false;
55   
56   if (setUserDataLength)
57   {
58     bufSize = headerLength + userDataLength;
59     lengthSet = true;
60   }
61   else
62   {
63     bufSize = 512;
64     userDataLength = 0; // so the below will write a zero
65   }
66   
67   buffer = (UCHAR*)malloc(bufSize);
68   if (!buffer) return false;
69   
70   channel = VDR::CHANNEL_REQUEST_RESPONSE;
71   serialNumber = serialNumberCounter++;
72   
73   *(ULONG*)&buffer[0] = htonl(channel);
74   *(ULONG*)&buffer[4] = htonl(serialNumber);
75   *(ULONG*)&buffer[8] = htonl(opcode);
76   *(ULONG*)&buffer[12] = htonl(userDataLength);
77   bufUsed = headerLength;
78
79   return true;
80 }
81
82 bool VDR_RequestPacket::copyin(const UCHAR* src, ULONG len)
83 {
84   if (!checkExtend(len)) return false;
85   memcpy(buffer + bufUsed, src, len);
86   bufUsed += len;
87   if (!lengthSet) *(ULONG*)&buffer[12] = htonl(bufUsed - headerLength);
88   return true;
89 }
90
91 bool VDR_RequestPacket::addString(const char* string)
92 {
93   ULONG len = strlen(string) + 1;
94   if (!checkExtend(len)) return false;
95   memcpy(buffer + bufUsed, string, len);
96   bufUsed += len;
97   if (!lengthSet) *(ULONG*)&buffer[12] = htonl(bufUsed - headerLength);
98   return true;
99 }
100
101 bool VDR_RequestPacket::addULONG(ULONG ul)
102 {
103   if (!checkExtend(sizeof(ULONG))) return false;
104   *(ULONG*)&buffer[bufUsed] = htonl(ul);
105   bufUsed += sizeof(ULONG);
106   if (!lengthSet) *(ULONG*)&buffer[12] = htonl(bufUsed - headerLength);
107   return true;
108 }   
109
110 bool VDR_RequestPacket::addULLONG(ULLONG ull)
111 {
112   if (!checkExtend(sizeof(ULLONG))) return false;
113   *(ULLONG*)&buffer[bufUsed] = htonll(ull);
114   bufUsed += sizeof(ULLONG);
115   if (!lengthSet) *(ULONG*)&buffer[12] = htonl(bufUsed - headerLength);
116   return true;
117 }
118
119 bool VDR_RequestPacket::checkExtend(ULONG by)
120 {
121   if (lengthSet) return true;
122   if ((bufUsed + by) < bufSize) return true;
123   UCHAR* newBuf = (UCHAR*)realloc(buffer, bufSize + 512);
124   if (!newBuf) return false;
125   buffer = newBuf;
126   bufSize += 512;
127   return true;
128 }
129