our current experimental Neutrino branch

git-svn-id: file:///home/bas/coolstream_public_svn/THIRDPARTY/applications/neutrino-experimental@27 e54a6e83-5905-42d5-8d5c-058d10e6a962
This commit is contained in:
mrcolor
2009-12-08 11:05:11 +00:00
commit bc5bd4154e
876 changed files with 193775 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
noinst_LIBRARIES = libtuxbox-connection.a
AM_CPPFLAGS = -fno-rtti -fno-exceptions
libtuxbox_connection_a_SOURCES = basicclient.cpp basicserver.cpp basicsocket.cpp messagetools.cpp

View File

@@ -0,0 +1,164 @@
/*
* $Header: /cvs/tuxbox/apps/misc/libs/libconnection/basicclient.cpp,v 1.17 2004/04/08 07:19:00 thegoodguy Exp $
*
* Basic Client Class - The Tuxbox Project
*
* (C) 2002-2003 by thegoodguy <thegoodguy@berlios.de>
*
* License: GPL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include "basicclient.h"
#include "basicmessage.h"
#include "basicsocket.h"
#include <inttypes.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#define TIMEOUT_SEC 60
#define TIMEOUT_USEC 0
#define MAX_TIMEOUT_SEC 300
#define MAX_TIMEOUT_USEC 0
CBasicClient::CBasicClient()
{
sock_fd = -1;
}
bool CBasicClient::open_connection()
{
close_connection();
struct sockaddr_un servaddr;
int clilen;
memset(&servaddr, 0, sizeof(struct sockaddr_un));
servaddr.sun_family = AF_UNIX;
strcpy(servaddr.sun_path, getSocketName()); // no length check !!!
clilen = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path);
if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
{
printf("[CBasicClient] socket failed.\n");
perror(getSocketName());
sock_fd = -1;
return false;
}
if (connect(sock_fd, (struct sockaddr*) &servaddr, clilen) < 0)
{
printf("[CBasicClient] connect failed.\n");
perror(getSocketName());
close_connection();
return false;
}
return true;
}
void CBasicClient::close_connection()
{
if (sock_fd != -1)
{
close(sock_fd);
sock_fd = -1;
}
}
bool CBasicClient::send_data(const char* data, const size_t size)
{
timeval timeout;
if (sock_fd == -1)
return false;
timeout.tv_sec = TIMEOUT_SEC;
timeout.tv_usec = TIMEOUT_USEC;
if (::send_data(sock_fd, data, size, timeout) == false)
{
printf("[CBasicClient] send failed: %s\n", getSocketName());
close_connection();
return false;
}
return true;
}
bool CBasicClient::send_string(const char* data)
{
uint8_t send_length;
size_t length = strlen(data);
if (length > 255)
{
printf("[CBasicClient] string too long - sending only first 255 characters: %s\n", data);
send_length = 255;
}
else
{
send_length = length;
}
return (send_data((char *)&send_length, sizeof(send_length)) &&
send_data(data, send_length));
}
bool CBasicClient::receive_data(char* data, const size_t size, bool use_max_timeout)
{
timeval timeout;
if (sock_fd == -1)
return false;
if (use_max_timeout)
{
timeout.tv_sec = MAX_TIMEOUT_SEC;
timeout.tv_usec = MAX_TIMEOUT_USEC;
}
else
{
timeout.tv_sec = TIMEOUT_SEC;
timeout.tv_usec = TIMEOUT_USEC;
}
if (::receive_data(sock_fd, data, size, timeout) == false)
{
printf("[CBasicClient] receive failed: %s\n", getSocketName());
close_connection();
return false;
}
return true;
}
bool CBasicClient::send(const unsigned char command, const char* data, const unsigned int size)
{
CBasicMessage::Header msgHead;
msgHead.version = getVersion();
msgHead.cmd = command;
open_connection(); // if the return value is false, the next send_data call will return false, too
if (!send_data((char*)&msgHead, sizeof(msgHead)))
return false;
if (size != 0)
return send_data(data, size);
return true;
}

View File

@@ -0,0 +1,51 @@
/*
* $Header: /cvs/tuxbox/apps/misc/libs/libconnection/basicclient.h,v 1.7 2004/04/08 07:19:00 thegoodguy Exp $
*
* Basic Client Class - The Tuxbox Project
*
* (C) 2002-2003 by thegoodguy <thegoodguy@berlios.de>
*
* License: GPL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifndef __basicclient__
#define __basicclient__
#include <malloc.h>
#include <sys/types.h>
class CBasicClient
{
private:
int sock_fd;
protected:
virtual const unsigned char getVersion () const = 0;
virtual const char * getSocketName() const = 0;
bool open_connection();
bool send_data(const char * data, const size_t size);
bool send_string(const char * data);
bool receive_data(char* data, const size_t size, bool use_max_timeout = false);
bool send(const unsigned char command, const char* data = NULL, const unsigned int size = 0);
void close_connection();
CBasicClient();
};
#endif

View File

@@ -0,0 +1,43 @@
/*
* $Header: /cvs/tuxbox/apps/misc/libs/libconnection/basicmessage.h,v 1.2 2002/10/18 11:56:56 thegoodguy Exp $
*
* types used for clientlib <-> daemon communication - d-box2 linux project
*
* (C) 2002 by thegoodguy <thegoodguy@berlios.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifndef __basicmessage_h__
#define __basicmessage_h__
class CBasicMessage
{
public:
typedef unsigned char t_version;
typedef unsigned char t_cmd;
struct Header
{
t_version version;
t_cmd cmd;
};
};
#endif /* __basicmessage_h__ */

