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"
|
|
|
|
|
2012-07-09 15:53:03 +01:00
|
|
|
|
|
|
|
|
2012-07-09 10:57:32 +01:00
|
|
|
using namespace std;
|
2012-07-09 15:53:03 +01:00
|
|
|
using namespace sd;
|
|
|
|
using rtl::OUString;
|
|
|
|
using rtl::OString;
|
2012-07-09 10:57:32 +01:00
|
|
|
|
2012-07-09 15:53:03 +01:00
|
|
|
Server::Server()
|
|
|
|
: Thread( "ServerThread" ), mSocket(), mStreamSocket(), mReceiver()
|
2012-07-09 10:57:32 +01:00
|
|
|
{
|
2012-07-09 11:52:22 +01:00
|
|
|
|
2012-07-09 15:53:03 +01:00
|
|
|
// boost::thread t( boost::bind(&Server::listenThread, this ))
|
2012-07-09 11:52:22 +01:00
|
|
|
|
2012-07-09 10:57:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Server::~Server()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run as a thread
|
|
|
|
void Server::listenThread()
|
|
|
|
{
|
|
|
|
char aTemp;
|
2012-07-09 15:53:03 +01:00
|
|
|
vector<char> aBuffer;
|
2012-07-09 10:57:32 +01:00
|
|
|
while (true)
|
|
|
|
{
|
2012-07-09 15:53:03 +01:00
|
|
|
int aRet;
|
|
|
|
while ( (aRet = mStreamSocket.read( &aTemp, 1)) && aTemp != 0x0d ) // look for newline
|
2012-07-09 10:57:32 +01:00
|
|
|
{
|
|
|
|
aBuffer.push_back( aTemp );
|
|
|
|
// TODO: decryption
|
|
|
|
}
|
2012-07-09 15:53:03 +01:00
|
|
|
if (aRet != 1) // Error reading or connection closed
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
OUString aLengthString = OUString( &aBuffer.front(), aBuffer.size(), RTL_TEXTENCODING_UNICODE );
|
|
|
|
OString aTempStr;
|
|
|
|
aLengthString.convertToString( &aTempStr, RTL_TEXTENCODING_ASCII_US, 0);
|
|
|
|
const sal_Char* aLengthChar = aTempStr.getStr();
|
2012-07-09 11:52:22 +01:00
|
|
|
|
2012-07-09 15:53:03 +01:00
|
|
|
sal_Int32 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];
|
2012-07-09 15:53:03 +01:00
|
|
|
|
|
|
|
if( mStreamSocket.read( (void*) aMessage, aLen ) != aLen)// Error reading or connection closed
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-07-09 10:57:32 +01:00
|
|
|
// Parse.
|
2012-07-09 15:53:03 +01:00
|
|
|
mReceiver.parseCommand( aMessage, aLen, NULL );
|
2012-07-09 11:52:22 +01:00
|
|
|
|
2012-07-09 10:57:32 +01:00
|
|
|
delete [] aMessage;
|
2012-07-09 15:53:03 +01:00
|
|
|
|
2012-07-09 10:57:32 +01:00
|
|
|
// TODO: deal with transmision errors gracefully.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-09 15:53:03 +01:00
|
|
|
void Server::execute()
|
|
|
|
{
|
|
|
|
|
|
|
|
osl::SocketAddr aAddr( "", PORT );
|
|
|
|
if ( !mSocket.bind( aAddr ) )
|
|
|
|
{
|
|
|
|
// Error binding
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !mSocket.listen() )
|
|
|
|
{
|
|
|
|
// Error listening
|
|
|
|
}
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
mSocket.acceptConnection( mStreamSocket );
|
|
|
|
listenThread();
|
|
|
|
}
|
2012-07-09 12:20:45 +01:00
|
|
|
|
2012-07-09 15:53:03 +01:00
|
|
|
}
|
2012-07-09 12:20:45 +01:00
|
|
|
|
|
|
|
void SdDLL::RegisterRemotes()
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Register our remote control goodness\n" );
|
2012-07-09 15:53:03 +01:00
|
|
|
Server server;
|
|
|
|
server.launch();
|
2012-07-09 12:20:45 +01:00
|
|
|
// FIXME: create a thread
|
|
|
|
}
|