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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
/*
* $Id: server.cpp 9810 2009-05-19 01:43:22Z gena01 $
*
* myYahoo Miranda Plugin
*
* Authors: Gennady Feldman (aka Gena01)
* Laurent Marechal (aka Peorth)
*
* This code is under GPL and is based on AIM, MSN and Miranda source code.
* I want to thank Robert Rainwater and George Hazan for their code and support
* and for answering some of my questions during development of this plugin.
*/
#include "yahoo.h"
#include <time.h>
int PASCAL send(SOCKET s, const char FAR *buf, int len, int flags)
{
int rlen = Netlib_Send((HANDLE)s, buf, len, 0);
#ifdef HTTP_GATEWAY
if (iHTTPGateway)
lLastSend = time(NULL);
#endif
if (rlen == SOCKET_ERROR) {
LOG(("SEND Error."));
return -1;
}
return len;
}
int PASCAL recv(SOCKET s, char FAR *buf, int len, int flags)
{
int RecvResult = Netlib_Recv((HANDLE)s, buf, len, (len == 1) ? MSG_NODUMP : 0);
if (RecvResult == 0) {
LOG(("[recv] Connection closed gracefully."));
return 0;
}
if (RecvResult == SOCKET_ERROR) {
LOG(("[recv] Connection abortively closed"));
return -1;
}
return RecvResult;
}
void __cdecl CYahooProto::server_main(void *empty)
{
enum yahoo_status status = (enum yahoo_status) (int)empty;
time_t lLastPing, lLastKeepAlive, t;
YList *l;
NETLIBSELECTEX nls = { 0 };
int recvResult, ridx = 0, widx = 0, i;
debugLogA("Server Thread Starting status: %d", status);
do_yahoo_debug = YAHOO_LOG_DEBUG;
yahoo_set_log_level((yahoo_log_level)do_yahoo_debug);
poll_loop = 1; /* set this so we start looping */
ext_login(status);
lLastKeepAlive = lLastPing = time(NULL);
while (poll_loop) {
nls.cbSize = sizeof(nls);
nls.dwTimeout = 1000; // 1000 millis = 1 sec
FD_ZERO(&nls.hReadStatus);
FD_ZERO(&nls.hWriteStatus);
FD_ZERO(&nls.hExceptStatus);
nls.hExceptConns[0] = NULL;
ridx = 0; widx = 0;
for (l = m_connections; l;) {
struct _conn *c = (_conn *)l->data;
//LOG(("Connection tag:%d id:%d fd:%d remove:%d", c->tag, c->id, c->fd, c->remove));
if (c->remove) {
YList *n = y_list_next(l);
//LOG(("Removing id:%d fd:%d tag:%d", c->id, c->fd, c->tag));
m_connections = y_list_remove_link(m_connections, l);
y_list_free_1(l);
FREE(c);
l = n;
}
else {
if (c->cond & YAHOO_INPUT_READ) {
//LOG(("[YAHOO_INPUT_READ] Waiting on read. Tag: %d fd: %d", c->tag, c->fd ));
nls.hReadConns[ridx++] = (HANDLE)c->fd;
}
if (c->cond & YAHOO_INPUT_WRITE) {
//LOG(("[YAHOO_INPUT_WRITE] Waiting on write. Tag: %d fd: %d", c->tag, c->fd ));
nls.hWriteConns[widx++] = (HANDLE)c->fd;
}
l = y_list_next(l);
}
}
//debugLogA("[Yahoo_Server] ridx:%d widx:%d", ridx, widx);
nls.hReadConns[ridx] = NULL;
nls.hWriteConns[widx] = NULL;
if (m_connections == NULL) {
debugLogA("Last connection closed.");
break;
}
recvResult = CallService(MS_NETLIB_SELECTEX, (WPARAM)0, (LPARAM)&nls);
/* do the timer check */
if (m_id > 0) {
#ifdef HTTP_GATEWAY
//debugLogA("HTTPGateway: %d", iHTTPGateway);
if (!iHTTPGateway) {
#endif
t = time(NULL);
if (m_bLoggedIn && t - lLastKeepAlive >= 60) {
LOG(("[TIMER] Sending a keep alive message"));
yahoo_keepalive(m_id);
lLastKeepAlive = t;
}
if (m_bLoggedIn && t - lLastPing >= 3600) {
LOG(("[TIMER] Sending ping"));
yahoo_send_ping(m_id);
lLastPing = t;
}
#ifdef HTTP_GATEWAY
} else {
debugLogA("[SERVER] Got packets: %d", ylad->rpkts);
if ( m_bLoggedIn && ( (ylad->rpkts > 0 && (time(NULL) - lLastSend) >=3) ||
( (time(NULL) - lLastSend) >= 13)) ) {
LOG(("[TIMER] Sending an idle message..."));
yahoo_send_idle_packet(m_id);
}
//
// need to sleep, cause netlibselectex is too fast?
//
SleepEx(500, TRUE);
}
#endif
}
/* do the timer check ends */
for (l = m_connections; l; l = y_list_next(l)) {
struct _conn *c = (_conn *)l->data;
if (c->remove)
continue;
/* are we waiting for a Read request? */
if (c->cond & YAHOO_INPUT_READ) {
for (i = 0; i < ridx; i++) {
if ((HANDLE)c->fd == nls.hReadConns[i]) {
if (nls.hReadStatus[i]) {
//LOG(("[YAHOO_INPUT_READ] Read Ready. Tag: %d fd: %d", c->tag, c->fd ));
yahoo_callback(c, YAHOO_INPUT_READ);
}
}//if c->fd=
}//for i = 0
}
/* are we waiting for a Write request? */
if (c->cond & YAHOO_INPUT_WRITE) {
for (i = 0; i < widx; i++) {
if ((HANDLE)c->fd == nls.hWriteConns[i]) {
if (nls.hWriteStatus[i]) {
//LOG(("[YAHOO_INPUT_WRITE] Write ready. Tag: %d fd: %d", c->tag, c->fd ));
yahoo_callback(c, YAHOO_INPUT_WRITE);
}
} // if c->fd == nls
}// for i = 0
}
}// for l=connections
/* Check for Miranda Exit Status */
if (Miranda_Terminated()) {
debugLogA("Miranda Exiting... stopping the loop.");
break;
}
}
debugLogA("Exited loop");
/* cleanup the data stuff and close our connection handles */
while (m_connections) {
YList *tmp = m_connections;
struct _conn * c = (_conn *)m_connections->data;
Netlib_CloseHandle((HANDLE)c->fd);
FREE(c);
m_connections = y_list_remove_link(m_connections, m_connections);
y_list_free_1(tmp);
}
yahoo_close(m_id);
m_bLoggedIn = FALSE;
m_status = YAHOO_STATUS_OFFLINE;
m_id = 0;
/* now set ourselves to offline */
BroadcastStatus(ID_STATUS_OFFLINE);
logoff_buddies();
debugLogA("Server thread ending");
}
|