View File

@@ -0,0 +1,172 @@
/*
* $Header: /cvs/tuxbox/apps/misc/libs/libconnection/basicserver.cpp,v 1.11 2004/04/08 08:00:55 thegoodguy Exp $
*
* Basic Server Class Class - The Tuxbox Project
*
* (C) 2002 - 2003 by thegoodguy <thegoodguy@berlios.de>
*
* License: GPL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include "basicserver.h"
#include "basicsocket.h"
#include <cstdio>
#include <cstdlib>
#include <inttypes.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#define RECEIVE_TIMEOUT_IN_SECONDS 60
#define SEND_TIMEOUT_IN_SECONDS 60
bool CBasicServer::receive_data(int fd, void * data, const size_t size)
{
timeval timeout;
timeout.tv_sec = RECEIVE_TIMEOUT_IN_SECONDS;
timeout.tv_usec = 0;
return ::receive_data(fd, data, size, timeout);
}
bool CBasicServer::send_data(int fd, const void * data, const size_t size)
{
timeval timeout;
timeout.tv_sec = SEND_TIMEOUT_IN_SECONDS;
timeout.tv_usec = 0;
return ::send_data(fd, data, size, timeout);
}
void CBasicServer::delete_string(char * data)
{
free((void *)data);
}
char * CBasicServer::receive_string(int fd)
{
uint8_t length;
if (receive_data(fd, &length, sizeof(length)))
{
char * data = (char *)malloc(((size_t)length) + 1);
if (receive_data(fd, data, length))
{
data[length] = 0; /* add terminating 0 */
return data;
}
else
{
delete_string(data);
return NULL;
}
}
return NULL;
}
bool CBasicServer::prepare(const char* socketname)
{
struct sockaddr_un servaddr;
int clilen;
memset(&servaddr, 0, sizeof(struct sockaddr_un));
servaddr.sun_family = AF_UNIX;
strcpy(servaddr.sun_path, socketname);
clilen = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path);
unlink(socketname);
if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
{
printf("[CBasicServer] socket failed.\n");
perror(socketname);
return false;
}
if (bind(sock_fd, (struct sockaddr*) &servaddr, clilen) < 0)
{
printf("[CBasicServer] bind failed.\n");
perror(socketname);
return false;
}
#define N_connection_requests_queued 5
if (listen(sock_fd, N_connection_requests_queued) != 0)
{
printf("[CBasicServer] listen failed.\n");
perror(socketname);
return false;
}
name = socketname;
return true;
}
bool CBasicServer::parse(bool (parse_command)(CBasicMessage::Header &rmsg, int connfd), const CBasicMessage::t_version version)
{
int conn_fd;
struct sockaddr_un servaddr;
int clilen = sizeof(servaddr);
bool parse_another_command = true;
CBasicMessage::Header rmsg;
conn_fd = accept(sock_fd, (struct sockaddr*) &servaddr, (socklen_t*) &clilen);
memset(&rmsg, 0, sizeof(rmsg));
read(conn_fd, &rmsg, sizeof(rmsg));
if (rmsg.version == version)
parse_another_command = parse_command(rmsg, conn_fd);
else
printf("[%s] Command ignored: cmd version %d received - server cmd version is %d\n", name.c_str(), rmsg.version, version);
close(conn_fd);
return parse_another_command;
}
bool CBasicServer::run(bool (parse_command)(CBasicMessage::Header &rmsg, int connfd), const CBasicMessage::t_version version, bool non_blocking)
{
if (non_blocking) {
struct pollfd pfd;
pfd.fd = sock_fd;
pfd.events = (POLLIN | POLLPRI);
if (poll(&pfd, 1, 0) > 0)
return parse(parse_command, version);
else
return true;
}
else {
while(parse(parse_command, version));
stop();
return false;
}
}
void CBasicServer::stop(void)
{
close(sock_fd);
unlink(name.c_str());
}

