summaryrefslogtreecommitdiff
path: root/svdrpclient.h
diff options
context:
space:
mode:
authorJohann Friedrichs <johann.friedrichs@web.de>2018-03-21 12:14:55 +0100
committerJohann Friedrichs <johann.friedrichs@web.de>2018-03-21 12:14:55 +0100
commite8a0e569152c50d6084f252d12854b8fd4e74466 (patch)
tree5a90ef7ea08ff2096df157ca109c5268cdc04903 /svdrpclient.h
parent9c7d95ff8d6ba965cb23147507a859b1fd0491d6 (diff)
downloadvdr-plugin-epgsearch-e8a0e569152c50d6084f252d12854b8fd4e74466.tar.gz
vdr-plugin-epgsearch-e8a0e569152c50d6084f252d12854b8fd4e74466.tar.bz2
unified indentation
Diffstat (limited to 'svdrpclient.h')
-rw-r--r--svdrpclient.h196
1 files changed, 92 insertions, 104 deletions
diff --git a/svdrpclient.h b/svdrpclient.h
index e9533fe..5e36722 100644
--- a/svdrpclient.h
+++ b/svdrpclient.h
@@ -31,115 +31,103 @@ The project's page is at http://winni.vdr-developer.org/epgsearch
#define SVDRPDISCONNECT 221
#define CMDSUCCESS 250
-namespace epgsSVDRP {
+namespace epgsSVDRP
+{
-class cSVDRPClient {
- private:
+class cSVDRPClient
+{
+private:
int sock;
public:
bool bConnected;
- cSVDRPClient()
- {
- bConnected = false;
- sock = socket(PF_INET, SOCK_STREAM, 0);
- if (sock < 0)
- {
- LogFile.eSysLog("error creating socket!");
- return;
- }
-
- struct sockaddr_in AdrSock;
- AdrSock.sin_family = AF_INET;
- AdrSock.sin_port = htons(EPGSearchConfig.SVDRPPort);
- inet_aton("127.0.0.1", &AdrSock.sin_addr);
-
- if (connect(sock, (struct sockaddr*)&AdrSock, sizeof(AdrSock)) == -1)
- {
- LogFile.eSysLog("error connecting to socket!");
- return;
- }
- bConnected = (Receive() == SVDRPCONNECT);
- if (!bConnected)
- LogFile.eSysLog("EPGSearch: could not connect to VDR!");
- }
-
- ~cSVDRPClient()
- {
- if (sock >= 0)
- close(sock);
- }
-
- bool SendCmd(const char* cmd)
- {
- if (!bConnected)
- return false;
-
- cString szCmd = cString::sprintf("%s\r\n", cmd);
- Send(*szCmd);
- bool cmdret = (Receive() == CMDSUCCESS);
-
- szCmd = cString::sprintf("QUIT\r\n");
- Send(szCmd);
-
- long rc = 0;
- if ((rc = Receive()) != SVDRPDISCONNECT)
- LogFile.eSysLog("could not disconnect (%ld)!", rc);
-
- close(sock);
- sock = -1;
- return cmdret;
- }
- bool Send(const char* szSend)
- {
- int length = strlen(szSend);
- int sent = 0;
- do
- {
- sent += send(sock, szSend + sent, length - sent, 0);
- if( sent < 0 )
- {
- LogFile.eSysLog("error sending command!");
- return false;
- }
- }
- while ( sent < length );
- return true;
- }
- long Receive()
- {
- char* csResp = strdup("");
- char ch;
- long rc = 0;
-
- bool bCheckMultiLine = true;
-
- while(bCheckMultiLine)
- {
- while( strlen(csResp) < 2 || strcmp(csResp + strlen(csResp) - 2, "\r\n") != 0)
- {
- if (recv(sock, &ch, 1, 0 ) < 0)
- {
- LogFile.eSysLog("EPGSearch: error receiving response!");
- free(csResp);
- return -1;
- }
- char* Temp = NULL;
- msprintf(&Temp, "%s%c", csResp, ch);
- free(csResp);
- csResp = Temp;
- }
- if( csResp[3] == ' ' )
- {
- bCheckMultiLine = false;
- rc = atol(csResp);
- }
- free(csResp);
- csResp = strdup("");
- }
- free(csResp);
- return rc;
- }
+ cSVDRPClient() {
+ bConnected = false;
+ sock = socket(PF_INET, SOCK_STREAM, 0);
+ if (sock < 0) {
+ LogFile.eSysLog("error creating socket!");
+ return;
+ }
+
+ struct sockaddr_in AdrSock;
+ AdrSock.sin_family = AF_INET;
+ AdrSock.sin_port = htons(EPGSearchConfig.SVDRPPort);
+ inet_aton("127.0.0.1", &AdrSock.sin_addr);
+
+ if (connect(sock, (struct sockaddr*)&AdrSock, sizeof(AdrSock)) == -1) {
+ LogFile.eSysLog("error connecting to socket!");
+ return;
+ }
+ bConnected = (Receive() == SVDRPCONNECT);
+ if (!bConnected)
+ LogFile.eSysLog("EPGSearch: could not connect to VDR!");
+ }
+
+ ~cSVDRPClient() {
+ if (sock >= 0)
+ close(sock);
+ }
+
+ bool SendCmd(const char* cmd) {
+ if (!bConnected)
+ return false;
+
+ cString szCmd = cString::sprintf("%s\r\n", cmd);
+ Send(*szCmd);
+ bool cmdret = (Receive() == CMDSUCCESS);
+
+ szCmd = cString::sprintf("QUIT\r\n");
+ Send(szCmd);
+
+ long rc = 0;
+ if ((rc = Receive()) != SVDRPDISCONNECT)
+ LogFile.eSysLog("could not disconnect (%ld)!", rc);
+
+ close(sock);
+ sock = -1;
+ return cmdret;
+ }
+ bool Send(const char* szSend) {
+ int length = strlen(szSend);
+ int sent = 0;
+ do {
+ sent += send(sock, szSend + sent, length - sent, 0);
+ if (sent < 0) {
+ LogFile.eSysLog("error sending command!");
+ return false;
+ }
+ } while (sent < length);
+ return true;
+ }
+ long Receive() {
+ char* csResp = strdup("");
+ char ch;
+ long rc = 0;
+
+ bool bCheckMultiLine = true;
+
+ while (bCheckMultiLine) {
+ while (strlen(csResp) < 2 || strcmp(csResp + strlen(csResp) - 2, "\r\n") != 0) {
+ if (recv(sock, &ch, 1, 0) < 0) {
+ LogFile.eSysLog("EPGSearch: error receiving response!");
+ free(csResp);
+ return -1;
+ }
+ char* Temp = NULL;
+ msprintf(&Temp, "%s%c", csResp, ch);
+ free(csResp);
+ csResp = Temp;
+ }
+ if (csResp[3] == ' ') {
+ bCheckMultiLine = false;
+ rc = atol(csResp);
+ }
+ free(csResp);
+ csResp = strdup("");
+ }
+ free(csResp);
+ return rc;
+ }
static const char *SVDRPSendCmd;
};