]> git.vomp.tv Git - vompclient.git/blob - vdrresponsepacket.cc
Rename TCP class to TCPOld
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "vdrresponsepacket.h"
22
23 #include "vdr.h"
24
25 VDR_ResponsePacket::VDR_ResponsePacket()
26 {
27   userDataLength = 0;
28   packetPos = 0;
29   userData = NULL;
30   ownBlock = true;
31   
32   channelID = 0;
33   
34   requestID = 0;
35   streamID = 0;
36   
37   flag = 0;
38 }
39
40 VDR_ResponsePacket::~VDR_ResponsePacket()
41 {
42   if (!ownBlock) return; // don't free if it's a getblock
43   
44   if (userData) free(userData);
45 }
46
47 void VDR_ResponsePacket::setResponse(ULONG trequestID, UCHAR* tuserData, ULONG tuserDataLength)
48 {
49   channelID = VDR::CHANNEL_REQUEST_RESPONSE;
50   requestID = trequestID;
51   userData = tuserData;
52   userDataLength = tuserDataLength;
53 }
54
55 void VDR_ResponsePacket::setStream(ULONG tstreamID, ULONG tflag, UCHAR* tuserData, ULONG tuserDataLength, ULONG tchannelID)
56 {
57   channelID = tchannelID;
58   streamID = tstreamID;
59   flag = tflag;
60   userData = tuserData;
61   userDataLength = tuserDataLength;
62 }
63
64 bool VDR_ResponsePacket::end()
65 {
66   return (packetPos >= userDataLength);
67 }
68
69 void VDR_ResponsePacket::dumpUD()
70 {
71   // FIXME TODO - use generic stdout hex printer for userData, userDataLength
72 }
73
74 int VDR_ResponsePacket::serverError()
75 {
76   if ((packetPos == 0) && (userDataLength == 4) && !ntohl(*(ULONG*)userData)) return 1;
77   else return 0;
78 }
79
80 std::string  VDR_ResponsePacket::extractStdString()
81 {
82   if (serverError()) return NULL;
83
84   int length = strlen((char*)&userData[packetPos]);
85   if ((packetPos + length) > userDataLength) return std::string("");
86   const char * dest_str=(char*)&userData[packetPos];
87   packetPos += length + 1;
88   return dest_str;
89 }
90
91 char* VDR_ResponsePacket::extractString()
92 {
93   if (serverError()) return NULL;
94
95   int length = strlen((char*)&userData[packetPos]);
96   if ((packetPos + length) > userDataLength) return NULL;
97   char* str = new char[length + 1];
98   strcpy(str, (char*)&userData[packetPos]);
99   packetPos += length + 1;
100   return str;
101 }
102
103 UCHAR VDR_ResponsePacket::extractUCHAR()
104 {
105   if ((packetPos + sizeof(UCHAR)) > userDataLength) return 0;
106   UCHAR uc = userData[packetPos];
107   packetPos += sizeof(UCHAR);
108   return uc;
109 }
110
111 ULONG VDR_ResponsePacket::extractULONG()
112 {
113   if ((packetPos + sizeof(ULONG)) > userDataLength) return 0;
114   ULONG ul = userData[packetPos++]<<24;
115   ul|= userData[packetPos++]<<16;
116   ul|= userData[packetPos++]<<8;
117   ul|= userData[packetPos++];
118   //packetPos += sizeof(ULONG);
119   return ul;
120 }
121
122 ULLONG VDR_ResponsePacket::extractULLONG()
123 {
124   if ((packetPos + sizeof(ULLONG)) > userDataLength) return 0;
125   ULLONG ull= ((ULLONG)userData[packetPos++])<<56;
126   ull|= ((ULLONG)userData[packetPos++])<<48;
127   ull|= ((ULLONG)userData[packetPos++])<<40;
128   ull|= ((ULLONG)userData[packetPos++])<<32;
129   ull|= ((ULLONG)userData[packetPos++])<<24;
130   ull|= ((ULLONG)userData[packetPos++])<<16;
131   ull|= ((ULLONG)userData[packetPos++])<<8;
132   ull|= ((ULLONG)userData[packetPos++]);
133   return ull;
134 }
135
136 double VDR_ResponsePacket::extractdouble()
137 {
138   if ((packetPos + sizeof(ULLONG)) > userDataLength) return 0;
139   ULLONG ull = extractULLONG();
140   double d;
141   memcpy(&d,&ull,sizeof(double));
142   return d;
143 }
144
145 long VDR_ResponsePacket::extractLONG()
146 {
147   if ((packetPos + sizeof(long)) > userDataLength) return 0;
148   long l = userData[packetPos++]<<24;
149   l|= userData[packetPos++]<<16;
150   l|= userData[packetPos++]<<8;
151   l|= userData[packetPos++];
152   return l;
153 }
154
155 UCHAR* VDR_ResponsePacket::getUserData()
156 {
157   ownBlock = false;
158   return userData;
159 }
160