summaryrefslogtreecommitdiff
path: root/Ivy/ThreadedSocket.cxx
blob: ee1e03bb6ce4b28911154b30c92dc112ac22ac15 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// ThreadedSocket.cpp: implementation of the CThreadedSocket class.
//
//////////////////////////////////////////////////////////////////////

#include "IvyStdAfx.h"


#include "ThreadedSocket.h"



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CThreadedSocket::CThreadedSocket()
{
 
	m_hSocket	= INVALID_SOCKET;
	h_reader	= NULL;
	h_writer	= NULL;
	reader_id	= 0;
	writer_id	= 0;
	listen_mode = false;
	connect_pending = true;
	send_pending = false;
}

CThreadedSocket::~CThreadedSocket()
{
	if (m_hSocket != INVALID_SOCKET)
		Close();
}
int CThreadedSocket::Create(UINT nSocketPort, int nSocketType, const char * lpszSocketAddress)
{
	if ( Socket(nSocketType) < 0 )
		return SOCKET_ERROR;

	send_count = CreateSemaphore( NULL, 0, 100, NULL);  // unnamed semaphore
	if (send_count == NULL) 
		return SOCKET_ERROR;

	return Bind(nSocketPort,lpszSocketAddress);
}

/////////////////////////////////////////////////////////////////////////////
// CThreadedSocket Attributes



void CThreadedSocket::GetPeerName(ivy::string & rPeerAddress, UINT& rPeerPort)
{
	SOCKADDR_IN sockAddr;
	memset(&sockAddr, 0, sizeof(sockAddr));

	int nSockAddrLen = sizeof(sockAddr);
	GetPeerName((SOCKADDR*)&sockAddr, &nSockAddrLen);
	rPeerPort = ntohs(sockAddr.sin_port);
	rPeerAddress = inet_ntoa(sockAddr.sin_addr);
}

void CThreadedSocket::GetSockName(ivy::string & rSocketAddress, UINT& rSocketPort)
{
	SOCKADDR_IN sockAddr;
	memset(&sockAddr, 0, sizeof(sockAddr));

	int nSockAddrLen = sizeof(sockAddr);
	GetSockName((SOCKADDR*)&sockAddr, &nSockAddrLen);
	rSocketPort = ntohs(sockAddr.sin_port);
	rSocketAddress = inet_ntoa(sockAddr.sin_addr);
}

/////////////////////////////////////////////////////////////////////////////
// CAscynSocket Operations

SOCKET CThreadedSocket::Accept(CThreadedSocket& rConnectedSocket,
	SOCKADDR* lpSockAddr, int* lpSockAddrLen)
{
	ASSERT(rConnectedSocket.m_hSocket == INVALID_SOCKET);

	SOCKET hTemp = accept(m_hSocket, lpSockAddr, lpSockAddrLen);

	if (hTemp == INVALID_SOCKET)
	{
		rConnectedSocket.m_hSocket = INVALID_SOCKET;
		return SOCKET_ERROR;
	}
	else 
	{
		rConnectedSocket.m_hSocket = hTemp;
		rConnectedSocket.send_count = CreateSemaphore( NULL, 0, 100, NULL);  // unnamed semaphore
		if (rConnectedSocket.send_count == NULL) 
			return SOCKET_ERROR;

		rConnectedSocket.StartListener();
		return hTemp;
	}
}

int CThreadedSocket::Bind(UINT nSocketPort, const char *  lpszSocketAddress)
{

	SOCKADDR_IN sockAddr;
	memset(&sockAddr,0,sizeof(sockAddr));

	sockAddr.sin_family = AF_INET;

	if (lpszSocketAddress == NULL)
		sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	else
	{
		DWORD lResult = inet_addr(lpszSocketAddress);
		if (lResult == INADDR_NONE)
		{
			WSASetLastError(WSAEINVAL);
			return SOCKET_ERROR;
		}
		sockAddr.sin_addr.s_addr = lResult;
	}

	sockAddr.sin_port = htons((u_short)nSocketPort);

	return Bind((SOCKADDR*)&sockAddr, sizeof(sockAddr));
}

