2004-12-30 23:43:55 +01:00
|
|
|
/*
|
2010-12-02 09:57:17 +01:00
|
|
|
* $Id: connection.h,v 1.10 2010/08/03 10:46:41 schmirl Exp $
|
2004-12-30 23:43:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef VDR_STREAMDEV_SERVER_CONNECTION_H
|
|
|
|
#define VDR_STREAMDEV_SERVER_CONNECTION_H
|
|
|
|
|
|
|
|
#include "tools/socket.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
2010-12-02 09:57:17 +01:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
typedef std::map<std::string,std::string> tStrStrMap;
|
|
|
|
typedef std::pair<std::string,std::string> tStrStr;
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
class cChannel;
|
|
|
|
class cDevice;
|
2011-03-07 21:37:08 +01:00
|
|
|
class cSwitchLive;
|
2004-12-30 23:43:55 +01:00
|
|
|
|
|
|
|
/* Basic capabilities of a straight text-based protocol, most functions
|
|
|
|
virtual to support more complicated protocols */
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
class cServerConnection: public cListObject, public cTBSocket
|
|
|
|
{
|
2004-12-30 23:43:55 +01:00
|
|
|
private:
|
2005-05-09 22:22:29 +02:00
|
|
|
const char *m_Protocol;
|
|
|
|
bool m_DeferClose;
|
|
|
|
bool m_Pending;
|
|
|
|
|
|
|
|
char m_ReadBuffer[MAXPARSEBUFFER];
|
|
|
|
uint m_ReadBytes;
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
char m_WriteBuffer[MAXPARSEBUFFER];
|
|
|
|
uint m_WriteBytes;
|
|
|
|
uint m_WriteIndex;
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2011-03-07 21:37:08 +01:00
|
|
|
cSwitchLive *m_SwitchLive;
|
|
|
|
|
2010-12-02 09:57:17 +01:00
|
|
|
tStrStrMap m_Headers;
|
|
|
|
|
|
|
|
/* Check if a device would be available for transfering the given
|
|
|
|
channel. This call has no side effects except for temporarily
|
|
|
|
detaching this connection's receivers. */
|
|
|
|
cDevice *CheckDevice(const cChannel *Channel, int Priority, bool LiveView, const cDevice *AvoidDevice = NULL);
|
|
|
|
|
2011-01-28 12:42:33 +01:00
|
|
|
/* Test if device is in use as the transfer mode receiver device
|
|
|
|
or a FF card, displaying live TV from internal tuner */
|
|
|
|
static bool UsedByLiveTV(cDevice *device);
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
protected:
|
|
|
|
/* Will be called when a command terminated by a newline has been
|
|
|
|
received */
|
|
|
|
virtual bool Command(char *Cmd) = 0;
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
/* Will put Message into the response queue, which will be sent in the next
|
|
|
|
server cycle. Note that Message will be line-terminated by Respond.
|
|
|
|
Only one line at a time may be sent. If there are lines to follow, set
|
|
|
|
Last to false. Command(NULL) will be called in the next cycle, so you can
|
|
|
|
post the next line. */
|
2007-04-16 13:01:02 +02:00
|
|
|
virtual bool Respond(const char *Message, bool Last = true, ...);
|
|
|
|
//__attribute__ ((format (printf, 2, 4)));
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2010-12-02 09:57:17 +01:00
|
|
|
/* Add a request header */
|
|
|
|
void SetHeader(const char *Name, const char *Value, const char *Prefix = "") { m_Headers.insert(tStrStr(std::string(Prefix) + Name, Value)); }
|
|
|
|
|
|
|
|
static const cChannel *ChannelFromString(const char *String, int *Apid = NULL, int *Dpid = NULL);
|
2009-09-18 12:41:41 +02:00
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
public:
|
|
|
|
/* If you derive, specify a short string such as HTTP for Protocol, which
|
|
|
|
will be displayed in error messages */
|
2009-02-13 11:39:20 +01:00
|
|
|
cServerConnection(const char *Protocol, int Type = SOCK_STREAM);
|
2004-12-30 23:43:55 +01:00
|
|
|
virtual ~cServerConnection();
|
|
|
|
|
2008-10-14 13:05:46 +02:00
|
|
|
/* If true, any client IP will be accepted */
|
|
|
|
virtual bool CanAuthenticate(void) { return false; }
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
/* Gets called if the client has been accepted by the core */
|
|
|
|
virtual void Welcome(void) { }
|
|
|
|
|
|
|
|
/* Gets called if the client has been rejected by the core */
|
|
|
|
virtual void Reject(void) { DeferClose(); }
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
/* Get the client socket's file number */
|
|
|
|
virtual int Socket(void) const { return (int)*this; }
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
/* Determine if there is data to send or any command pending */
|
|
|
|
virtual bool HasData(void) const;
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
/* Gets called by server when the socket can accept more data. Writes
|
|
|
|
the buffer filled up by Respond(). Calls Command(NULL) if there is a
|
|
|
|
command pending. Returns false in case of an error */
|
|
|
|
virtual bool Write(void);
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
/* Gets called by server when there is incoming data to read. Calls
|
|
|
|
Command() for each line. Returns false in case of an error, or if
|
|
|
|
the connection shall be closed and removed by the server */
|
|
|
|
virtual bool Read(void);
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2007-04-02 12:32:34 +02:00
|
|
|
/* Is polled regularely by the server. Returns true if the connection
|
|
|
|
needs to be terminated. */
|
|
|
|
virtual bool Abort(void) const = 0;
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
/* Will make the socket close after sending all queued output data */
|
|
|
|
void DeferClose(void) { m_DeferClose = true; }
|
|
|
|
|
2010-12-02 09:57:17 +01:00
|
|
|
/* Will retrieve an unused device for transmitting data. Receivers have
|
|
|
|
already been attached from the device if necessary. Use the returned
|
2004-12-30 23:43:55 +01:00
|
|
|
cDevice in a following call to StartTransfer */
|
|
|
|
cDevice *GetDevice(const cChannel *Channel, int Priority);
|
|
|
|
|
2010-12-02 09:57:17 +01:00
|
|
|
/* Test if a call to GetDevice would return a usable device. */
|
|
|
|
bool ProvidesChannel(const cChannel *Channel, int Priority);
|
|
|
|
|
2011-03-07 21:37:08 +01:00
|
|
|
/* Do things which must be done in VDR's main loop */
|
|
|
|
void MainThreadHook();
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
virtual void Flushed(void) {}
|
|
|
|
|
|
|
|
virtual void Detach(void) = 0;
|
|
|
|
virtual void Attach(void) = 0;
|
2010-12-02 09:57:17 +01:00
|
|
|
|
|
|
|
/* This connections protocol name */
|
|
|
|
virtual const char* Protocol(void) const { return m_Protocol; }
|
|
|
|
|
|
|
|
/* std::map with additional information */
|
|
|
|
const tStrStrMap& Headers(void) const { return m_Headers; }
|
2004-12-30 23:43:55 +01:00
|
|
|
};
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
inline bool cServerConnection::HasData(void) const
|
|
|
|
{
|
|
|
|
return m_WriteBytes > 0 || m_Pending || m_DeferClose;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // VDR_STREAMDEV_SERVER_CONNECTION_H
|