]> git.vomp.tv Git - vompclient.git/blob - vdrresponsepacket.cc
Updates to new streaming protocol and live tv
[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 #include "vdr.h"
24 #include "tcp.h"
25
26 VDR_ResponsePacket::VDR_ResponsePacket()
27 {
28   userDataLength = 0;
29   packetPos = 0;
30   userData = NULL;
31   ownBlock = true;
32   
33   channelID = 0;
34   
35   requestID = 0;
36   streamID = 0;
37 }
38
39 VDR_ResponsePacket::~VDR_ResponsePacket()
40 {
41   if (!ownBlock) return; // don't free if it's a getblock
42   
43   if (userData) free(userData);
44 }
45
46 void VDR_ResponsePacket::setResponse(ULONG trequestID, UCHAR* tuserData, ULONG tuserDataLength)
47 {
48   channelID = VDR::CHANNEL_REQUEST_RESPONSE;
49   requestID = trequestID;
50   userData = tuserData;
51   userDataLength = tuserDataLength;
52 }
53
54 void VDR_ResponsePacket::setStream(ULONG tstreamID, UCHAR* tuserData, ULONG tuserDataLength)
55 {
56   channelID = VDR::CHANNEL_STREAM;
57   streamID = tstreamID;
58   userData = tuserData;
59   userDataLength = tuserDataLength;
60 }
61
62 bool VDR_ResponsePacket::end()
63 {
64   return (packetPos >= userDataLength);
65 }
66
67 void VDR_ResponsePacket::dumpUD()
68 {
69   TCP::dump(userData, userDataLength);
70 }
71
72 int VDR_ResponsePacket::serverError()
73 {
74   if ((packetPos == 0) && (userDataLength == 4) && !ntohl(*(ULONG*)userData)) return 1;
75   else return 0;
76 }
77
78 char* VDR_ResponsePacket::extractString()
79 {
80   if (serverError()) return NULL;
81
82   int length = strlen((char*)&userData[packetPos]);
83   if ((packetPos + length) > userDataLength) return NULL;
84   char* str = new char[length + 1];
85   strcpy(str, (char*)&userData[packetPos]);
86   packetPos += length + 1;
87   return str;
88 }
89
90 UCHAR VDR_ResponsePacket::extractUCHAR()
91 {
92   if ((packetPos + sizeof(UCHAR)) > userDataLength) return 0;
93   UCHAR uc = userData[packetPos];
94   packetPos += sizeof(UCHAR);
95   return uc;
96 }
97
98 ULONG VDR_ResponsePacket::extractULONG()
99 {
100   if ((packetPos + sizeof(ULONG)) > userDataLength) return 0;
101   ULONG ul = ntohl(*(ULONG*)&userData[packetPos]);
102   packetPos += sizeof(ULONG);
103   return ul;
104 }
105
106 ULLONG VDR_ResponsePacket::extractULLONG()
107 {
108   if ((packetPos + sizeof(ULLONG)) > userDataLength) return 0;
109   ULLONG ull = ntohll(*(ULLONG*)&userData[packetPos]);
110   packetPos += sizeof(ULLONG);
111   return ull;
112 }
113
114 long VDR_ResponsePacket::extractLONG()
115 {
116   if ((packetPos + sizeof(long)) > userDataLength) return 0;
117   long l = ntohl(*(long*)&userData[packetPos]);
118   packetPos += sizeof(long);
119   return l;
120 }
121
122 UCHAR* VDR_ResponsePacket::getUserData()
123 {
124   ownBlock = false;
125   return userData;
126 }
127