From 9b456a150fc2b535bbe0abc534a2f813d07e2f7e Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Tue, 18 Jul 2006 17:30:12 +0000 Subject: [PATCH] UDP server for inserting remote button presses --- command.cc | 9 ++ command.h | 3 + dsock.cc | 10 +-- dsock.h | 4 +- message.h | 1 + objects.mk | 2 +- udp.cc | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++++ udp.h | 60 +++++++++++++ 8 files changed, 332 insertions(+), 13 deletions(-) create mode 100755 udp.cc create mode 100755 udp.h diff --git a/command.cc b/command.cc index 540a652..08750ce 100644 --- a/command.cc +++ b/command.cc @@ -78,6 +78,7 @@ int Command::shutdown() void Command::stop() { // VDR::getInstance()->cancelFindingServer(); + udp.shutdown(); irun = 0; } @@ -135,6 +136,9 @@ void Command::run() viewman->add(vconnect); vconnect->run(); + // Start method 2 of getting commands in... + udp.run(this); + UCHAR button = 0; while(irun) { @@ -281,6 +285,11 @@ void Command::processMessage(Message* m) doFromTheTop(true); break; } + case Message::UDP_BUTTON: + { + handleCommand(m->parameter); + break; + } } } diff --git a/command.h b/command.h index 6b35c3b..c38272c 100644 --- a/command.h +++ b/command.h @@ -58,6 +58,7 @@ #include "i18n.h" #include "timerreceiver.h" #include "timers.h" +#include "udp.h" class VConnect; @@ -105,6 +106,8 @@ class Command : public MessageQueue VWallpaper* wallpaper; VInfo* connLost; + UDP udp; + void processMessage(Message* m); }; diff --git a/dsock.cc b/dsock.cc index 36e6a8e..af6cf1e 100644 --- a/dsock.cc +++ b/dsock.cc @@ -20,11 +20,9 @@ #include "dsock.h" -ULONG DatagramSocket::iterate_ip = 0; - DatagramSocket::DatagramSocket(short port) { - theInstance = this; + iterate_ip = 0; myPort = port; addrlen = sizeof(struct sockaddr); initted = false; @@ -35,12 +33,6 @@ DatagramSocket::~DatagramSocket() if (initted) shutdown(); } -DatagramSocket* DatagramSocket::theInstance = 0; -DatagramSocket* DatagramSocket::getInstance(void) -{ - return theInstance; -} - int DatagramSocket::init() { if (initted) return 0; diff --git a/dsock.h b/dsock.h index 25545e1..aa8e488 100644 --- a/dsock.h +++ b/dsock.h @@ -49,7 +49,6 @@ class DatagramSocket public: DatagramSocket(short); // port ~DatagramSocket(); - static DatagramSocket* getInstance(void); int init(); void shutdown(); unsigned char waitforMessage(unsigned char); // int =0-block =1-new wait =2-continue wait @@ -62,9 +61,8 @@ class DatagramSocket private: bool initted; ULONG getIPNumber(ULONG num); - static ULONG iterate_ip; + ULONG iterate_ip; const static char DSOCKDEBUG = 0; - static DatagramSocket* theInstance; int socketnum; // Socket descriptor short myPort; // My port number struct sockaddr_in myAddr; // My address diff --git a/message.h b/message.h index c1a9f0e..c0084ce 100644 --- a/message.h +++ b/message.h @@ -63,6 +63,7 @@ class Message const static ULONG CHANGED_OPTIONS = 18; const static ULONG CONNECTION_LOST = 19; const static ULONG MOVE_RECORDING = 20; + const static ULONG UDP_BUTTON = 21; }; #endif diff --git a/objects.mk b/objects.mk index 1fd2051..aed0e6d 100644 --- a/objects.mk +++ b/objects.mk @@ -1,5 +1,5 @@ OBJECTS1 = command.o log.o tcp.o dsock.o thread.o timers.o i18n.o mutex.o \ - message.o messagequeue.o \ + message.o messagequeue.o udp.o \ vdr.o recman.o recording.o channel.o rectimer.o event.o directory.o\ player.o vfeed.o afeed.o \ demuxer.o demuxervdr.o stream.o draintarget.o \ diff --git a/udp.cc b/udp.cc new file mode 100755 index 0000000..08a9d7b --- /dev/null +++ b/udp.cc @@ -0,0 +1,256 @@ +/* + Copyright 2006 Chris Tallon + + This file is part of VOMP. + + VOMP 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. + + VOMP 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 VOMP; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "udp.h" + +//void dump(unsigned char* data, USHORT size); +//unsigned char dcc(UCHAR c); + +UDP::UDP() +{ + log = Log::getInstance(); + initted = 0; +} + +UDP::~UDP() +{ + shutdown(); +} + +int UDP::shutdown() +{ + if (!initted) return 1; + if (threadIsActive()) threadCancel(); + ds->shutdown(); + + initted = 0; + return 1; +} + +int UDP::run(MessageQueue* tcommandMessageQueue) +{ + if (threadIsActive()) return 1; + log->log("UDP", Log::DEBUG, "Starting UDP command server"); + + initted = 1; + + commandMessageQueue = tcommandMessageQueue; + ds = new DatagramSocket(2000); + + if (!ds->init()) + { + log->log("UDP", Log::DEBUG, "DSock init error"); + shutdown(); + return 0; + } + + if (!threadStart()) + { + log->log("UDP", Log::DEBUG, "Thread start error"); + shutdown(); + return 0; + } + + log->log("UDP", Log::DEBUG, "UDP command server started"); + return 1; +} + +void UDP::threadMethod() +{ + int retval; + while(1) + { + log->log("UDP", Log::DEBUG, "Starting wait"); + retval = ds->waitforMessage(0); + log->log("UDP", Log::DEBUG, "Wait finished"); + + if (retval == 0) + { + log->log("UDP", Log::CRIT, "Wait for packet error"); + return; + } + else if (retval == 1) + { + continue; + } + else + { + processRequest((UCHAR*)ds->getData(), ds->getDataLength()); + } + } +} + +void UDP::processRequest(UCHAR* data, int length) +{ + log->log("UDP", Log::DEBUG, "Got request"); + + char* temp = new char[length + 1]; + memcpy(temp, data, length); + temp[length] = '\0'; + int command = atoi(temp); + delete[] temp; + + log->log("UDP", Log::DEBUG, "Command %i recieved", command); + + + Message *m = new Message(); + m->to = commandMessageQueue; + m->message = Message::UDP_BUTTON; + m->parameter = command; + commandMessageQueue->postMessage(m); +} + +/* +void dump(unsigned char* data, USHORT size) +{ + printf("Size = %u\n", size); + + USHORT c = 0; + while(c < size) + { + if ((size - c) > 15) + { + printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7], + data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14], data[c+15], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]), + dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]), dcc(data[c+14]), dcc(data[c+15])); + c += 16; + } + else + { + switch (size - c) + { + case 15: + printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7], + data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]), + dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]), dcc(data[c+14])); + c += 15; + break; + case 14: + printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7], + data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]), + dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13])); + c += 14; + break; + case 13: + printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7], + data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]), + dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12])); + c += 13; + break; + case 12: + printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7], + data[c+8], data[c+9], data[c+10], data[c+11], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]), + dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11])); + c += 12; + break; + case 11: + printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7], + data[c+8], data[c+9], data[c+10], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]), + dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10])); + c += 11; + break; + case 10: + printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7], + data[c+8], data[c+9], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]), + dcc(data[c+8]), dcc(data[c+9])); + c += 10; + break; + case 9: + printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7], + data[c+8], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]), + dcc(data[c+8])); + c += 9; + break; + case 8: + printf(" %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7])); + c += 8; + break; + case 7: + printf(" %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6])); + c += 7; + break; + case 6: + printf(" %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5])); + c += 6; + break; + case 5: + printf(" %02X %02X %02X %02X %02X %c%c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], data[c+4], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4])); + c += 5; + break; + case 4: + printf(" %02X %02X %02X %02X %c%c%c%c\n", + data[c], data[c+1], data[c+2], data[c+3], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3])); + c += 4; + break; + case 3: + printf(" %02X %02X %02X %c%c%c\n", + data[c], data[c+1], data[c+2], + dcc(data[c]), dcc(data[c+1]), dcc(data[c+2])); + c += 3; + break; + case 2: + printf(" %02X %02X %c%c\n", + data[c], data[c+1], + dcc(data[c]), dcc(data[c+1])); + c += 2; + break; + case 1: + printf(" %02X %c\n", + data[c], + dcc(data[c])); + c += 1; + break; + } + } + } +} + +unsigned char dcc(UCHAR c) +{ + if (isspace(c)) return ' '; + if (isprint(c)) return c; + return '.'; +} +*/ diff --git a/udp.h b/udp.h new file mode 100755 index 0000000..9133391 --- /dev/null +++ b/udp.h @@ -0,0 +1,60 @@ +/* + Copyright 2006 Chris Tallon + + This file is part of VOMP. + + VOMP 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. + + VOMP 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 VOMP; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef UDP_H +#define UDP_H + +#include +#include +#include + +#include "defines.h" +#include "log.h" +#include "dsock.h" +#include "messagequeue.h" + +#ifdef WIN32 +#include "threadwin.h" +#else +#include "threadp.h" +#endif + +class UDP : public Thread_TYPE +{ + public: + UDP(); + virtual ~UDP(); + + int run(MessageQueue* commandMessageQueue); + int shutdown(); + + private: + void threadPostStopCleanup() {}; + + void threadMethod(); + void processRequest(UCHAR* data, int length); + + int initted; + DatagramSocket* ds; + MessageQueue* commandMessageQueue; + Log* log; +}; + +#endif -- 2.39.2