PTLib
Version 2.10.4
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
socket.h
Go to the documentation of this file.
1
/*
2
* socket.h
3
*
4
* Berkley Socket channel ancestor class.
5
*
6
* Portable Tools Library
7
*
8
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9
*
10
* The contents of this file are subject to the Mozilla Public License
11
* Version 1.0 (the "License"); you may not use this file except in
12
* compliance with the License. You may obtain a copy of the License at
13
* http://www.mozilla.org/MPL/
14
*
15
* Software distributed under the License is distributed on an "AS IS"
16
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17
* the License for the specific language governing rights and limitations
18
* under the License.
19
*
20
* The Original Code is Portable Windows Library.
21
*
22
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23
*
24
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25
* All Rights Reserved.
26
*
27
* Contributor(s): ______________________________________.
28
*
29
* $Revision: 24177 $
30
* $Author: rjongbloed $
31
* $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
32
*/
33
34
#ifndef PTLIB_SOCKET_H
35
#define PTLIB_SOCKET_H
36
37
#ifdef P_USE_PRAGMA
38
#pragma interface
39
#endif
40
41
#include <
ptlib/channel.h
>
42
43
#ifdef __NUCLEUS_PLUS__
44
#include <sys/socket.h>
45
#endif
46
47
class
PSocket
;
48
49
PLIST
(PSocketList,
PSocket
);
50
51
58
class
PSocket
:
public
PChannel
59
{
60
PCLASSINFO(
PSocket
,
PChannel
);
61
62
protected
:
63
PSocket
();
64
65
public
:
78
virtual
PBoolean
Connect
(
79
const
PString
& address
80
);
81
82
84
enum
Reusability
{
85
CanReuseAddress
,
86
AddressIsExclusive
87
};
88
102
virtual
PBoolean
Listen
(
103
unsigned
queueSize = 5,
104
WORD
port
= 0,
105
Reusability
reuse =
AddressIsExclusive
106
);
107
108
130
virtual
PBoolean
Accept
(
131
PSocket
& socket
132
);
133
139
virtual
PBoolean
Shutdown
(
140
ShutdownValue
option
141
);
143
152
PBoolean
SetOption
(
153
int
option,
154
int
value,
155
int
level = SOL_SOCKET
156
);
157
164
PBoolean
SetOption
(
165
int
option,
166
const
void
* valuePtr,
167
PINDEX valueSize,
168
int
level = SOL_SOCKET
169
);
170
177
PBoolean
GetOption
(
178
int
option,
179
int
& value,
180
int
level = SOL_SOCKET
181
);
182
189
PBoolean
GetOption
(
190
int
option,
191
void
* valuePtr,
192
PINDEX valueSize,
193
int
level = SOL_SOCKET
194
);
196
204
static
WORD
GetProtocolByName
(
205
const
PString
& name
206
);
207
213
static
PString
GetNameByProtocol
(
214
WORD proto
215
);
216
217
219
virtual
WORD
GetPortByService
(
220
const
PString
& service
221
)
const
;
239
static
WORD
GetPortByService
(
240
const
char
* protocol,
241
const
PString
& service
242
);
243
245
virtual
PString
GetServiceByPort
(
246
WORD
port
247
)
const
;
265
static
PString
GetServiceByPort
(
266
const
char
* protocol,
267
WORD
port
268
);
269
270
272
void
SetPort
(
273
WORD
port
274
);
287
void
SetPort
(
288
const
PString
& service
289
);
290
296
WORD
GetPort
()
const
;
297
305
PString
GetService
()
const
;
307
310
311
class
SelectList
:
public
PSocketList
312
{
313
PCLASSINFO
(
SelectList
, PSocketList)
314
public
:
315
SelectList
()
316
{ DisallowDeleteObjects(); }
318
void
operator+=
(
PSocket
& sock )
319
{ Append(&sock); }
321
void
operator-=
(
PSocket
& sock )
322
{ Remove(&sock); }
323
};
324
326
static
int
Select
(
327
PSocket
& sock1,
328
PSocket
& sock2
329
);
331
static
int
Select
(
332
PSocket
& sock1,
333
PSocket
& sock2,
334
const
PTimeInterval
& timeout
335
);
337
static
Errors
Select
(
338
SelectList & read
339
);
341
static
Errors
Select
(
342
SelectList & read,
343
const
PTimeInterval
& timeout
344
);
346
static
Errors
Select
(
347
SelectList & read,
348
SelectList & write
349
);
351
static
Errors
Select
(
352
SelectList & read,
353
SelectList & write,
354
const
PTimeInterval
& timeout
355
);
357
static
Errors
Select
(
358
SelectList & read,
359
SelectList & write,
360
SelectList & except
361
);
383
static
Errors
Select
(
384
SelectList & read,
385
SelectList & write,
386
SelectList & except,
387
const
PTimeInterval
& timeout
388
);
390
393
394
inline
static
WORD
Host2Net
(WORD v) {
return
htons(v); }
396
inline
static
DWORD
Host2Net
(DWORD v) {
return
htonl(v); }
397
399
inline
static
WORD
Net2Host
(WORD v) {
return
ntohs(v); }
401
inline
static
DWORD
Net2Host
(DWORD v) {
return
ntohl(v); }
403
404
protected
:
405
/*This function calls os_socket() with the correct parameters for the
406
socket protocol type.
407
*/
408
virtual
PBoolean
OpenSocket
() = 0;
409
412
virtual
const
char
*
GetProtocolName
()
const
= 0;
413
414
415
int
os_close
();
416
int
os_socket
(
int
af,
int
type,
int
proto);
417
PBoolean
os_connect
(
418
struct
sockaddr * sin,
419
PINDEX size
420
);
421
PBoolean
os_recvfrom
(
422
void
* buf,
423
PINDEX len,
424
int
flags,
425
struct
sockaddr * from,
426
PINDEX * fromlen
427
);
428
PBoolean
os_sendto
(
429
const
void
* buf,
430
PINDEX len,
431
int
flags,
432
struct
sockaddr * to,
433
PINDEX tolen
434
);
435
PBoolean
os_accept
(
436
PSocket
& listener,
437
struct
sockaddr * addr,
438
PINDEX * size
439
);
440
441
442
// Member variables
444
WORD
port
;
445
446
#if P_HAS_RECVMSG
447
PBoolean
catchReceiveToAddr;
448
virtual
void
SetLastReceiveAddr(
void
*
/*addr*/
,
int
/*addrLen*/
)
449
{ }
450
#endif
451
452
// Include platform dependent part of class
453
#ifdef _WIN32
454
#include "msos/ptlib/socket.h"
455
#else
456
#include "unix/ptlib/socket.h"
457
#endif
458
};
459
460
461
// Utility classes
462
463
class
P_fd_set
{
464
public
:
465
P_fd_set
();
466
P_fd_set
(SOCKET fd);
467
~P_fd_set
()
468
{
469
free
(
set
);
470
}
471
472
P_fd_set
&
operator=
(SOCKET fd);
473
P_fd_set
&
operator+=
(SOCKET fd);
474
P_fd_set
&
operator-=
(SOCKET fd);
475
476
void
Zero
();
477
478
PBoolean
IsPresent
(SOCKET fd)
const
479
{
480
return
FD_ISSET(fd,
set
);
481
}
482
483
operator
fd_set*()
const
484
{
485
return
set
;
486
}
487
488
protected
:
489
void
Construct
();
490
491
SOCKET
max_fd
;
492
fd_set *
set
;
493
494
private
:
495
P_fd_set
(
const
P_fd_set
&) {}
496
void
operator=
(
const
P_fd_set
&) {}
497
};
498
499
500
class
P_timeval
{
501
public
:
502
P_timeval
();
503
P_timeval
(
const
PTimeInterval
& time)
504
{
505
operator=
(time);
506
}
507
508
P_timeval
&
operator=
(
const
PTimeInterval
& time);
509
510
operator
timeval*()
511
{
512
return
infinite ? NULL : &tval;
513
}
514
515
timeval *
operator->
()
516
{
517
return
&tval;
518
}
519
520
timeval &
operator*
()
521
{
522
return
tval;
523
}
524
525
private
:
526
struct
timeval tval;
527
PBoolean
infinite;
528
};
529
530
#ifdef _WIN32
531
class
PWinSock :
public
PSocket
532
{
533
PCLASSINFO
(PWinSock,
PSocket
)
534
// Must be one and one only instance of this class, and it must be static!.
535
public:
536
PWinSock();
537
~PWinSock();
538
private:
539
virtual
PBoolean
OpenSocket();
540
virtual const
char
* GetProtocolName() const;
541
};
542
#endif
543
544
545
#endif // PTLIB_SOCKET_H
546
547
548
// End Of File ///////////////////////////////////////////////////////////////
include
ptlib
socket.h
Generated on Tue Mar 11 2014 15:09:30 for PTLib by
1.8.1.2