Permalink
Browse files

vnsi: make network interface portable

  • Loading branch information...
1 parent db8b2f6 commit b8760d0a26381e77a28b1687ed34b3b86c573283 @FernetMenta FernetMenta committed Sep 28, 2012
@@ -158,14 +158,8 @@ bool cVNSISession::Login()
cResponsePacket* cVNSISession::ReadMessage(int iInitialTimeout /*= 10000*/, int iDatapacketTimeout /*= 10000*/)
{
uint32_t channelID = 0;
- uint32_t requestID;
uint32_t userDataLength = 0;
uint8_t* userData = NULL;
- uint32_t streamID;
- uint32_t duration;
- uint32_t opCodeID;
- int64_t dts = 0;
- int64_t pts = 0;
cResponsePacket* vresp = NULL;
@@ -179,16 +173,17 @@ cResponsePacket* cVNSISession::ReadMessage(int iInitialTimeout /*= 10000*/, int
channelID = ntohl(channelID);
if (channelID == VNSI_CHANNEL_STREAM)
{
- if (!readData((uint8_t*)&m_streamPacketHeader, sizeof(m_streamPacketHeader), iDatapacketTimeout)) return NULL;
+ vresp = new cResponsePacket();
- opCodeID = ntohl(m_streamPacketHeader.opCodeID);
- streamID = ntohl(m_streamPacketHeader.streamID);
- duration = ntohl(m_streamPacketHeader.duration);
- pts = ntohll(*(int64_t*)m_streamPacketHeader.pts);
- dts = ntohll(*(int64_t*)m_streamPacketHeader.dts);
- userDataLength = ntohl(m_streamPacketHeader.userDataLength);
+ if (!readData(vresp->getHeader(), vresp->getStreamHeaderLength(), iDatapacketTimeout))
+ {
+ delete vresp;
+ return NULL;
+ }
+ vresp->extractStreamHeader();
+ userDataLength = vresp->getUserDataLength();
- if(opCodeID == VNSI_STREAM_MUXPKT)
+ if(vresp->getOpCodeID() == VNSI_STREAM_MUXPKT)
{
DemuxPacket* p = PVR->AllocateDemuxPacket(userDataLength);
userData = (uint8_t*)p;
@@ -198,6 +193,7 @@ cResponsePacket* cVNSISession::ReadMessage(int iInitialTimeout /*= 10000*/, int
if (!readData(p->pData, userDataLength, iDatapacketTimeout))
{
PVR->FreeDemuxPacket(p);
+ delete vresp;
return NULL;
}
}
@@ -209,19 +205,23 @@ cResponsePacket* cVNSISession::ReadMessage(int iInitialTimeout /*= 10000*/, int
if (!readData(userData, userDataLength, iDatapacketTimeout))
{
free(userData);
+ delete vresp;
return NULL;
}
}
-
- vresp = new cResponsePacket();
- vresp->setStream(opCodeID, streamID, duration, dts, pts, userData, userDataLength);
+ vresp->setStream(userData, userDataLength);
}
else
{
- if (!readData((uint8_t*)&m_responsePacketHeader, sizeof(m_responsePacketHeader), iDatapacketTimeout)) return NULL;
+ vresp = new cResponsePacket();
- requestID = ntohl(m_responsePacketHeader.requestID);
- userDataLength = ntohl(m_responsePacketHeader.userDataLength);
+ if (!readData(vresp->getHeader(), vresp->getHeaderLength(), iDatapacketTimeout))
+ {
+ delete vresp;
+ return NULL;
+ }
+ vresp->extractHeader();
+ userDataLength = vresp->getUserDataLength();
if (userDataLength > 5000000) return NULL; // how big can these packets get?
userData = NULL;
@@ -232,15 +232,15 @@ cResponsePacket* cVNSISession::ReadMessage(int iInitialTimeout /*= 10000*/, int
if (!readData(userData, userDataLength, iDatapacketTimeout))
{
free(userData);
+ delete vresp;
return NULL;
}
}
- vresp = new cResponsePacket();
if (channelID == VNSI_CHANNEL_STATUS)
- vresp->setStatus(requestID, userData, userDataLength);
+ vresp->setStatus(userData, userDataLength);
else
- vresp->setResponse(requestID, userData, userDataLength);
+ vresp->setResponse(userData, userDataLength);
}
return vresp;
@@ -81,19 +81,4 @@ class cVNSISession
std::string m_server;
std::string m_version;
bool m_connectionLost;
-
- struct {
- uint32_t opCodeID;
- uint32_t streamID;
- uint32_t duration;
- uint8_t pts[sizeof(int64_t)];
- uint8_t dts[sizeof(int64_t)];
- uint32_t userDataLength;
- } m_streamPacketHeader;
-
- struct {
- uint32_t requestID;
- uint32_t userDataLength;
- } m_responsePacketHeader;
-
};
@@ -70,10 +70,15 @@ bool cRequestPacket::init(uint32_t topcode, bool stream, bool setUserDataLength,
serialNumber = serialNumberCounter++;
opcode = topcode;
- *(uint32_t*)&buffer[0] = htonl(channel);
- *(uint32_t*)&buffer[4] = htonl(serialNumber);
- *(uint32_t*)&buffer[8] = htonl(opcode);
- *(uint32_t*)&buffer[userDataLenPos] = htonl(userDataLength);
+ uint32_t ul;
+ ul = htonl(channel);
+ memcpy(&buffer[0], &ul, sizeof(uint32_t));
+ ul = htonl(serialNumber);
+ memcpy(&buffer[4], &ul, sizeof(uint32_t));
+ ul = htonl(opcode);
+ memcpy(&buffer[8], &ul, sizeof(uint32_t));
+ ul = htonl(userDataLength);
+ memcpy(&buffer[userDataLenPos], &ul, sizeof(uint32_t));
bufUsed = headerLength;
return true;
@@ -85,7 +90,11 @@ bool cRequestPacket::add_String(const char* string)
if (!checkExtend(len)) return false;
memcpy(buffer + bufUsed, string, len);
bufUsed += len;
- if (!lengthSet) *(uint32_t*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
+ if (!lengthSet)
+ {
+ uint32_t tmp = htonl(bufUsed - headerLength);
+ memcpy(&buffer[userDataLenPos], &tmp, sizeof(uint32_t));
+ }
return true;
}
@@ -94,34 +103,53 @@ bool cRequestPacket::add_U8(uint8_t c)
if (!checkExtend(sizeof(uint8_t))) return false;
buffer[bufUsed] = c;
bufUsed += sizeof(uint8_t);
- if (!lengthSet) *(uint32_t*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
+ if (!lengthSet)
+ {
+ uint32_t tmp = htonl(bufUsed - headerLength);
+ memcpy(&buffer[userDataLenPos], &tmp, sizeof(uint32_t));
+ }
return true;
}
bool cRequestPacket::add_S32(int32_t l)
{
if (!checkExtend(sizeof(int32_t))) return false;
- *(int32_t*)&buffer[bufUsed] = htonl(l);
+ int32_t tmp = htonl(l);
+ memcpy(&buffer[bufUsed], &tmp, sizeof(int32_t));
bufUsed += sizeof(int32_t);
- if (!lengthSet) *(uint32_t*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
+ if (!lengthSet)
+ {
+ uint32_t tmp = htonl(bufUsed - headerLength);
+ memcpy(&buffer[userDataLenPos], &tmp, sizeof(uint32_t));
+ }
return true;
}
bool cRequestPacket::add_U32(uint32_t ul)
{
if (!checkExtend(sizeof(uint32_t))) return false;
- *(uint32_t*)&buffer[bufUsed] = htonl(ul);
+ uint32_t tmp = htonl(ul);
+ memcpy(&buffer[bufUsed], &tmp, sizeof(uint32_t));
bufUsed += sizeof(uint32_t);
- if (!lengthSet) *(uint32_t*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
+ if (!lengthSet)
+ {
+ uint32_t tmp = htonl(bufUsed - headerLength);
+ memcpy(&buffer[userDataLenPos], &tmp, sizeof(uint32_t));
+ }
return true;
}
bool cRequestPacket::add_U64(uint64_t ull)
{
if (!checkExtend(sizeof(uint64_t))) return false;
- *(uint64_t*)&buffer[bufUsed] = htonll(ull);
+ uint64_t tmp = htonll(ull);
+ memcpy(&buffer[bufUsed], &tmp, sizeof(uint64_t));
bufUsed += sizeof(uint64_t);
- if (!lengthSet) *(uint32_t*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
+ if (!lengthSet)
+ {
+ uint32_t tmp = htonl(bufUsed - headerLength);
+ memcpy(&buffer[userDataLenPos], &tmp, sizeof(uint32_t));
+ }
return true;
}
@@ -53,32 +53,57 @@ cResponsePacket::~cResponsePacket()
}
}
-void cResponsePacket::setResponse(uint32_t trequestID, uint8_t* tuserData, uint32_t tuserDataLength)
+void cResponsePacket::setResponse(uint8_t* tuserData, uint32_t tuserDataLength)
{
channelID = VNSI_CHANNEL_REQUEST_RESPONSE;
- requestID = trequestID;
userData = tuserData;
userDataLength = tuserDataLength;
+ packetPos = 0;
}
-void cResponsePacket::setStatus(uint32_t trequestID, uint8_t* tuserData, uint32_t tuserDataLength)
+void cResponsePacket::setStatus(uint8_t* tuserData, uint32_t tuserDataLength)
{
channelID = VNSI_CHANNEL_STATUS;
- requestID = trequestID;
userData = tuserData;
userDataLength = tuserDataLength;
+ packetPos = 0;
}
-void cResponsePacket::setStream(uint32_t topcodeID, uint32_t tstreamID, uint32_t tduration, int64_t tdts, int64_t tpts, uint8_t* tuserData, uint32_t tuserDataLength)
+void cResponsePacket::setStream(uint8_t* tuserData, uint32_t tuserDataLength)
{
channelID = VNSI_CHANNEL_STREAM;
- opcodeID = topcodeID;
- streamID = tstreamID;
- duration = tduration;
- dts = tdts;
- pts = tpts;
+ // set pointer to user data
userData = tuserData;
userDataLength = tuserDataLength;
+ packetPos = 0;
+}
+
+void cResponsePacket::extractHeader()
+{
+ // set data pointers to header first
+ userData = header;
+ userDataLength = sizeof(header);
+ packetPos = 0;
+
+ requestID = extract_U32();
+ userDataLength = extract_U32();
+}
+
+void cResponsePacket::extractStreamHeader()
+{
+ channelID = VNSI_CHANNEL_STREAM;
+
+ // set data pointers to header first
+ userData = header;
+ userDataLength = sizeof(header);
+ packetPos = 0;
+
+ opcodeID = extract_U32();
+ streamID = extract_U32();
+ duration = extract_U32();
+ pts = extract_U64();
+ dts = extract_U64();
+ userDataLength = extract_U32();
}
bool cResponsePacket::end()
@@ -115,23 +140,29 @@ uint8_t cResponsePacket::extract_U8()
uint32_t cResponsePacket::extract_U32()
{
if ((packetPos + sizeof(uint32_t)) > userDataLength) return 0;
- uint32_t ul = ntohl(*(uint32_t*)&userData[packetPos]);
+ uint32_t ul;
+ memcpy(&ul, &userData[packetPos], sizeof(uint32_t));
+ ul = ntohl(ul);
packetPos += sizeof(uint32_t);
return ul;
}
uint64_t cResponsePacket::extract_U64()
{
if ((packetPos + sizeof(uint64_t)) > userDataLength) return 0;
- uint64_t ull = ntohll(*(uint64_t*)&userData[packetPos]);
+ uint64_t ull;
+ memcpy(&ull, &userData[packetPos], sizeof(uint64_t));
+ ull = ntohll(ull);
packetPos += sizeof(uint64_t);
return ull;
}
double cResponsePacket::extract_Double()
{
if ((packetPos + sizeof(uint64_t)) > userDataLength) return 0;
- uint64_t ull = ntohll(*(uint64_t*)&userData[packetPos]);
+ uint64_t ull;
+ memcpy(&ull, &userData[packetPos], sizeof(uint64_t));
+ ull = ntohll(ull);
double d;
memcpy(&d,&ull,sizeof(double));
packetPos += sizeof(uint64_t);
@@ -141,15 +172,19 @@ double cResponsePacket::extract_Double()
int32_t cResponsePacket::extract_S32()
{
if ((packetPos + sizeof(int32_t)) > userDataLength) return 0;
- int32_t l = ntohl(*(int32_t*)&userData[packetPos]);
+ int32_t l;
+ memcpy(&l, &userData[packetPos], sizeof(int32_t));
+ l = ntohl(l);
packetPos += sizeof(int32_t);
return l;
}
int64_t cResponsePacket::extract_S64()
{
if ((packetPos + sizeof(int64_t)) > userDataLength) return 0;
- int64_t ll = ntohll(*(int64_t*)&userData[packetPos]);
+ int64_t ll;
+ memcpy(&ll, &userData[packetPos], sizeof(int64_t));
+ ll = ntohll(ll);
packetPos += sizeof(int64_t);
return ll;
}
@@ -29,10 +29,12 @@ class cResponsePacket
cResponsePacket();
~cResponsePacket();
- void setResponse(uint32_t requestID, uint8_t* packet, uint32_t packetLength);
- void setStatus(uint32_t requestID, uint8_t* packet, uint32_t packetLength);
+ void setResponse(uint8_t* packet, uint32_t packetLength);
+ void setStatus(uint8_t* packet, uint32_t packetLength);
+ void setStream(uint8_t* packet, uint32_t packetLength);
- void setStream(uint32_t opcodeID, uint32_t streamID, uint32_t duration, int64_t dts, int64_t pts, uint8_t* packet, uint32_t packetLength);
+ void extractHeader();
+ void extractStreamHeader();
bool noResponse() { return (userData == NULL); };
int serverError();
@@ -61,7 +63,12 @@ class cResponsePacket
// If you call this, the memory becomes yours. Free with free()
uint8_t* getUserData();
+ uint8_t* getHeader() { return header; };
+ unsigned int getStreamHeaderLength() { return 32; };
+ unsigned int getHeaderLength() { return 8; };
+
private:
+ uint8_t header[36];
uint8_t* userData;
uint32_t userDataLength;
uint32_t packetPos;

0 comments on commit b8760d0

Please sign in to comment.