]> git.vomp.tv Git - vompclient.git/blob - vdrrequestpacket.cc
Sleep timer
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #ifndef WIN32
22 #include <arpa/inet.h>
23 #else
24
25 #endif
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "vdrrequestpacket.h"
30
31 #include "vdr.h"
32
33 ULONG VDR_RequestPacket::serialNumberCounter = 1;
34
35 VDR_RequestPacket::VDR_RequestPacket()
36 {
37   buffer = NULL;
38   bufSize = 0;
39   bufUsed = 0;
40   lengthSet = false;
41   serialNumber = 0;
42   
43   opcode = 0;
44 }
45
46 VDR_RequestPacket::~VDR_RequestPacket()
47 {
48   free(buffer);
49 }
50
51 bool VDR_RequestPacket::init(ULONG topcode, bool setUserDataLength, ULONG userDataLength)
52 {
53   if (buffer) return false;
54   
55   if (setUserDataLength)
56   {
57     bufSize = headerLength + userDataLength;
58     lengthSet = true;
59   }
60   else
61   {
62     bufSize = 512;
63     userDataLength = 0; // so the below will write a zero
64   }
65   
66   buffer = (UCHAR*)malloc(bufSize);
67   if (!buffer) return false;
68   
69   channel = VDR::CHANNEL_REQUEST_RESPONSE;
70   serialNumber = serialNumberCounter++;
71   opcode = topcode;
72   
73   *(ULONG*)&buffer[0] = htonl(channel);
74   *(ULONG*)&buffer[4] = htonl(serialNumber);
75   *(ULONG*)&buffer[8] = htonl(opcode);
76   *(ULONG*)&buffer[userDataLenPos] = 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[userDataLenPos] = 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[userDataLenPos] = 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[userDataLenPos] = 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[userDataLenPos] = 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