View File

@@ -0,0 +1,60 @@
#ifndef __basicserver__
#define __basicserver__
/*
* $Header: /cvs/tuxbox/apps/misc/libs/libconnection/basicserver.h,v 1.7 2004/04/08 07:19:00 thegoodguy Exp $
*
* Basic Server Class Class - The Tuxbox Project
*
* (C) 2002 - 2003 by thegoodguy <thegoodguy@berlios.de>
*
* License: GPL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <string>
#include "basicmessage.h"
class CBasicServer
{
private:
int sock_fd;
std::string name;
// used by run
bool parse(bool (parse_command)(CBasicMessage::Header &rmsg, int connfd), const CBasicMessage::t_version version);
public:
static bool receive_data (int fd, void * data, const size_t size);
static bool send_data (int fd, const void * data, const size_t size);
static char * receive_string(int fd);
static void delete_string (char * data);
bool prepare(const char* socketname);
// run the server socket
// if set to non-blocking, it will leave the socket open but
// will return immediately without parsing a command if no data
// is sent by a client
bool run(bool (parse_command)(CBasicMessage::Header &rmsg, int connfd), const CBasicMessage::t_version version, bool non_blocking = false);
// manual stop, can and should only be used in non-blocking mode
void stop(void);
};
#endif

View File

@@ -0,0 +1,140 @@
/*
* $Header: /cvs/tuxbox/apps/misc/libs/libconnection/basicsocket.cpp,v 1.2 2003/02/24 21:14:15 thegoodguy Exp $
*
* Basic Socket Class - The Tuxbox Project
*
* (C) 2003 by thegoodguy <thegoodguy@berlios.de>
*
* License: GPL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include "basicsocket.h"
#include <errno.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
bool send_data(int fd, const void * data, const size_t size, const timeval timeout)
{
fd_set writefds;
timeval tv;
const void * buffer;
size_t n;
int rc;
n = size;
while (n > 0)
{
buffer = (void *)((char *)data + (size - n));
rc = ::send(fd, buffer, n, MSG_DONTWAIT | MSG_NOSIGNAL);
if (rc == -1)
{
perror("[basicsocket] send_data");
char * buf = (char *) data;
printf("send_data: errno %d data %X\n", errno, buf[0]);
//if (errno == EPIPE)
if (errno == EPIPE || errno == ESPIPE)
return false;
FD_ZERO(&writefds);
FD_SET(fd, &writefds);
tv = timeout;
rc = select(fd + 1, NULL, &writefds, NULL, &tv);
if (rc == 0)
{
printf("[basicsocket] send timed out.\n");
return false;
}
if (rc == -1)
{
perror("[basicsocket] send_data select");
return false;
}
}
else
n -= rc;
}
return true;
}
bool receive_data(int fd, void * data, const size_t size, const timeval timeout)
{
fd_set readfds;
timeval tv;
void * buffer;
size_t n;
int rc;
n = size;
while (n > 0)
{
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
tv = timeout;
rc = select(fd + 1, &readfds, NULL, NULL, &tv);
if (rc == 0)
{
printf("[basicsocket] receive timed out.\n");
return false;
}
if (rc == -1)
{
perror("[basicsocket] receive_data select");
return false;
}
buffer = (void *)((char *)data + (size - n));
rc = ::recv(fd, buffer, n, MSG_DONTWAIT | MSG_NOSIGNAL);
if ((rc == 0) || (rc == -1))
{
if (rc == -1)
{
perror("[basicsocket] receive_data");
//if (errno == EPIPE)
if (errno == EPIPE || errno == ESPIPE)
return false;
}
else
{
/*
* silently return false
*
* printf("[basicsocket] no more data\n");
*/
return false;
}
}
else
n -= rc;
}
return true;
}

