From eebd99099139f273a5c6ae0b02df4fcd295115df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Sepp=C3=A4l=C3=A4?= Date: Sun, 21 Oct 2007 17:32:43 +0000 Subject: [PATCH] Improve object constructs of protocols. Create Separate ProtocolUdp and ProtocolTcp -classes. --- protocolext.c | 12 +++++------ protocolext.h | 6 +++--- protocolhttp.c | 33 ++++++------------------------ protocolhttp.h | 6 +++--- protocoludp.c | 12 +++++------ protocoludp.h | 6 +++--- socket.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++--- socket.h | 17 ++++++++++++++-- 8 files changed, 93 insertions(+), 53 deletions(-) diff --git a/protocolext.c b/protocolext.c index 32b75aa..112b918 100644 --- a/protocolext.c +++ b/protocolext.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolext.c,v 1.18 2007/10/21 13:31:21 ajhseppa Exp $ + * $Id: protocolext.c,v 1.19 2007/10/21 17:32:43 ajhseppa Exp $ */ #include @@ -110,11 +110,6 @@ void cIptvProtocolExt::TerminateScript(void) } } -int cIptvProtocolExt::Read(unsigned char* *BufferAddr) -{ - return ReadUdpSocket(BufferAddr); -} - bool cIptvProtocolExt::Open(void) { debug("cIptvProtocolExt::Open()\n"); @@ -140,6 +135,11 @@ bool cIptvProtocolExt::Close(void) return true; } +int cIptvProtocolExt::Read(unsigned char* *BufferAddr) +{ + return cIptvUdpSocket::Read(BufferAddr); +} + bool cIptvProtocolExt::Set(const char* Location, const int Parameter, const int Index) { debug("cIptvProtocolExt::Set(): Location=%s Parameter=%d Index=%d\n", Location, Parameter, Index); diff --git a/protocolext.h b/protocolext.h index b795b9e..8bf0a10 100644 --- a/protocolext.h +++ b/protocolext.h @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolext.h,v 1.7 2007/10/21 13:31:21 ajhseppa Exp $ + * $Id: protocolext.h,v 1.8 2007/10/21 17:32:43 ajhseppa Exp $ */ #ifndef __IPTV_PROTOCOLEXT_H @@ -13,7 +13,7 @@ #include "protocolif.h" #include "socket.h" -class cIptvProtocolExt : public cIptvSocket, public cIptvProtocolIf { +class cIptvProtocolExt : public cIptvUdpSocket, public cIptvProtocolIf { private: int pid; char* listenAddr; @@ -27,7 +27,7 @@ private: public: cIptvProtocolExt(); virtual ~cIptvProtocolExt(); - virtual int Read(unsigned char* *BufferAddr); + int Read(unsigned char* *BufferAddr); virtual bool Set(const char* Location, const int Parameter, const int Index); virtual bool Open(void); virtual bool Close(void); diff --git a/protocolhttp.c b/protocolhttp.c index 24984e2..bc6149d 100644 --- a/protocolhttp.c +++ b/protocolhttp.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolhttp.c,v 1.18 2007/10/21 13:31:21 ajhseppa Exp $ + * $Id: protocolhttp.c,v 1.19 2007/10/21 17:32:43 ajhseppa Exp $ */ #include @@ -204,32 +204,6 @@ bool cIptvProtocolHttp::ProcessHeaders(void) return true; } -int cIptvProtocolHttp::Read(unsigned char* *BufferAddr) -{ - //debug("cIptvProtocolHttp::Read()\n"); - // Error out if socket not initialized - if (socketDesc <= 0) { - error("ERROR: Invalid socket in %s\n", __FUNCTION__); - return -1; - } - socklen_t addrlen = sizeof(sockAddr); - // Set argument point to read buffer - *BufferAddr = readBuffer; - // Wait for data - int retval = select_single_desc(socketDesc, 500000, false); - // Check if error - if (retval < 0) - return retval; - // Check if data available - else if (retval) { - // Read data from socket - if (isActive) - return recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, - (struct sockaddr *)&sockAddr, &addrlen); - } - return 0; -} - bool cIptvProtocolHttp::Open(void) { debug("cIptvProtocolHttp::Open()\n"); @@ -245,6 +219,11 @@ bool cIptvProtocolHttp::Close(void) return true; } +int cIptvProtocolHttp::Read(unsigned char* *BufferAddr) +{ + return cIptvTcpSocket::Read(BufferAddr); +} + bool cIptvProtocolHttp::Set(const char* Location, const int Parameter, const int Index) { debug("cIptvProtocolHttp::Set(): Location=%s Parameter=%d Index=%d\n", Location, Parameter, Index); diff --git a/protocolhttp.h b/protocolhttp.h index 5916ab6..e9c188d 100644 --- a/protocolhttp.h +++ b/protocolhttp.h @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolhttp.h,v 1.10 2007/10/21 13:31:21 ajhseppa Exp $ + * $Id: protocolhttp.h,v 1.11 2007/10/21 17:32:43 ajhseppa Exp $ */ #ifndef __IPTV_PROTOCOLHTTP_H @@ -13,7 +13,7 @@ #include "protocolif.h" #include "socket.h" -class cIptvProtocolHttp : public cIptvSocket, public cIptvProtocolIf { +class cIptvProtocolHttp : public cIptvTcpSocket, public cIptvProtocolIf { private: char* streamAddr; char* streamPath; @@ -28,7 +28,7 @@ private: public: cIptvProtocolHttp(); virtual ~cIptvProtocolHttp(); - virtual int Read(unsigned char* *BufferAddr); + int Read(unsigned char* *BufferAddr); virtual bool Set(const char* Location, const int Parameter, const int Index); virtual bool Open(void); virtual bool Close(void); diff --git a/protocoludp.c b/protocoludp.c index 0f993d7..14eb2ad 100644 --- a/protocoludp.c +++ b/protocoludp.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocoludp.c,v 1.19 2007/10/21 13:31:21 ajhseppa Exp $ + * $Id: protocoludp.c,v 1.20 2007/10/21 17:32:43 ajhseppa Exp $ */ #include @@ -74,11 +74,6 @@ bool cIptvProtocolUdp::DropMulticast(void) return true; } -int cIptvProtocolUdp::Read(unsigned char* *BufferAddr) -{ - return ReadUdpSocket(BufferAddr); -} - bool cIptvProtocolUdp::Open(void) { debug("cIptvProtocolUdp::Open()\n"); @@ -97,6 +92,11 @@ bool cIptvProtocolUdp::Close(void) return true; } +int cIptvProtocolUdp::Read(unsigned char* *BufferAddr) +{ + return cIptvUdpSocket::Read(BufferAddr); +} + bool cIptvProtocolUdp::Set(const char* Location, const int Parameter, const int Index) { debug("cIptvProtocolUdp::Set(): Location=%s Parameter=%d Index=%d\n", Location, Parameter, Index); diff --git a/protocoludp.h b/protocoludp.h index 7c7ab92..ddd66de 100644 --- a/protocoludp.h +++ b/protocoludp.h @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocoludp.h,v 1.11 2007/10/21 13:31:21 ajhseppa Exp $ + * $Id: protocoludp.h,v 1.12 2007/10/21 17:32:43 ajhseppa Exp $ */ #ifndef __IPTV_PROTOCOLUDP_H @@ -13,7 +13,7 @@ #include "protocolif.h" #include "socket.h" -class cIptvProtocolUdp : public cIptvSocket, public cIptvProtocolIf { +class cIptvProtocolUdp : public cIptvUdpSocket, public cIptvProtocolIf { private: char* streamAddr; @@ -24,7 +24,7 @@ private: public: cIptvProtocolUdp(); virtual ~cIptvProtocolUdp(); - virtual int Read(unsigned char* *BufferAddr); + int Read(unsigned char* *BufferAddr); virtual bool Set(const char* Location, const int Parameter, const int Index); virtual bool Open(void); virtual bool Close(void); diff --git a/socket.c b/socket.c index 7d8f0f9..1118632 100644 --- a/socket.c +++ b/socket.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: socket.c,v 1.2 2007/10/21 15:49:54 rahrenbe Exp $ + * $Id: socket.c,v 1.3 2007/10/21 17:32:43 ajhseppa Exp $ */ #include @@ -88,9 +88,20 @@ void cIptvSocket::CloseSocket(void) } } -int cIptvSocket::ReadUdpSocket(unsigned char* *BufferAddr) +// UDP socket class +cIptvUdpSocket::cIptvUdpSocket() { - //debug("cIptvSocket::Read()\n"); + debug("cIptvUdpSocket::cIptvUdpSocket()\n"); +} + +cIptvUdpSocket::~cIptvUdpSocket() +{ + debug("cIptvUdpSocket::~cIptvUdpSocket()\n"); +} + +int cIptvUdpSocket::Read(unsigned char* *BufferAddr) +{ + //debug("cIptvUdpSocket::Read()\n"); // Error out if socket not initialized if (socketDesc <= 0) { error("ERROR: Invalid socket in %s\n", __FUNCTION__); @@ -147,3 +158,40 @@ int cIptvSocket::ReadUdpSocket(unsigned char* *BufferAddr) } return 0; } + +// TCP socket class +cIptvTcpSocket::cIptvTcpSocket() +{ + debug("cIptvTcpSocket::cIptvTcpSocket()\n"); +} + +cIptvTcpSocket::~cIptvTcpSocket() +{ + debug("cIptvTcpSocket::~cIptvTcpSocket()\n"); +} + +int cIptvTcpSocket::Read(unsigned char* *BufferAddr) +{ + //debug("cIptvTcpSocket::Read()\n"); + // Error out if socket not initialized + if (socketDesc <= 0) { + error("ERROR: Invalid socket in %s\n", __FUNCTION__); + return -1; + } + socklen_t addrlen = sizeof(sockAddr); + // Set argument point to read buffer + *BufferAddr = readBuffer; + // Wait for data + int retval = select_single_desc(socketDesc, 500000, false); + // Check if error + if (retval < 0) + return retval; + // Check if data available + else if (retval) { + // Read data from socket + if (isActive) + return recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, + (struct sockaddr *)&sockAddr, &addrlen); + } + return 0; +} diff --git a/socket.h b/socket.h index 9141e06..143f66a 100644 --- a/socket.h +++ b/socket.h @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: socket.h,v 1.1 2007/10/21 13:31:21 ajhseppa Exp $ + * $Id: socket.h,v 1.2 2007/10/21 17:32:43 ajhseppa Exp $ */ #ifndef __IPTV_SOCKET_H @@ -23,12 +23,25 @@ protected: protected: bool OpenSocket(const int Port, const bool isUdp); void CloseSocket(void); - int ReadUdpSocket(unsigned char* *BufferAddr); public: cIptvSocket(); virtual ~cIptvSocket(); }; +class cIptvUdpSocket : public cIptvSocket { +public: + cIptvUdpSocket(); + virtual ~cIptvUdpSocket(); + virtual int Read(unsigned char* *BufferAddr); +}; + +class cIptvTcpSocket : public cIptvSocket { +public: + cIptvTcpSocket(); + virtual ~cIptvTcpSocket(); + virtual int Read(unsigned char* *BufferAddr); +}; + #endif // __IPTV_SOCKET_H