Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

socket.h

Go to the documentation of this file.
00001 /* 00002 * socket.h 00003 * 00004 * Berkley Socket channel ancestor class. 00005 * 00006 * Portable Windows Library 00007 * 00008 * Copyright (c) 1993-1998 Equivalence Pty. Ltd. 00009 * 00010 * The contents of this file are subject to the Mozilla Public License 00011 * Version 1.0 (the "License"); you may not use this file except in 00012 * compliance with the License. You may obtain a copy of the License at 00013 * http://www.mozilla.org/MPL/ 00014 * 00015 * Software distributed under the License is distributed on an "AS IS" 00016 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00017 * the License for the specific language governing rights and limitations 00018 * under the License. 00019 * 00020 * The Original Code is Portable Windows Library. 00021 * 00022 * The Initial Developer of the Original Code is Equivalence Pty. Ltd. 00023 * 00024 * Portions are Copyright (C) 1993 Free Software Foundation, Inc. 00025 * All Rights Reserved. 00026 * 00027 * Contributor(s): ______________________________________. 00028 * 00029 * $Log: socket.h,v $ 00030 * Revision 1.47 2004/05/06 11:28:30 rjongbloed 00031 * Changed P_fd_set to use malloc/free isntead of new/delete due to pedantry about []. 00032 * 00033 * Revision 1.46 2004/04/27 04:37:50 rjongbloed 00034 * Fixed ability to break of a PSocket::Select call under linux when a socket 00035 * is closed by another thread. 00036 * 00037 * Revision 1.45 2004/01/28 08:53:02 csoutheren 00038 * Fixed missing delete[] operator. Thanks to Borko Jandras 00039 * 00040 * Revision 1.44 2003/09/17 05:41:59 csoutheren 00041 * Removed recursive includes 00042 * 00043 * Revision 1.43 2003/09/17 01:18:02 csoutheren 00044 * Removed recursive include file system and removed all references 00045 * to deprecated coooperative threading support 00046 * 00047 * Revision 1.42 2002/10/18 08:07:41 robertj 00048 * Fixed use of FD_ZERO as (strangely) crashes on some paltforms and would 00049 * not have cleared enough of an enlarges fd_set anyway. 00050 * 00051 * Revision 1.41 2002/10/17 07:17:42 robertj 00052 * Added ability to increase maximum file handles on a process. 00053 * 00054 * Revision 1.40 2002/09/16 01:08:59 robertj 00055 * Added #define so can select if #pragma interface/implementation is used on 00056 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00057 * 00058 * Revision 1.39 2002/04/12 01:42:41 robertj 00059 * Changed return value on os_connect() and os_accept() to make sure 00060 * get the correct error codes propagated up under unix. 00061 * 00062 * Revision 1.38 2002/02/14 03:34:18 craigs 00063 * Added comment on using SetReadTimeout to set maximum wait for Connect 00064 * 00065 * Revision 1.37 2001/09/06 02:30:17 robertj 00066 * Fixed mismatched declarations, thanks Vjacheslav Andrejev 00067 * 00068 * Revision 1.36 2001/05/22 12:49:32 robertj 00069 * Did some seriously wierd rewrite of platform headers to eliminate the 00070 * stupid GNU compiler warning about braces not matching. 00071 * 00072 * Revision 1.35 2001/03/20 06:44:25 robertj 00073 * Lots of changes to fix the problems with terminating threads that are I/O 00074 * blocked, especially when doing orderly shutdown of service via SIGTERM. 00075 * 00076 * Revision 1.34 2000/06/26 13:58:42 robertj 00077 * Nucleus port (again) 00078 * 00079 * Revision 1.33 2000/06/26 11:17:19 robertj 00080 * Nucleus++ port (incomplete). 00081 * 00082 * Revision 1.32 2000/02/18 09:55:21 robertj 00083 * Added parameter so get/setsockopt can have other levels to SOL_SOCKET. 00084 * 00085 * Revision 1.31 1999/03/09 02:59:51 robertj 00086 * Changed comments to doc++ compatible documentation. 00087 * 00088 * Revision 1.30 1999/02/16 08:11:10 robertj 00089 * MSVC 6.0 compatibility changes. 00090 * 00091 * Revision 1.29 1998/11/30 02:51:58 robertj 00092 * New directory structure 00093 * 00094 * Revision 1.28 1998/09/23 06:21:25 robertj 00095 * Added open source copyright license. 00096 * 00097 * Revision 1.27 1998/08/27 00:46:58 robertj 00098 * Resolved signedness problems with various GNU libraries. 00099 * 00100 * Revision 1.26 1998/08/25 14:07:42 robertj 00101 * Added getprotobyxxx wrapper functions. 00102 * 00103 * Revision 1.25 1998/01/26 00:35:21 robertj 00104 * Fixed documentation of PSocket::Select(). 00105 * 00106 * Revision 1.24 1996/09/14 13:09:24 robertj 00107 * Major upgrade: 00108 * rearranged sockets to help support IPX. 00109 * added indirect channel class and moved all protocols to descend from it, 00110 * separating the protocol from the low level byte transport. 00111 * 00112 * Revision 1.23 1996/07/27 04:14:00 robertj 00113 * Changed Select() calls to return error codes. 00114 * 00115 * Revision 1.22 1996/05/15 10:11:38 robertj 00116 * Added timeout to accept function. 00117 * 00118 * Revision 1.21 1996/03/31 08:52:36 robertj 00119 * Added socket shutdown function. 00120 * 00121 * Revision 1.20 1996/03/18 13:33:12 robertj 00122 * Fixed incompatibilities to GNU compiler where PINDEX != int. 00123 * 00124 * Revision 1.19 1996/03/03 07:37:58 robertj 00125 * Added Reusability clause to the Listen() function on sockets. 00126 * 00127 * Revision 1.18 1996/03/02 03:10:18 robertj 00128 * Added Apability to get and set Berkeley socket options. 00129 * 00130 * Revision 1.17 1996/02/25 03:02:14 robertj 00131 * Moved some socket functions to platform dependent code. 00132 * Added array of fds to os_select for unix threading support. 00133 * 00134 * Revision 1.16 1996/02/15 14:46:43 robertj 00135 * Added Select() function to PSocket. 00136 * 00137 * Revision 1.15 1995/12/23 03:46:54 robertj 00138 * Fixed portability issue with closingh sockets. 00139 * 00140 * Revision 1.14 1995/12/10 11:35:21 robertj 00141 * Numerous fixes for sockets. 00142 * 00143 * Revision 1.13 1995/10/14 15:05:54 robertj 00144 * Added functions for changing integer from host to network byte order. 00145 * 00146 * Revision 1.12 1995/06/17 11:13:25 robertj 00147 * Documentation update. 00148 * 00149 * Revision 1.11 1995/06/17 00:44:35 robertj 00150 * More logical design of port numbers and service names. 00151 * Changed overloaded Open() calls to 3 separate function names. 00152 * 00153 * Revision 1.10 1995/06/04 12:36:37 robertj 00154 * Slight redesign of port numbers on sockets. 00155 * 00156 * Revision 1.9 1995/03/14 12:42:39 robertj 00157 * Updated documentation to use HTML codes. 00158 * 00159 * Revision 1.8 1995/03/12 04:45:40 robertj 00160 * Added more functionality. 00161 * 00162 * Revision 1.7 1995/01/03 09:36:19 robertj 00163 * Documentation. 00164 * 00165 * Revision 1.6 1995/01/02 12:16:17 robertj 00166 * Moved constructor to platform dependent code. 00167 * 00168 * Revision 1.5 1994/08/23 11:32:52 robertj 00169 * Oops 00170 * 00171 * Revision 1.4 1994/08/22 00:46:48 robertj 00172 * Added pragma fro GNU C++ compiler. 00173 * 00174 * Revision 1.3 1994/08/21 23:43:02 robertj 00175 * Changed type of socket port number for better portability. 00176 * 00177 * Revision 1.2 1994/07/25 03:36:03 robertj 00178 * Added sockets to common, normalising to same comment standard. 00179 * 00180 */ 00181 00182 #ifndef _PSOCKETS 00183 #define _PSOCKETS 00184 00185 #ifdef P_USE_PRAGMA 00186 #pragma interface 00187 #endif 00188 00189 #include <ptlib/channel.h> 00190 00191 #ifdef __NUCLEUS_PLUS__ 00192 #include <sys/socket.h> 00193 #endif 00194 00195 class PSocket; 00196 00197 PLIST(PSocketList, PSocket); 00198 00199 00206 class PSocket : public PChannel 00207 { 00208 PCLASSINFO(PSocket, PChannel); 00209 00210 protected: 00211 PSocket(); 00212 00213 public: 00226 virtual BOOL Connect( 00227 const PString & address 00228 ); 00229 00230 00232 enum Reusability { 00233 CanReuseAddress, 00234 AddressIsExclusive 00235 }; 00236 00250 virtual BOOL Listen( 00251 unsigned queueSize = 5, 00252 WORD port = 0, 00253 Reusability reuse = AddressIsExclusive 00254 ); 00255 00256 00278 virtual BOOL Accept( 00279 PSocket & socket 00280 ); 00281 00287 virtual BOOL Shutdown( 00288 ShutdownValue option 00289 ); 00291 00300 BOOL SetOption( 00301 int option, 00302 int value, 00303 int level = SOL_SOCKET 00304 ); 00305 00312 BOOL SetOption( 00313 int option, 00314 const void * valuePtr, 00315 PINDEX valueSize, 00316 int level = SOL_SOCKET 00317 ); 00318 00325 BOOL GetOption( 00326 int option, 00327 int & value, 00328 int level = SOL_SOCKET 00329 ); 00330 00337 BOOL GetOption( 00338 int option, 00339 void * valuePtr, 00340 PINDEX valueSize, 00341 int level = SOL_SOCKET 00342 ); 00344 00352 static WORD GetProtocolByName( 00353 const PString & name 00354 ); 00355 00361 static PString GetNameByProtocol( 00362 WORD proto 00363 ); 00364 00365 00367 virtual WORD GetPortByService( 00368 const PString & service 00369 ) const; 00387 static WORD GetPortByService( 00388 const char * protocol, 00389 const PString & service 00390 ); 00391 00393 virtual PString GetServiceByPort( 00394 WORD port 00395 ) const; 00413 static PString GetServiceByPort( 00414 const char * protocol, 00415 WORD port 00416 ); 00417 00418 00420 void SetPort( 00421 WORD port 00422 ); 00435 void SetPort( 00436 const PString & service 00437 ); 00438 00444 WORD GetPort() const; 00445 00453 PString GetService() const; 00455 00458 00459 class SelectList : public PSocketList 00460 { 00461 PCLASSINFO(SelectList, PSocketList) 00462 public: 00463 SelectList() 00464 { DisallowDeleteObjects(); } 00466 void operator+=(PSocket & sock ) 00467 { Append(&sock); } 00469 void operator-=(PSocket & sock ) 00470 { Remove(&sock); } 00471 }; 00472 00474 static int Select( 00475 PSocket & sock1, 00476 PSocket & sock2 00477 ); 00479 static int Select( 00480 PSocket & sock1, 00481 PSocket & sock2, 00482 const PTimeInterval & timeout 00483 ); 00485 static Errors Select( 00486 SelectList & read 00487 ); 00489 static Errors Select( 00490 SelectList & read, 00491 const PTimeInterval & timeout 00492 ); 00494 static Errors Select( 00495 SelectList & read, 00496 SelectList & write 00497 ); 00499 static Errors Select( 00500 SelectList & read, 00501 SelectList & write, 00502 const PTimeInterval & timeout 00503 ); 00505 static Errors Select( 00506 SelectList & read, 00507 SelectList & write, 00508 SelectList & except 00509 ); 00531 static Errors Select( 00532 SelectList & read, 00533 SelectList & write, 00534 SelectList & except, 00535 const PTimeInterval & timeout 00536 ); 00538 00541 00542 inline static WORD Host2Net(WORD v) { return htons(v); } 00544 inline static DWORD Host2Net(DWORD v) { return htonl(v); } 00545 00547 inline static WORD Net2Host(WORD v) { return ntohs(v); } 00549 inline static DWORD Net2Host(DWORD v) { return ntohl(v); } 00551 00552 protected: 00553 /*This function calls os_socket() with the correct parameters for the 00554 socket protocol type. 00555 */ 00556 virtual BOOL OpenSocket() = 0; 00557 00560 virtual const char * GetProtocolName() const = 0; 00561 00562 00563 int os_close(); 00564 int os_socket(int af, int type, int proto); 00565 BOOL os_connect( 00566 struct sockaddr * sin, 00567 PINDEX size 00568 ); 00569 BOOL os_recvfrom( 00570 void * buf, 00571 PINDEX len, 00572 int flags, 00573 struct sockaddr * from, 00574 PINDEX * fromlen 00575 ); 00576 BOOL os_sendto( 00577 const void * buf, 00578 PINDEX len, 00579 int flags, 00580 struct sockaddr * to, 00581 PINDEX tolen 00582 ); 00583 BOOL os_accept( 00584 PSocket & listener, 00585 struct sockaddr * addr, 00586 PINDEX * size 00587 ); 00588 00589 00590 // Member variables 00592 WORD port; 00593 00594 // Include platform dependent part of class 00595 #ifdef _WIN32 00596 #include "msos/ptlib/socket.h" 00597 #else 00598 #include "unix/ptlib/socket.h" 00599 #endif 00600 }; 00601 00602 00603 // Utility classes 00604 00605 class P_fd_set { 00606 public: 00607 P_fd_set(); 00608 P_fd_set(SOCKET fd); 00609 ~P_fd_set() 00610 { 00611 free(set); 00612 } 00613 00614 P_fd_set & operator=(SOCKET fd); 00615 P_fd_set & operator+=(SOCKET fd); 00616 P_fd_set & operator-=(SOCKET fd); 00617 00618 void Zero(); 00619 00620 BOOL IsPresent(SOCKET fd) const 00621 { 00622 return FD_ISSET(fd, set); 00623 } 00624 00625 operator fd_set*() const 00626 { 00627 return set; 00628 } 00629 00630 private: 00631 void Construct(); 00632 00633 SOCKET max_fd; 00634 fd_set * set; 00635 }; 00636 00637 00638 class P_timeval { 00639 public: 00640 P_timeval(); 00641 P_timeval(const PTimeInterval & time) 00642 { 00643 operator=(time); 00644 } 00645 00646 P_timeval & operator=(const PTimeInterval & time); 00647 00648 operator timeval*() 00649 { 00650 return infinite ? NULL : &tval; 00651 } 00652 00653 timeval * operator->() 00654 { 00655 return &tval; 00656 } 00657 00658 timeval & operator*() 00659 { 00660 return tval; 00661 } 00662 00663 private: 00664 struct timeval tval; 00665 BOOL infinite; 00666 }; 00667 00668 #ifdef _WIN32 00669 class PWinSock : public PSocket 00670 { 00671 PCLASSINFO(PWinSock, PSocket) 00672 // Must be one and one only instance of this class, and it must be static!. 00673 public: 00674 PWinSock(); 00675 ~PWinSock(); 00676 private: 00677 virtual BOOL OpenSocket(); 00678 virtual const char * GetProtocolName() const; 00679 }; 00680 #endif 00681 00682 #endif 00683 00684 // End Of File ///////////////////////////////////////////////////////////////

Generated on Sat Jul 24 15:35:56 2004 for PWLib by doxygen 1.3.7