View File

@@ -0,0 +1,35 @@
/*
* $Header: /cvs/tuxbox/apps/misc/libs/libconnection/basicsocket.h,v 1.2 2003/02/24 21:14:15 thegoodguy Exp $
*
* Basic Socket Class - The Tuxbox Project
*
* (C) 2003 by thegoodguy <thegoodguy@berlios.de>
*
* License: GPL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifndef __basicsocket__
#define __basicsocket__
#include <sys/time.h>
#include <unistd.h>
bool send_data(int fd, const void * data, const size_t size, const timeval timeout);
bool receive_data(int fd, void * data, const size_t size, const timeval timeout);
#endif

View File

@@ -0,0 +1,96 @@
/*
* $Header: /cvs/tuxbox/apps/misc/libs/libconnection/messagetools.cpp,v 1.1 2002/12/07 13:37:06 thegoodguy Exp $
*
* tools used in clientlib <-> daemon communication - d-box2 linux project
*
* (C) 2002 by thegoodguy <thegoodguy@berlios.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include "messagetools.h"
size_t write_length_field (unsigned char * buffer, unsigned int length)
{
if (length < 128)
{
buffer[0] = length;
return 1;
}
else
{
unsigned int pos = 0;
unsigned int shiftby = 8;
unsigned char length_field_size = 1;
while ((length >> shiftby) != 0)
{
length_field_size++;
shiftby += 8;
}
buffer[pos++] = ((1 << 7) | length_field_size);
while (shiftby != 0)
{
shiftby -= 8;
buffer[pos++] = length >> shiftby;
}
return pos;
}
}
unsigned int parse_length_field (const unsigned char * buffer)
{
unsigned char size_indicator = (buffer[0] >> 7) & 0x01;
unsigned int length_value = 0;
if (size_indicator == 0)
{
length_value = buffer[0] & 0x7F;
}
else if (size_indicator == 1)
{
unsigned char length_field_size = buffer[0] & 0x7F;
unsigned int i;
for (i = 0; i < length_field_size; i++)
{
length_value = (length_value << 8) | buffer[i + 1];
}
}
return length_value;
}
size_t get_length_field_size (const unsigned int length)
{
if (length < 0x80)
return 0x01;
if (length < 0x100)
return 0x02;
if (length < 0x10000)
return 0x03;
if (length < 0x1000000)
return 0x04;
return 0x05;
}

View File

@@ -0,0 +1,34 @@
/*
* $Header: /cvs/tuxbox/apps/misc/libs/libconnection/messagetools.h,v 1.1 2002/12/07 13:37:06 thegoodguy Exp $
*
* tools used in clientlib <-> daemon communication - d-box2 linux project
*
* (C) 2002 by thegoodguy <thegoodguy@berlios.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifndef __messagetools_h__
#define __messagetools_h__
#include <malloc.h>
size_t write_length_field (unsigned char * buffer, const unsigned int length);
size_t get_length_field_size (const unsigned int length);
unsigned int parse_length_field (const unsigned char * buffer);
#endif /* __messagetools_h__ */