vdr-plugin-streamdev/server/server.c

174 lines
4.2 KiB
C
Raw Normal View History

2010-12-02 08:53:01 +01:00
/*
2010-12-02 09:43:13 +01:00
* $Id: server.c,v 1.10 2009/02/13 10:39:22 schmirl Exp $
2010-12-02 08:53:01 +01:00
*/
#include "server/server.h"
#include "server/componentVTP.h"
#include "server/componentHTTP.h"
2010-12-02 09:43:13 +01:00
#include "server/componentIGMP.h"
2010-12-02 08:53:01 +01:00
#include "server/setup.h"
#include <vdr/tools.h>
#include <tools/select.h>
#include <string.h>
#include <errno.h>
cSVDRPhosts StreamdevHosts;
2010-12-02 09:43:13 +01:00
char *opt_auth = NULL;
char *opt_remux = NULL;
2010-12-02 08:53:01 +01:00
cStreamdevServer *cStreamdevServer::m_Instance = NULL;
cList<cServerComponent> cStreamdevServer::m_Servers;
cList<cServerConnection> cStreamdevServer::m_Clients;
cStreamdevServer::cStreamdevServer(void):
2010-12-02 09:43:13 +01:00
cThread("streamdev server")
2010-12-02 08:53:01 +01:00
{
Start();
}
cStreamdevServer::~cStreamdevServer()
{
Stop();
}
void cStreamdevServer::Initialize(void)
{
if (m_Instance == NULL) {
if (StreamdevServerSetup.StartVTPServer) Register(new cComponentVTP);
if (StreamdevServerSetup.StartHTTPServer) Register(new cComponentHTTP);
2010-12-02 09:43:13 +01:00
if (StreamdevServerSetup.StartIGMPServer) {
if (strcmp(StreamdevServerSetup.IGMPBindIP, "0.0.0.0") == 0)
esyslog("streamdev-server: Not starting IGMP. IGMP must be bound to a local IP");
else
Register(new cComponentIGMP);
}
2010-12-02 08:53:01 +01:00
m_Instance = new cStreamdevServer;
}
}
void cStreamdevServer::Destruct(void)
{
DELETENULL(m_Instance);
}
void cStreamdevServer::Stop(void)
{
2010-12-02 09:43:13 +01:00
if (Running())
2010-12-02 08:53:01 +01:00
Cancel(3);
}
void cStreamdevServer::Register(cServerComponent *Server)
{
m_Servers.Add(Server);
}
void cStreamdevServer::Action(void)
{
/* Initialize Server components, deleting those that failed */
for (cServerComponent *c = m_Servers.First(); c;) {
cServerComponent *next = m_Servers.Next(c);
if (!c->Initialize())
m_Servers.Del(c);
c = next;
}
if (m_Servers.Count() == 0) {
esyslog("ERROR: no streamdev server activated, exiting");
2010-12-02 09:43:13 +01:00
Cancel(-1);
2010-12-02 08:53:01 +01:00
}
cTBSelect select;
2010-12-02 09:43:13 +01:00
while (Running()) {
2010-12-02 08:53:01 +01:00
select.Clear();
/* Ask all Server components to register to the selector */
for (cServerComponent *c = m_Servers.First(); c; c = m_Servers.Next(c))
select.Add(c->Socket(), false);
/* Ask all Client connections to register to the selector */
for (cServerConnection *s = m_Clients.First(); s; s = m_Clients.Next(s))
{
select.Add(s->Socket(), false);
if (s->HasData())
select.Add(s->Socket(), true);
}
2010-12-02 08:56:19 +01:00
int sel;
do
{
sel = select.Select(400);
if (sel < 0 && errno == ETIMEDOUT) {
// check for aborted clients
for (cServerConnection *s = m_Clients.First(); s; s = m_Clients.Next(s)) {
if (s->Abort())
sel = 0;
}
}
2010-12-02 09:43:13 +01:00
} while (sel < 0 && errno == ETIMEDOUT && Running());
2010-12-02 08:53:01 +01:00
2010-12-02 09:43:13 +01:00
if (!Running())
2010-12-02 08:56:19 +01:00
break;
if (sel < 0) {
esyslog("fatal error, server exiting: %m");
2010-12-02 08:53:01 +01:00
break;
}
/* Ask all Server components to act on signalled sockets */
for (cServerComponent *c = m_Servers.First(); c; c = m_Servers.Next(c)){
2010-12-02 08:56:19 +01:00
if (sel && select.CanRead(c->Socket())) {
2010-12-02 08:53:01 +01:00
cServerConnection *client = c->Accept();
2010-12-02 09:43:13 +01:00
if (!client)
continue;
2010-12-02 08:53:01 +01:00
m_Clients.Add(client);
if (m_Clients.Count() > StreamdevServerSetup.MaxClients) {
esyslog("streamdev: too many clients, rejecting %s:%d",
client->RemoteIp().c_str(), client->RemotePort());
client->Reject();
2010-12-02 09:43:13 +01:00
} else if (!client->CanAuthenticate() && !StreamdevHosts.Acceptable(client->RemoteIpAddr())) {
2010-12-02 08:53:01 +01:00
esyslog("streamdev: client %s:%d not allowed to connect",
client->RemoteIp().c_str(), client->RemotePort());
client->Reject();
} else
client->Welcome();
}
}
/* Ask all Client connections to act on signalled sockets */
for (cServerConnection *s = m_Clients.First(); s;) {
bool result = true;
2010-12-02 08:56:19 +01:00
if (sel && select.CanWrite(s->Socket()))
2010-12-02 08:53:01 +01:00
result = s->Write();
2010-12-02 08:56:19 +01:00
if (sel && result && select.CanRead(s->Socket()))
2010-12-02 08:53:01 +01:00
result = s->Read();
2010-12-02 08:56:19 +01:00
result &= !s->Abort();
2010-12-02 08:53:01 +01:00
cServerConnection *next = m_Clients.Next(s);
if (!result) {
isyslog("streamdev: closing streamdev connection to %s:%d",
s->RemoteIp().c_str(), s->RemotePort());
s->Close();
m_Clients.Del(s);
}
s = next;
}
}
while (m_Clients.Count() > 0) {
cServerConnection *s = m_Clients.First();
s->Close();
m_Clients.Del(s);
}
while (m_Servers.Count() > 0) {
cServerComponent *c = m_Servers.First();
c->Destruct();
m_Servers.Del(c);
}
}