mirror of
https://github.com/rofafor/vdr-plugin-iptv.git
synced 2023-10-10 13:37:03 +02:00
Added private functions for Checking + creating and closing sockets.
This commit is contained in:
parent
59d56fe5c0
commit
1621555dd8
108
streamer.c
108
streamer.c
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* See the README file for copyright information and how to reach the author.
|
* See the README file for copyright information and how to reach the author.
|
||||||
*
|
*
|
||||||
* $Id: streamer.c,v 1.3 2007/09/12 18:30:50 rahrenbe Exp $
|
* $Id: streamer.c,v 1.4 2007/09/12 18:33:56 ajhseppa Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -29,23 +29,12 @@ cIptvStreamer::cIptvStreamer(cRingBufferLinear* BufferPtr, cMutex* Mutex)
|
|||||||
socketActive(false),
|
socketActive(false),
|
||||||
mcastActive(false)
|
mcastActive(false)
|
||||||
{
|
{
|
||||||
int yes = 1;
|
|
||||||
|
|
||||||
debug("cIptvStreamer::cIptvStreamer()\n");
|
debug("cIptvStreamer::cIptvStreamer()\n");
|
||||||
memset(&stream, '\0', strlen(stream));
|
memset(&stream, '\0', strlen(stream));
|
||||||
|
|
||||||
// Create socket
|
// Create the socket
|
||||||
socketDesc = socket(PF_INET, SOCK_DGRAM, 0);
|
CheckAndCreateSocket(dataPort);
|
||||||
if (socketDesc < 0) {
|
|
||||||
char tmp[64];
|
|
||||||
error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allow multiple sockets to use the same PORT number
|
|
||||||
if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
|
|
||||||
char tmp[64];
|
|
||||||
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
|
|
||||||
}
|
|
||||||
|
|
||||||
pReceiveBuffer = MALLOC(unsigned char, bufferSize);
|
pReceiveBuffer = MALLOC(unsigned char, bufferSize);
|
||||||
if (!pReceiveBuffer)
|
if (!pReceiveBuffer)
|
||||||
@ -61,8 +50,8 @@ cIptvStreamer::~cIptvStreamer()
|
|||||||
if (pReceiveBuffer)
|
if (pReceiveBuffer)
|
||||||
free(pReceiveBuffer);
|
free(pReceiveBuffer);
|
||||||
|
|
||||||
close(socketDesc);
|
// Close the socket
|
||||||
socketActive = false;
|
CloseSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
void cIptvStreamer::Action()
|
void cIptvStreamer::Action()
|
||||||
@ -82,6 +71,62 @@ void cIptvStreamer::Action()
|
|||||||
debug("cIptvStreamer::Action(): Exiting\n");
|
debug("cIptvStreamer::Action(): Exiting\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool cIptvStreamer::CheckAndCreateSocket(const int port)
|
||||||
|
{
|
||||||
|
// If socket is there already and it is bound to a different port, it must
|
||||||
|
// be closed first
|
||||||
|
if (socketActive && port != dataPort) {
|
||||||
|
debug("Full tear-down of active socket\n");
|
||||||
|
CloseSocket();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind to the socket if it is not active already
|
||||||
|
if (!socketActive) {
|
||||||
|
|
||||||
|
// Create socket
|
||||||
|
socketDesc = socket(PF_INET, SOCK_DGRAM, 0);
|
||||||
|
if (socketDesc < 0) {
|
||||||
|
char tmp[64];
|
||||||
|
error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp)));
|
||||||
|
}
|
||||||
|
|
||||||
|
int yes = 1;
|
||||||
|
|
||||||
|
// Allow multiple sockets to use the same PORT number
|
||||||
|
if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes,
|
||||||
|
sizeof(yes)) < 0) {
|
||||||
|
char tmp[64];
|
||||||
|
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&sa, '\0', sizeof(sa));
|
||||||
|
sa.sin_family = AF_INET;
|
||||||
|
sa.sin_port = htons(port);
|
||||||
|
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
|
||||||
|
int err = bind(socketDesc, (struct sockaddr *)&sa, sizeof(sa));
|
||||||
|
if (err < 0) {
|
||||||
|
char tmp[64];
|
||||||
|
error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp)));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
dataPort = port;
|
||||||
|
socketActive = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void cIptvStreamer::CloseSocket()
|
||||||
|
{
|
||||||
|
if (socketActive) {
|
||||||
|
close(socketDesc);
|
||||||
|
socketActive = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool cIptvStreamer::Activate()
|
bool cIptvStreamer::Activate()
|
||||||
{
|
{
|
||||||
struct ip_mreq mreq;
|
struct ip_mreq mreq;
|
||||||
@ -102,6 +147,9 @@ bool cIptvStreamer::Activate()
|
|||||||
if (!Running())
|
if (!Running())
|
||||||
Start();
|
Start();
|
||||||
|
|
||||||
|
// Ensure that socket is valid
|
||||||
|
CheckAndCreateSocket(dataPort);
|
||||||
|
|
||||||
// Join a new multicast group
|
// Join a new multicast group
|
||||||
mreq.imr_multiaddr.s_addr = inet_addr(stream);
|
mreq.imr_multiaddr.s_addr = inet_addr(stream);
|
||||||
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||||
@ -149,35 +197,13 @@ bool cIptvStreamer::SetStream(const char* address, const int port, const int pro
|
|||||||
{
|
{
|
||||||
debug("cIptvStreamer::SetChannel(): channel = %s:%d\n", address, port);
|
debug("cIptvStreamer::SetChannel(): channel = %s:%d\n", address, port);
|
||||||
|
|
||||||
if (port != dataPort) {
|
|
||||||
error("ERROR: Support for full re-initialization is not implemented!\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bind to the socket if it is not active already
|
|
||||||
if (!socketActive) {
|
|
||||||
debug("cIptvStreamer::SetChannel(): Binding socket to %s:%d\n", address, port);
|
|
||||||
dataPort = port;
|
|
||||||
|
|
||||||
memset(&sa, '\0', sizeof(sa));
|
|
||||||
sa.sin_family = AF_INET;
|
|
||||||
sa.sin_port = htons(dataPort);
|
|
||||||
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
|
|
||||||
int err = bind(socketDesc, (struct sockaddr *)&sa, sizeof(sa));
|
|
||||||
if (err < 0) {
|
|
||||||
char tmp[64];
|
|
||||||
error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp)));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
socketActive = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// De-activate the reception if it is running currently. Otherwise the
|
// De-activate the reception if it is running currently. Otherwise the
|
||||||
// reception stream is overwritten and cannot be un-set after this
|
// reception stream is overwritten and cannot be un-set after this
|
||||||
Deactivate();
|
Deactivate();
|
||||||
|
|
||||||
|
// Ensure that the socket is valid
|
||||||
|
CheckAndCreateSocket(port);
|
||||||
|
|
||||||
// Check if the address fits into the buffer
|
// Check if the address fits into the buffer
|
||||||
if (strlen(address) > sizeof(stream)) {
|
if (strlen(address) > sizeof(stream)) {
|
||||||
error("ERROR: Address too big\n");
|
error("ERROR: Address too big\n");
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* See the README file for copyright information and how to reach the author.
|
* See the README file for copyright information and how to reach the author.
|
||||||
*
|
*
|
||||||
* $Id: streamer.h,v 1.1 2007/09/12 17:28:59 rahrenbe Exp $
|
* $Id: streamer.h,v 1.2 2007/09/12 18:33:56 ajhseppa Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __IPTV_STREAMER_H
|
#ifndef __IPTV_STREAMER_H
|
||||||
@ -28,6 +28,9 @@ private:
|
|||||||
bool socketActive;
|
bool socketActive;
|
||||||
bool mcastActive;
|
bool mcastActive;
|
||||||
|
|
||||||
|
bool CheckAndCreateSocket(const int port);
|
||||||
|
void CloseSocket();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum {
|
enum {
|
||||||
PROTOCOL_UDP,
|
PROTOCOL_UDP,
|
||||||
|
Loading…
Reference in New Issue
Block a user