void CThreadedSocket::Close()
{
	long PreviousCount = 0;  
	int err;
	DWORD currentThreadId;


	if (m_hSocket != INVALID_SOCKET)
	{
		currentThreadId = GetCurrentThreadId();

		//ASSERT(SOCKET_ERROR != closesocket(m_hSocket));
		SOCKET temp = m_hSocket;		// Thread ACK 
		m_hSocket = INVALID_SOCKET;
		TRACE( "CThreadedSocket::Close (reader=0x%0lx) (writer=0x%0lx)\n", reader_id, writer_id );
		closesocket(temp); // close silently
		
	//if ( thread ) // On fait de l'auto delete mais dans le cas de terminaison anormale l'object reste ????!!!
	//	delete thread;
//	TRACE("CThreadedSocket waiting for thread end ...\n");
	if ( h_reader && currentThreadId != reader_id )
		WaitForSingleObject( h_reader, 5000 );
	// wake up writer
	if ( h_writer && currentThreadId != writer_id ) 
	{
	err = ReleaseSemaphore(send_count, 1,  &PreviousCount);
	WaitForSingleObject( h_writer, 5000 );
	}
//	TRACE("CThreadedSocket all thread ended\n");
	}
}
int CThreadedSocket::Listen(int nConnectionBacklog)
{ 
	int err =   listen(m_hSocket, nConnectionBacklog);
	if ( !err )
		listen_mode = true;
	return StartListener();
}

int CThreadedSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen)
{
	int ok;
	fd_set writefds;

	FD_ZERO(&writefds);

	FD_SET(m_hSocket, &writefds);
	
	ok = connect(m_hSocket, lpSockAddr, nSockAddrLen);
	if ( ok != 0  )
	{
		int err = this->GetLastError();
		if ( err == 0 ) return err;
		TRACE( "***************************************Error connect %d\n", err);
		if ( err != WSAEWOULDBLOCK )
			return -1;
		// Wait for connection to complete
		TRACE( "***************************************Waiting for connection to complete\n");
		FD_SET(m_hSocket, &writefds);
		err = select( 1, NULL, &writefds, NULL,   NULL );
		//TRACE( "Thread socket %d wait return %d\n", m_hSocket, err );
		if ( !err ) 
			connect_pending = true;
		return err;
	}
	connect_pending = true;
	return StartListener();
}

int CThreadedSocket::Connect(const char * lpszHostAddress, UINT nHostPort)
{

	ASSERT(lpszHostAddress != NULL);

	SOCKADDR_IN sockAddr;
	memset(&sockAddr,0,sizeof(sockAddr));

	sockAddr.sin_family = AF_INET;
	sockAddr.sin_addr.s_addr = inet_addr(lpszHostAddress);

	if (sockAddr.sin_addr.s_addr == INADDR_NONE)
	{
		LPHOSTENT lphost;
		lphost = gethostbyname(lpszHostAddress);
		if (lphost != NULL)
			sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
		else
		{
			WSASetLastError(WSAEINVAL);
			return SOCKET_ERROR;
		}
	}

	sockAddr.sin_port = htons((u_short)nHostPort);

	return Connect((SOCKADDR*)&sockAddr, sizeof(sockAddr));
}

size_t CThreadedSocket::Receive(void* lpBuf, size_t nBufLen, int nFlags)
{	
	int lg = recv(m_hSocket, (LPSTR)lpBuf, (int)nBufLen, nFlags);
	return lg;
}

size_t CThreadedSocket::ReceiveFrom(void* lpBuf, size_t nBufLen, ivy::string & rSocketAddress, UINT& rSocketPort, int nFlags)
{
	SOCKADDR_IN sockAddr;

	memset(&sockAddr, 0, sizeof(sockAddr));

	size_t nSockAddrLen = sizeof(sockAddr);
	size_t nResult = ReceiveFrom(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, &nSockAddrLen, nFlags);
	rSocketPort = ntohs(sockAddr.sin_port);
	rSocketAddress = inet_ntoa(sockAddr.sin_addr);

	return nResult;
}

size_t CThreadedSocket::Send(const void* lpBuf, size_t nBufLen, int nFlags)
{
	return  send(m_hSocket, (LPSTR)lpBuf, (int)nBufLen, nFlags);
}

