source: nscp/include/Socket.h @ c6e008c

0.4.00.4.10.4.2stable
Last change on this file since c6e008c was c6e008c, checked in by Michael Medin <michael@…>, 8 years ago

* empty log message *

  • Property mode set to 100644
File size: 2.0 KB
Line 
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 */
30class SimpleSocketListsner {
31private:
32        MutexHandler mutexHandler;
33        SOCKET server;
34        HANDLE hStopEvent;
35        u_short port_;
36
37public:
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
76private:
77        virtual void onAccept(SOCKET client) {}
78};
79
80
Note: See TracBrowser for help on using the repository browser.