Close connection when client is gone. Fixes high CPU load problem (#201)

Modified Files:
	server/connection.h server/connectionHTTP.h
	server/connectionVTP.h server/server.c server/streamer.c
	server/streamer.h tools/select.c tools/select.h tools/source.c
This commit is contained in:
schmirl
2007-04-02 10:32:34 +00:00
parent cd7d4e3588
commit 525574f9b0
9 changed files with 76 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
/*
* $Id: connection.h,v 1.3 2005/05/09 20:22:29 lordjaxom Exp $
* $Id: connection.h,v 1.4 2007/04/02 10:32:34 schmirl Exp $
*/
#ifndef VDR_STREAMDEV_SERVER_CONNECTION_H
@@ -69,6 +69,10 @@ public:
the connection shall be closed and removed by the server */
virtual bool Read(void);
/* Is polled regularely by the server. Returns true if the connection
needs to be terminated. */
virtual bool Abort(void) const = 0;
/* Will make the socket close after sending all queued output data */
void DeferClose(void) { m_DeferClose = true; }

View File

@@ -1,5 +1,5 @@
/*
* $Id: connectionHTTP.h,v 1.3 2005/02/11 16:44:15 lordjaxom Exp $
* $Id: connectionHTTP.h,v 1.4 2007/04/02 10:32:34 schmirl Exp $
*/
#ifndef VDR_STREAMDEV_SERVERS_CONNECTIONHTTP_H
@@ -52,7 +52,13 @@ public:
virtual bool Command(char *Cmd);
bool CmdGET(const std::string &Opts);
virtual bool Abort(void) const;
virtual void Flushed(void);
};
inline bool cConnectionHTTP::Abort(void) const
{
return m_LiveStreamer && m_LiveStreamer->Abort();
}
#endif // VDR_STREAMDEV_SERVERS_CONNECTIONVTP_H

View File

@@ -2,9 +2,9 @@
#define VDR_STREAMDEV_SERVERS_CONNECTIONVTP_H
#include "server/connection.h"
#include "server/livestreamer.h"
class cTBSocket;
class cStreamdevLiveStreamer;
class cLSTEHandler;
class cLSTCHandler;
class cLSTTHandler;
@@ -39,6 +39,7 @@ public:
virtual void Welcome(void);
virtual void Reject(void);
virtual bool Abort(void) const;
virtual void Detach(void);
virtual void Attach(void);
@@ -72,4 +73,9 @@ public:
__attribute__ ((format (printf, 3, 4)));
};
inline bool cConnectionVTP::Abort(void) const
{
return m_LiveStreamer && m_LiveStreamer->Abort();
}
#endif // VDR_STREAMDEV_SERVERS_CONNECTIONVTP_H

View File

@@ -1,5 +1,5 @@
/*
* $Id: server.c,v 1.4 2006/11/10 11:52:41 schmirl Exp $
* $Id: server.c,v 1.5 2007/04/02 10:32:34 schmirl Exp $
*/
#include "server/server.h"
@@ -91,20 +91,29 @@ void cStreamdevServer::Action(void)
select.Add(s->Socket(), true);
}
int result;
while ((result = select.Select(100)) < 0 && errno == ETIMEDOUT) {
if (!m_Active) break;
}
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;
}
}
} while (sel < 0 && errno == ETIMEDOUT && m_Active);
if (result < 0) {
if (m_Active) // no exit was requested while polling
esyslog("fatal error, server exiting: %m");
if (!m_Active)
break;
if (sel < 0) {
esyslog("fatal error, server exiting: %m");
break;
}
/* Ask all Server components to act on signalled sockets */
for (cServerComponent *c = m_Servers.First(); c; c = m_Servers.Next(c)){
if (select.CanRead(c->Socket())) {
if (sel && select.CanRead(c->Socket())) {
cServerConnection *client = c->Accept();
m_Clients.Add(client);
@@ -125,11 +134,13 @@ void cStreamdevServer::Action(void)
for (cServerConnection *s = m_Clients.First(); s;) {
bool result = true;
if (select.CanWrite(s->Socket()))
if (sel && select.CanWrite(s->Socket()))
result = s->Write();
if (result && select.CanRead(s->Socket()))
if (sel && result && select.CanRead(s->Socket()))
result = s->Read();
result &= !s->Abort();
cServerConnection *next = m_Clients.Next(s);
if (!result) {

View File

@@ -1,5 +1,5 @@
/*
* $Id: streamer.c,v 1.14 2005/05/09 20:22:29 lordjaxom Exp $
* $Id: streamer.c,v 1.15 2007/04/02 10:32:34 schmirl Exp $
*/
#include <vdr/ringbuffer.h>
@@ -40,6 +40,9 @@ void cStreamdevWriter::Action(void)
uchar *block = NULL;
int count, offset = 0;
m_Active = true;
sel.Clear();
sel.Add(*m_Socket, true);
while (m_Active) {
if (block == NULL) {
block = m_Streamer->Get(count);
@@ -47,9 +50,7 @@ void cStreamdevWriter::Action(void)
}
if (block != NULL) {
sel.Clear();
sel.Add(*m_Socket, true);
if (sel.Select(500) == -1) {
if (sel.Select(15000) == -1) {
esyslog("ERROR: streamdev-server: couldn't send data: %m");
break;
}

View File

@@ -1,5 +1,5 @@
/*
* $Id: streamer.h,v 1.7 2005/03/12 12:54:19 lordjaxom Exp $
* $Id: streamer.h,v 1.8 2007/04/02 10:32:34 schmirl Exp $
*/
#ifndef VDR_STREAMDEV_STREAMER_H
@@ -29,6 +29,8 @@ protected:
public:
cStreamdevWriter(cTBSocket *Socket, cStreamdevStreamer *Streamer);
virtual ~cStreamdevWriter();
bool IsActive(void) const { return m_Active; }
};
// --- cStreamdevStreamer -----------------------------------------------------
@@ -52,6 +54,7 @@ public:
virtual void Start(cTBSocket *Socket);
virtual void Stop(void);
bool Abort(void) const;
void Activate(bool On);
int Receive(uchar *Data, int Length) { return m_RingBuffer->Put(Data, Length); }
@@ -65,5 +68,10 @@ public:
virtual void Attach(void) {}
};
inline bool cStreamdevStreamer::Abort(void) const
{
return m_Active && !m_Writer->IsActive();
}
#endif // VDR_STREAMDEV_STREAMER_H