size_t CThreadedSocket::SendTo(const void* lpBuf, size_t nBufLen, UINT nHostPort, const char * lpszHostAddress, int nFlags)
{
	SOCKADDR_IN sockAddr;
	
	memset(&sockAddr,0,sizeof(sockAddr));

	sockAddr.sin_family = AF_INET;

	if (lpszHostAddress == NULL)
		sockAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
	else
	{
		sockAddr.sin_addr.s_addr = inet_addr(lpszHostAddress);
		if (sockAddr.sin_addr.s_addr == INADDR_NONE)
		{
			LPHOSTENT lphost;
			lphost = gethostbyname(lpszHostAddress);
			if (lphost != NULL)
				sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
			else
			{
				WSASetLastError(WSAEINVAL);
				return SOCKET_ERROR;
			}
		}
	}
	
	sockAddr.sin_port = htons((u_short)nHostPort);

	return SendTo(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, sizeof(sockAddr), nFlags);
	}
int CThreadedSocket::AddMember( const char * lpszHostAddress )
{
	int multicast_ttl = 64; // region
	struct ip_mreq imr;
	
	TRACE("CThreadedSocket::AddMember adding %s\n", lpszHostAddress );
	imr.imr_multiaddr.s_addr = INADDR_ANY;
	imr.imr_interface.s_addr = INADDR_ANY; 
	
	if (lpszHostAddress )
	{
		imr.imr_multiaddr.s_addr = inet_addr(lpszHostAddress);
		if (imr.imr_multiaddr.s_addr == INADDR_NONE)
		{
			LPHOSTENT lphost;
			lphost = gethostbyname(lpszHostAddress);
			if (lphost != NULL)
				imr.imr_multiaddr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
			else
			{
				WSASetLastError(WSAEINVAL);
				return SOCKET_ERROR;
			}
		}
	}
	if ( IN_CLASSD( htonl(imr.imr_multiaddr.s_addr) ) )
		{
		SetSockOpt( IP_MULTICAST_TTL, &multicast_ttl, sizeof( multicast_ttl ), IPPROTO_IP );
		SetSockOpt( IP_ADD_MEMBERSHIP, &imr, sizeof( imr ), IPPROTO_IP );
		}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CThreadedSocket Overridable callbacks
void CThreadedSocket::OnReceive(int /*nErrorCode*/)
{
}

void CThreadedSocket::OnSend(int /*nErrorCode*/)
{
}

void CThreadedSocket::OnOutOfBandData(int /*nErrorCode*/)
{
}

void CThreadedSocket::OnAccept(int /*nErrorCode*/)
{
}

void CThreadedSocket::OnConnect(int /*nErrorCode*/)
{
}

void CThreadedSocket::OnClose(int /*nErrorCode*/)
{
}

/////////////////////////////////////////////////////////////////////////////
// CThreadedSocket Implementation


SOCKET CThreadedSocket::Socket(int nSocketType, int nProtocolType, int nAddressFormat)
{
	ASSERT(m_hSocket == INVALID_SOCKET);

	m_hSocket = socket(nAddressFormat,nSocketType,nProtocolType); 

	return m_hSocket;
	//return StartListener();
}

int CThreadedSocket::StartListener()
{
	h_reader = CreateThread(NULL,0,SocketReaderProc, this, 0,  &reader_id);
	return h_reader != NULL ? 0 : SOCKET_ERROR;
}
int CThreadedSocket::StartWriter()
{
	h_writer = CreateThread(NULL,0,SocketWriterProc, this, 0,  &writer_id);
	return h_writer != NULL ? 0 : SOCKET_ERROR;
}
bool CThreadedSocket::SignalWriter()
{
	long PreviousCount = 0;  
	BOOL err;
	err = ReleaseSemaphore(send_count, 1,  &PreviousCount);
//	TRACE("CThreadedSocket::SignalWriter() PreviousCount = %ld \n", PreviousCount );
	return (err != 0);
}

// Implementation 
DWORD WINAPI CThreadedSocket::SocketReaderProc( LPVOID pParam )
{
    CThreadedSocket* pObject = (CThreadedSocket*)pParam;

    if (pObject == NULL )
		{
		return SOCKET_ERROR;   // if pObject is not valid
		}

    return pObject->SocketReader();
}
// Implementation 
DWORD WINAPI CThreadedSocket::SocketWriterProc( LPVOID pParam )
{
    CThreadedSocket* pObject = (CThreadedSocket*)pParam;

    if (pObject == NULL )
		{
		return SOCKET_ERROR;   // if pObject is not valid
		}

    return pObject->SocketWriter();
}

UINT CThreadedSocket::SocketReader(  )
{
	int err;
	int sock_err;
	unsigned long readcount = 0;
	fd_set readfds;
	fd_set writefds;
	fd_set exceptfds;

	TRACE( "CThreadedSocket::SocketReader( START thread_id =( 0x%x) )\n",reader_id);

	FD_ZERO(&readfds);
	FD_ZERO(&writefds);
	FD_ZERO(&exceptfds);

	while ( m_hSocket != INVALID_SOCKET )
		{
		FD_SET(m_hSocket, &readfds);
		if ( connect_pending )
			FD_SET(m_hSocket, &writefds);
		else
			FD_CLR(m_hSocket, &writefds);
		FD_SET(m_hSocket, &exceptfds);

		err = select( 1, &readfds, &writefds, &exceptfds,   NULL );
		sock_err =  this->GetLastError();

		switch ( err ) 
		{
		case 0:
			TRACE( "CThreadedSocket::SocketThread Unhandled Timeout Event !\n");
			break;
		case SOCKET_ERROR:
		
			TRACE( "CThreadedSocket::SocketReader( select error thread_id =( 0x%x) )\n",reader_id);
			if ( (sock_err != WSAENOTSOCK ) && ( m_hSocket != INVALID_SOCKET ))  // could be Invalid if close when in select
				Close();
			h_reader = NULL;
			return sock_err;
			break;
		default:

			
			if ( FD_ISSET(m_hSocket, &readfds) )
				{
				readcount = 0;
				IOCtl( FIONREAD, &readcount );
				if ( listen_mode )
					OnAccept( sock_err );
				else if ( ! readcount )
					OnClose( sock_err );
				else OnReceive(sock_err);				
				}
			
			if ( FD_ISSET(m_hSocket, &writefds) ) 
				{
				if ( connect_pending )
					{
					OnConnect( sock_err );
					connect_pending = false;
					}	
				}

			if ( FD_ISSET(m_hSocket, &exceptfds) ) 
				OnOutOfBandData( sock_err );


			break;
		}
	}
	if (m_hSocket != INVALID_SOCKET)
		Close();
	h_reader = NULL;
	TRACE( "CThreadedSocket::SocketReader( END thread_id =( 0x%x) )\n",reader_id);

  return  0;
}

UINT CThreadedSocket::SocketWriter(  )
{
	int err;
	int sock_err;
	fd_set writefds;

	TRACE( "CThreadedSocket::SocketWriter( START thread_id =( 0x%x) )\n",writer_id);

	FD_ZERO(&writefds);


	while ( m_hSocket != INVALID_SOCKET )
		{
		DWORD dwWaitResult; 
 
		// Wait for message to send

		dwWaitResult = WaitForSingleObject( send_count, INFINITE );
  
		switch (dwWaitResult) { 
		
			// The semaphore object was signaled.
			case WAIT_OBJECT_0: 
				// OK to really send the message.
				break; 

			// Semaphore was nonsignaled, so a time-out occurred.
			case WAIT_TIMEOUT: 
			case WAIT_FAILED:
			default:
				TRACE( "CThreadedSocket::SocketWriter( WaitForSingleObject error thread_id =( %d) )\n",writer_id);
				if (m_hSocket != INVALID_SOCKET)
					Close();
				h_writer = NULL;
				return this->GetLastError();
				break; 
		}
		if (m_hSocket != INVALID_SOCKET)
			{
			FD_SET(m_hSocket, &writefds);

			err = select( 1, NULL, &writefds, NULL,   NULL );
			sock_err =  this->GetLastError();

			switch ( err ) 
			{
			case 0:
				TRACE( "CThreadedSocket::SocketThread Unhandled Timeout Event !\n");
				break;
			case SOCKET_ERROR:
				TRACE( "CThreadedSocketException( select )\n");
				if ( (sock_err != WSAENOTSOCK ) && ( m_hSocket != INVALID_SOCKET ))  // could be Invalid if close when in select
					 Close();
				h_reader = NULL;
				return sock_err;
				break;
			default:
				
				if ( FD_ISSET(m_hSocket, &writefds) ) 
					{
					OnSend( sock_err );
					}

				break;
			}
		}
	}
	Close();
	h_writer = NULL;
	TRACE( "CThreadedSocket::SocketWriter( END thread_id =( 0x%x) )\n",writer_id);

  return  0;
}