2012-07-09 10:57:32 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <vector>
|
|
|
|
|
2012-07-09 12:20:45 +01:00
|
|
|
#include "sddll.hxx"
|
2012-07-09 10:57:32 +01:00
|
|
|
#include "Server.hxx"
|
|
|
|
#include "Receiver.hxx"
|
|
|
|
|
|
|
|
#include <boost/thread.hpp>
|
|
|
|
using namespace std;
|
|
|
|
using namespace sd::remotecontrol;
|
|
|
|
|
|
|
|
Server::Server(char* aEncryptionKey, XSlideShowController aController)
|
|
|
|
: mReceiver()
|
|
|
|
{
|
|
|
|
if ( (mSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
|
|
|
{
|
|
|
|
// Error opening
|
|
|
|
}
|
2012-07-09 11:52:22 +01:00
|
|
|
|
2012-07-09 10:57:32 +01:00
|
|
|
sockaddr_in aAddress;
|
|
|
|
aAddress.sin_family = AF_INET;
|
|
|
|
aAddress.sin_port = htons(PORT);
|
|
|
|
aAddress.sin_addr.s_addr = htonl(INADDR_ANY);
|
2012-07-09 11:52:22 +01:00
|
|
|
|
2012-07-09 10:57:32 +01:00
|
|
|
if ( bind( mSocket, (struct sockaddr*) &aAddress, sizeof(aAddress) ) == -1 )
|
|
|
|
{
|
|
|
|
// Error binding
|
|
|
|
}
|
2012-07-09 11:52:22 +01:00
|
|
|
|
2012-07-09 10:57:32 +01:00
|
|
|
if ( listen ( mSocket, 3 ) == -1 )
|
|
|
|
{
|
|
|
|
// Error listening
|
|
|
|
}
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
mNewSocket = accept( mSocket, (struct sockaddr*) &aAddress, (socklen_t*) &aAddrLen );
|
2012-07-09 11:52:22 +01:00
|
|
|
boost::thread t( boost::bind(&Server::listenThread, this ))
|
2012-07-09 10:57:32 +01:00
|
|
|
}
|
|
|
|
// TODO: pass mNewSocket to the thread.
|
|
|
|
}
|
|
|
|
|
|
|
|
Server::~Server()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run as a thread
|
|
|
|
void Server::listenThread()
|
|
|
|
{
|
|
|
|
char aTemp;
|
|
|
|
vector<char> aBuffer();
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int aLength;
|
|
|
|
while ( recv( mSocket, *aTemp, 1, 0) && aTemp != 0x0d ) // look for newline
|
|
|
|
{
|
|
|
|
aBuffer.push_back( aTemp );
|
|
|
|
// TODO: decryption
|
|
|
|
}
|
|
|
|
OUString aLengthString = OUString( aBuffer.front(), aBuffer.size(), RTL_TEXTENCODING_UNICODE );
|
|
|
|
const sal_Char* aLengthChar = aLengthString.convertToString( *aCLength, RTL_TEXTENCODING_ASCII_US, 0).getStr();
|
2012-07-09 11:52:22 +01:00
|
|
|
|
2012-07-09 10:57:32 +01:00
|
|
|
sal_uInt32 aLen = strtol( aLengthChar, NULL, 10);
|
2012-07-09 11:52:22 +01:00
|
|
|
|
2012-07-09 10:57:32 +01:00
|
|
|
char *aMessage = new char[aLen];
|
|
|
|
recv( mSocket, *aMessage, aLen, 0);
|
|
|
|
// Parse.
|
|
|
|
mReceiver.parseCommand( aCommand );
|
2012-07-09 11:52:22 +01:00
|
|
|
|
2012-07-09 10:57:32 +01:00
|
|
|
delete [] aMessage;
|
|
|
|
// TODO: deal with transmision errors gracefully.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// void Server::clientConnected()
|
|
|
|
// {
|
|
|
|
// our_mServerList.insert( our_mServerList.end, new Server() );
|
|
|
|
// thread aThread = Boost::thread( listen );
|
2012-07-09 11:52:22 +01:00
|
|
|
//
|
2012-07-09 10:57:32 +01:00
|
|
|
// }
|
2012-07-09 11:52:22 +01:00
|
|
|
//
|
2012-07-09 10:57:32 +01:00
|
|
|
// void Server::clientDisconnected()
|
|
|
|
// {
|
|
|
|
// our_mServerList::iterator aBegin = our_mServerList.begin;
|
|
|
|
// while ( *aBegin != this ) {
|
|
|
|
// aBegin++;
|
|
|
|
// }
|
|
|
|
// if ( *aBegin == this )
|
|
|
|
// {
|
|
|
|
// our_mServerList.erase( this );
|
|
|
|
// delete this;
|
|
|
|
// }
|
2012-07-09 11:52:22 +01:00
|
|
|
// }
|
2012-07-09 12:20:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
void SdDLL::RegisterRemotes()
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Register our remote control goodness\n" );
|
|
|
|
// FIXME: create a thread
|
|
|
|
// FIXME: convert the above socket code to use sal/inc/osl/socket.hxx etc.
|
|
|
|
}
|