/
TclSocket.h
110 lines (81 loc) · 3.01 KB
/
TclSocket.h
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
/*******************************************************************************
* shroudBNC - an object-oriented framework for IRC *
* Copyright (C) 2005-2014 Gunnar Beutner *
* *
* This program 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. *
* *
* This program 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, write to the Free Software *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
*******************************************************************************/
class CCore;
class CConnection;
class CTclSocket;
extern CHashtable<CTclSocket*, false>* g_TclListeners;
extern int g_SocketIdx;
extern Tcl_Interp* g_Interp;
IMPL_SOCKETLISTENER(CTclSocket) {
private:
int m_Idx;
bool m_SSL;
char *m_TclProc;
public:
CTclSocket(unsigned int Port, const char *BindIp, const char *TclProc, bool SSL) : CListenerBase<CTclSocket>(Port, BindIp) {
char *Buf;
m_TclProc = strdup(TclProc);
int rc = asprintf(&Buf, "%d", g_SocketIdx);
if (RcFailed(rc)) {
g_Bouncer->Fatal();
}
m_Idx = g_SocketIdx;
g_SocketIdx++;
m_SSL = SSL;
g_TclListeners->Add(Buf, this);
free(Buf);
}
~CTclSocket(void) {
char *Buf;
free(m_TclProc);
int rc = asprintf(&Buf, "%d", m_Idx);
if (RcFailed(rc)) {
g_Bouncer->Fatal();
}
g_TclListeners->Remove(Buf);
free(Buf);
}
virtual const char *GetClassName(void) const {
return "CTclSocket";
}
virtual int GetIdx(void) {
return m_Idx;
}
virtual void Accept(SOCKET Client, const sockaddr *PeerAddress) {
char *ptr;
Tcl_Obj* objv[2];
CTclClientSocket *TclClient;
TclClient = new CTclClientSocket(Client, m_SSL, Role_Server);
int rc = asprintf(&ptr, "%d", TclClient->GetIdx());
if (RcFailed(rc)) {
g_Bouncer->Fatal();
}
objv[0] = Tcl_NewStringObj(m_TclProc, strlen(m_TclProc));
Tcl_IncrRefCount(objv[0]);
objv[1] = Tcl_NewStringObj(ptr, strlen(ptr));
Tcl_IncrRefCount(objv[1]);
free(ptr);
Tcl_EvalObjv(g_Interp, 2, objv, TCL_EVAL_GLOBAL);
Tcl_DecrRefCount(objv[1]);
Tcl_DecrRefCount(objv[0]);
if (TclClient->GetControlProc() == NULL) {
delete TclClient;
}
}
};