| 1 | #pragma once |
|---|
| 2 | #include "resource.h" |
|---|
| 3 | #include <Thread.h> |
|---|
| 4 | #include <Mutex.h> |
|---|
| 5 | #include <WinSock2.h> |
|---|
| 6 | /** |
|---|
| 7 | * @ingroup NSClient++ |
|---|
| 8 | * Socket responder class. |
|---|
| 9 | * This is a background thread that listens to the socket and executes incoming commands. |
|---|
| 10 | * |
|---|
| 11 | * @version 1.0 |
|---|
| 12 | * first version |
|---|
| 13 | * |
|---|
| 14 | * @date 02-12-2005 |
|---|
| 15 | * |
|---|
| 16 | * @author mickem |
|---|
| 17 | * |
|---|
| 18 | * @par license |
|---|
| 19 | * This code is absolutely free to use and modify. The code is provided "as is" with |
|---|
| 20 | * no expressed or implied warranty. The author accepts no liability if it causes |
|---|
| 21 | * any damage to your computer, causes your pet to fall ill, increases baldness |
|---|
| 22 | * or makes your car start emitting strange noises when you start it up. |
|---|
| 23 | * This code has no bugs, just undocumented features! |
|---|
| 24 | * |
|---|
| 25 | * @todo This is not very well written and should probably be reworked. |
|---|
| 26 | * |
|---|
| 27 | * @bug |
|---|
| 28 | * |
|---|
| 29 | */ |
|---|
| 30 | class SimpleSocketListsner { |
|---|
| 31 | private: |
|---|
| 32 | MutexHandler mutexHandler; |
|---|
| 33 | SOCKET server; |
|---|
| 34 | HANDLE hStopEvent; |
|---|
| 35 | u_short port_; |
|---|
| 36 | |
|---|
| 37 | public: |
|---|
| 38 | SimpleSocketListsner(u_short port) : port_(port), hStopEvent(NULL) {}; |
|---|
| 39 | virtual ~SimpleSocketListsner(); |
|---|
| 40 | DWORD threadProc(LPVOID lpParameter); |
|---|
| 41 | void exitThread(void); |
|---|
| 42 | #define RECV_BUFFER_LEN 1024 |
|---|
| 43 | typedef std::pair<char*,unsigned int> readAllDataBlock; |
|---|
| 44 | static readAllDataBlock readAll(SOCKET socket) { |
|---|
| 45 | // @todo Is this even working ? |
|---|
| 46 | // @todo Nedds *alot* more work... |
|---|
| 47 | unsigned int buffLen = RECV_BUFFER_LEN; |
|---|
| 48 | char *retBuf = NULL; |
|---|
| 49 | char *buff = new char[buffLen]; |
|---|
| 50 | int n=recv(socket,buff,RECV_BUFFER_LEN,0); |
|---|
| 51 | while ((n==SOCKET_ERROR )||(n==0)) { |
|---|
| 52 | if (n == buffLen) { |
|---|
| 53 | char* newBuf = new char[buffLen+RECV_BUFFER_LEN]; |
|---|
| 54 | memcpy(newBuf, buff, buffLen); |
|---|
| 55 | n = recv(socket, buff, RECV_BUFFER_LEN, 0); |
|---|
| 56 | if ((n!=SOCKET_ERROR )&&(n!=0)) { |
|---|
| 57 | memcpy(&newBuf[buffLen], buff, n); |
|---|
| 58 | buffLen += n; |
|---|
| 59 | } |
|---|
| 60 | if (retBuf) |
|---|
| 61 | delete [] retBuf; |
|---|
| 62 | retBuf = newBuf; |
|---|
| 63 | } else { |
|---|
| 64 | buffLen = n; |
|---|
| 65 | break; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | if (retBuf) { |
|---|
| 69 | delete [] buff; |
|---|
| 70 | return readAllDataBlock(retBuf,buffLen); |
|---|
| 71 | } |
|---|
| 72 | return readAllDataBlock(buff,buffLen); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | private: |
|---|
| 77 | virtual void onAccept(SOCKET client) {} |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | |
|---|