]> git.vomp.tv Git - vompclient.git/blob - vdrresponsepacket.cc
Upgrade to protocol
[vompclient.git] / vdrresponsepacket.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 "vdrresponsepacket.h"
22
23 VDR_ResponsePacket::VDR_ResponsePacket()
24 {
25   packetLength = 0;
26   packetPos = 0;
27   packet = NULL;
28   getBlockRelease = false;
29 }
30
31 VDR_ResponsePacket::~VDR_ResponsePacket()
32 {
33   if (getBlockRelease) return; // don't free if it's a getblock
34   
35   free(packet);
36 }
37
38 void VDR_ResponsePacket::set(UCHAR* tpacket, ULONG tpacketLength)
39 {
40   packet = tpacket;
41   packetLength = tpacketLength;
42 }
43
44 ULONG VDR_ResponsePacket::getLength()
45 {
46   return packetLength;
47 }
48
49 bool VDR_ResponsePacket::end()
50 {
51   return (packetPos >= packetLength);
52 }
53
54 int VDR_ResponsePacket::serverError()
55 {
56   if ((packetPos == 0) && (packetLength == 4) && !ntohl(*(ULONG*)packet)) return 1;
57   else return 0;
58 }
59
60 char* VDR_ResponsePacket::extractString()
61 {
62   if (serverError()) return NULL;
63
64   int length = strlen((char*)&packet[packetPos]);
65   if ((packetPos + length) > packetLength) return NULL;
66   char* str = new char[length + 1];
67   strcpy(str, (char*)&packet[packetPos]);
68   packetPos += length + 1;
69   return str;
70 }
71
72 UCHAR VDR_ResponsePacket::extractUCHAR()
73 {
74   if ((packetPos + sizeof(UCHAR)) > packetLength) return 0;
75   UCHAR uc = packet[packetPos];
76   packetPos += sizeof(UCHAR);
77   return uc;
78 }
79
80 ULONG VDR_ResponsePacket::extractULONG()
81 {
82   if ((packetPos + sizeof(ULONG)) > packetLength) return 0;
83   ULONG ul = ntohl(*(ULONG*)&packet[packetPos]);
84   packetPos += sizeof(ULONG);
85   return ul;
86 }
87
88 ULLONG VDR_ResponsePacket::extractULLONG()
89 {
90   if ((packetPos + sizeof(ULLONG)) > packetLength) return 0;
91   ULLONG ull = ntohll(*(ULLONG*)&packet[packetPos]);
92   packetPos += sizeof(ULLONG);
93   return ull;
94 }
95
96 long VDR_ResponsePacket::extractLONG()
97 {
98   if ((packetPos + sizeof(long)) > packetLength) return 0;
99   long l = ntohl(*(long*)&packet[packetPos]);
100   packetPos += sizeof(long);
101   return l;
102 }
103
104 UCHAR* VDR_ResponsePacket::getBlock_getPacket()
105 {
106   getBlockRelease = true;
107   return packet;
108 }
109