Repository navigation
Expand file tree
/
Copy pathPyConnectObjComm.h
More file actions
116 lines (98 loc) · 3.3 KB
/
Copy pathPyConnectObjComm.h
File metadata and controls
116 lines (98 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* PyConnectObjComm.h
*
* Copyright 2006, 2007 Xun Wang.
* This file is part of PyConnect.
*
* PyConnect 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 3 of the License, or
* (at your option) any later version.
*
* PyConnect 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PyConnectObjComm_h_DEFINED
#define PyConnectObjComm_h_DEFINED
#ifdef OPENR_OBJECT
#include <OPENR/OObject.h>
#include <OPENR/OObserver.h>
#include <OPENR/OSubject.h>
#endif
#ifdef WIN32
#include <winsock2.h>
#define SOCKET_T SOCKET
#else
#define SOCKET_T int
#define INVALID_SOCKET -1
#include <netinet/in.h>
#endif
#include "PyConnectCommon.h"
#include <map>
#include <vector>
namespace pyconnect {
class ObjectComm;
typedef enum {
NOT_SUPPORTED = 1,
INVALID_ADDR_PORT = 2,
COMM_FAILED = 4,
COMM_SUCCESS = 8
} CommObjectStat;
typedef enum {
MESG_PROCESSED_OK = 0,
MESG_PROCESSED_FAILED = 1,
MESG_TO_SHUTDOWN = 2
} MesgProcessResult;
class MessageProcessor {
public:
MessageProcessor() {}
virtual ~MessageProcessor();
virtual MesgProcessResult processInput(unsigned char *recData,
int bytesReceived,
struct sockaddr_in &cAddr,
bool skipdecrypt = false) = 0;
void addCommObject(ObjectComm *pCommObj);
virtual void updateMPID(int id) {}
protected:
typedef std::vector<ObjectComm *> CommObjectList;
CommObjectList commObjList_;
void dispatchMessage(const unsigned char *data, int size,
bool broadcast = false);
CommObjectStat connectTo(char *host, int port = 0);
CommObjectStat setBroadcastAddr(char *addr);
};
class ObjectComm {
public:
ObjectComm();
virtual ~ObjectComm() {}
virtual void dataPacketSender(const unsigned char *data, int size,
bool broadcast = false) = 0;
virtual CommObjectStat setBroadcastAddr(char *addr) { return NOT_SUPPORTED; }
virtual CommObjectStat createTalker(char *host, int port = 0) {
return NOT_SUPPORTED;
}
virtual void fini() {}
protected:
MessageProcessor *pMP_;
void setMP(MessageProcessor *pMP) { pMP_ = pMP; }
int verifyNegotiationMsg(const unsigned char *recBuffer, int receivedBytes);
SOCKET_T findOrAddCommChanByMsgID(const unsigned char *data);
SOCKET_T getLastUsedCommChannel();
void resetLastUsedCommChannel() { activeCommChannel_ = INVALID_SOCKET; }
void setLastUsedCommChannel(SOCKET_T index);
void resetObjCommChanMap();
void objCommChannelShutdown(SOCKET_T chanID, bool notifyMesgProc = false);
private:
typedef std::map<int, SOCKET_T>
ObjectCommChannelMap; // < remote host object (module or server) ID ,
// channel index/socket >
ObjectCommChannelMap objCommMap_;
SOCKET_T activeCommChannel_;
};
} // namespace pyconnect
#endif // PyConnectObjComm_h_DEFINED