2004-12-30 23:43:55 +01:00
|
|
|
/*
|
2005-05-09 22:30:38 +02:00
|
|
|
* $Id: connection.c,v 1.5 2005/05/09 20:30:38 lordjaxom Exp $
|
2004-12-30 23:43:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "server/connection.h"
|
|
|
|
#include "server/setup.h"
|
|
|
|
#include "server/suspend.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include <vdr/tools.h>
|
|
|
|
#include <string.h>
|
2005-05-09 22:30:38 +02:00
|
|
|
#include <stdarg.h>
|
2004-12-30 23:43:55 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
cServerConnection::cServerConnection(const char *Protocol):
|
|
|
|
m_Protocol(Protocol),
|
|
|
|
m_DeferClose(false),
|
|
|
|
m_Pending(false),
|
|
|
|
m_ReadBytes(0),
|
|
|
|
m_WriteBytes(0),
|
|
|
|
m_WriteIndex(0)
|
|
|
|
{
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
cServerConnection::~cServerConnection()
|
|
|
|
{
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
bool cServerConnection::Read(void)
|
|
|
|
{
|
|
|
|
int b;
|
|
|
|
if ((b = cTBSocket::Read(m_ReadBuffer + m_ReadBytes,
|
|
|
|
sizeof(m_ReadBuffer) - m_ReadBytes - 1)) < 0) {
|
|
|
|
esyslog("ERROR: read from client (%s) %s:%d failed: %m",
|
|
|
|
m_Protocol, RemoteIp().c_str(), RemotePort());
|
|
|
|
return false;
|
|
|
|
}
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
if (b == 0) {
|
|
|
|
isyslog("client (%s) %s:%d has closed connection",
|
|
|
|
m_Protocol, RemoteIp().c_str(), RemotePort());
|
|
|
|
return false;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
m_ReadBytes += b;
|
|
|
|
m_ReadBuffer[m_ReadBytes] = '\0';
|
|
|
|
|
|
|
|
char *end;
|
|
|
|
bool result = true;
|
|
|
|
while ((end = strchr(m_ReadBuffer, '\012')) != NULL) {
|
|
|
|
*end = '\0';
|
|
|
|
if (end > m_ReadBuffer && *(end - 1) == '\015')
|
|
|
|
*(end - 1) = '\0';
|
|
|
|
|
|
|
|
if (!Command(m_ReadBuffer))
|
2004-12-30 23:43:55 +01:00
|
|
|
return false;
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
m_ReadBytes -= ++end - m_ReadBuffer;
|
|
|
|
if (m_ReadBytes > 0)
|
|
|
|
memmove(m_ReadBuffer, end, m_ReadBytes);
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
if (m_ReadBytes == sizeof(m_ReadBuffer) - 1) {
|
|
|
|
esyslog("ERROR: streamdev: input buffer overflow (%s) for %s:%d",
|
|
|
|
m_Protocol, RemoteIp().c_str(), RemotePort());
|
|
|
|
return false;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
2005-05-09 22:22:29 +02:00
|
|
|
|
|
|
|
return result;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
bool cServerConnection::Write(void)
|
|
|
|
{
|
|
|
|
int b;
|
|
|
|
if ((b = cTBSocket::Write(m_WriteBuffer + m_WriteIndex,
|
|
|
|
m_WriteBytes - m_WriteIndex)) < 0) {
|
|
|
|
esyslog("ERROR: streamdev: write to client (%s) %s:%d failed: %m",
|
|
|
|
m_Protocol, RemoteIp().c_str(), RemotePort());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_WriteIndex += b;
|
|
|
|
if (m_WriteIndex == m_WriteBytes) {
|
|
|
|
m_WriteIndex = 0;
|
|
|
|
m_WriteBytes = 0;
|
|
|
|
if (m_Pending)
|
|
|
|
Command(NULL);
|
|
|
|
if (m_DeferClose)
|
2004-12-30 23:43:55 +01:00
|
|
|
return false;
|
2005-05-09 22:22:29 +02:00
|
|
|
Flushed();
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
bool cServerConnection::Respond(const char *Message, bool Last, ...)
|
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
int length;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, Last);
|
|
|
|
length = vasprintf(&buffer, Message, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (m_WriteBytes + length + 2 > sizeof(m_WriteBuffer)) {
|
|
|
|
esyslog("ERROR: streamdev: output buffer overflow (%s) for %s:%d",
|
|
|
|
m_Protocol, RemoteIp().c_str(), RemotePort());
|
2004-12-30 23:43:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
2005-05-09 22:22:29 +02:00
|
|
|
Dprintf("OUT: |%s|\n", buffer);
|
|
|
|
memcpy(m_WriteBuffer + m_WriteBytes, buffer, length);
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
m_WriteBytes += length;
|
|
|
|
m_WriteBuffer[m_WriteBytes++] = '\015';
|
|
|
|
m_WriteBuffer[m_WriteBytes++] = '\012';
|
|
|
|
m_Pending = !Last;
|
2004-12-30 23:43:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
cDevice *cServerConnection::GetDevice(const cChannel *Channel, int Priority)
|
|
|
|
{
|
2004-12-30 23:43:55 +01:00
|
|
|
cDevice *device = NULL;
|
|
|
|
|
|
|
|
/*Dprintf("+ Statistics:\n");
|
|
|
|
Dprintf("+ Current Channel: %d\n", cDevice::CurrentChannel());
|
|
|
|
Dprintf("+ Current Device: %d\n", cDevice::ActualDevice()->CardIndex());
|
|
|
|
Dprintf("+ Transfer Mode: %s\n", cDevice::ActualDevice()
|
|
|
|
== cDevice::PrimaryDevice() ? "false" : "true");
|
|
|
|
Dprintf("+ Replaying: %s\n", cDevice::PrimaryDevice()->Replaying() ? "true"
|
|
|
|
: "false");*/
|
|
|
|
|
|
|
|
Dprintf(" * GetDevice(const cChannel*, int)\n");
|
|
|
|
Dprintf(" * -------------------------------\n");
|
|
|
|
|
|
|
|
device = cDevice::GetDevice(Channel, Priority);
|
|
|
|
|
|
|
|
Dprintf(" * Found following device: %p (%d)\n", device,
|
|
|
|
device ? device->CardIndex() + 1 : 0);
|
|
|
|
if (device == cDevice::ActualDevice())
|
|
|
|
Dprintf(" * is actual device\n");
|
|
|
|
if (!cSuspendCtl::IsActive() && StreamdevServerSetup.SuspendMode != smAlways)
|
|
|
|
Dprintf(" * NOT suspended\n");
|
|
|
|
|
|
|
|
if (!device || (device == cDevice::ActualDevice()
|
|
|
|
&& !cSuspendCtl::IsActive()
|
|
|
|
&& StreamdevServerSetup.SuspendMode != smAlways)) {
|
|
|
|
// mustn't switch actual device
|
|
|
|
// maybe a device would be free if THIS connection did turn off its streams?
|
|
|
|
Dprintf(" * trying again...\n");
|
|
|
|
const cChannel *current = Channels.GetByNumber(cDevice::CurrentChannel());
|
2005-03-24 22:31:38 +01:00
|
|
|
//isyslog("streamdev-server: Detaching current receiver");
|
|
|
|
//Detach(); XXX+
|
2004-12-30 23:43:55 +01:00
|
|
|
device = cDevice::GetDevice(Channel, Priority);
|
2005-03-24 22:31:38 +01:00
|
|
|
//Attach(); XXX+
|
2004-12-30 23:43:55 +01:00
|
|
|
Dprintf(" * Found following device: %p (%d)\n", device,
|
|
|
|
device ? device->CardIndex() + 1 : 0);
|
|
|
|
if (device == cDevice::ActualDevice())
|
|
|
|
Dprintf(" * is actual device\n");
|
|
|
|
if (!cSuspendCtl::IsActive()
|
|
|
|
&& StreamdevServerSetup.SuspendMode != smAlways)
|
|
|
|
Dprintf(" * NOT suspended\n");
|
|
|
|
if (current && !TRANSPONDER(Channel, current))
|
|
|
|
Dprintf(" * NOT same transponder\n");
|
|
|
|
if (device && (device == cDevice::ActualDevice()
|
|
|
|
&& !cSuspendCtl::IsActive()
|
|
|
|
&& StreamdevServerSetup.SuspendMode != smAlways
|
|
|
|
&& current != NULL
|
|
|
|
&& !TRANSPONDER(Channel, current))) {
|
|
|
|
// now we would have to switch away live tv...let's see if live tv
|
|
|
|
// can be handled by another device
|
|
|
|
cDevice *newdev = NULL;
|
|
|
|
for (int i = 0; i < cDevice::NumDevices(); ++i) {
|
|
|
|
cDevice *dev = cDevice::GetDevice(i);
|
|
|
|
if (dev->ProvidesChannel(current, 0) && dev != device) {
|
|
|
|
newdev = dev;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Dprintf(" * Found device for live tv: %p (%d)\n", newdev,
|
|
|
|
newdev ? newdev->CardIndex() + 1 : 0);
|
|
|
|
if (newdev == NULL || newdev == device)
|
|
|
|
// no suitable device to continue live TV, giving up...
|
|
|
|
device = NULL;
|
|
|
|
else
|
|
|
|
newdev->SwitchChannel(current, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return device;
|
|
|
|
}
|