PTLib
Version 2.10.4
|
00001 /* 00002 * socket.h 00003 * 00004 * Berkley Socket channel ancestor class. 00005 * 00006 * Portable Tools 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 * $Revision: 24177 $ 00030 * $Author: rjongbloed $ 00031 * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $ 00032 */ 00033 00034 #ifndef PTLIB_SOCKET_H 00035 #define PTLIB_SOCKET_H 00036 00037 #ifdef P_USE_PRAGMA 00038 #pragma interface 00039 #endif 00040 00041 #include <ptlib/channel.h> 00042 00043 #ifdef __NUCLEUS_PLUS__ 00044 #include <sys/socket.h> 00045 #endif 00046 00047 class PSocket; 00048 00049 PLIST(PSocketList, PSocket); 00050 00051 00058 class PSocket : public PChannel 00059 { 00060 PCLASSINFO(PSocket, PChannel); 00061 00062 protected: 00063 PSocket(); 00064 00065 public: 00078 virtual PBoolean Connect( 00079 const PString & address 00080 ); 00081 00082 00084 enum Reusability { 00085 CanReuseAddress, 00086 AddressIsExclusive 00087 }; 00088 00102 virtual PBoolean Listen( 00103 unsigned queueSize = 5, 00104 WORD port = 0, 00105 Reusability reuse = AddressIsExclusive 00106 ); 00107 00108 00130 virtual PBoolean Accept( 00131 PSocket & socket 00132 ); 00133 00139 virtual PBoolean Shutdown( 00140 ShutdownValue option 00141 ); 00143 00152 PBoolean SetOption( 00153 int option, 00154 int value, 00155 int level = SOL_SOCKET 00156 ); 00157 00164 PBoolean SetOption( 00165 int option, 00166 const void * valuePtr, 00167 PINDEX valueSize, 00168 int level = SOL_SOCKET 00169 ); 00170 00177 PBoolean GetOption( 00178 int option, 00179 int & value, 00180 int level = SOL_SOCKET 00181 ); 00182 00189 PBoolean GetOption( 00190 int option, 00191 void * valuePtr, 00192 PINDEX valueSize, 00193 int level = SOL_SOCKET 00194 ); 00196 00204 static WORD GetProtocolByName( 00205 const PString & name 00206 ); 00207 00213 static PString GetNameByProtocol( 00214 WORD proto 00215 ); 00216 00217 00219 virtual WORD GetPortByService( 00220 const PString & service 00221 ) const; 00239 static WORD GetPortByService( 00240 const char * protocol, 00241 const PString & service 00242 ); 00243 00245 virtual PString GetServiceByPort( 00246 WORD port 00247 ) const; 00265 static PString GetServiceByPort( 00266 const char * protocol, 00267 WORD port 00268 ); 00269 00270 00272 void SetPort( 00273 WORD port 00274 ); 00287 void SetPort( 00288 const PString & service 00289 ); 00290 00296 WORD GetPort() const; 00297 00305 PString GetService() const; 00307 00310 00311 class SelectList : public PSocketList 00312 { 00313 PCLASSINFO(SelectList, PSocketList) 00314 public: 00315 SelectList() 00316 { DisallowDeleteObjects(); } 00318 void operator+=(PSocket & sock ) 00319 { Append(&sock); } 00321 void operator-=(PSocket & sock ) 00322 { Remove(&sock); } 00323 }; 00324 00326 static int Select( 00327 PSocket & sock1, 00328 PSocket & sock2 00329 ); 00331 static int Select( 00332 PSocket & sock1, 00333 PSocket & sock2, 00334 const PTimeInterval & timeout 00335 ); 00337 static Errors Select( 00338 SelectList & read 00339 ); 00341 static Errors Select( 00342 SelectList & read, 00343 const PTimeInterval & timeout 00344 ); 00346 static Errors Select( 00347 SelectList & read, 00348 SelectList & write 00349 ); 00351 static Errors Select( 00352 SelectList & read, 00353 SelectList & write, 00354 const PTimeInterval & timeout 00355 ); 00357 static Errors Select( 00358 SelectList & read, 00359 SelectList & write, 00360 SelectList & except 00361 ); 00383 static Errors Select( 00384 SelectList & read, 00385 SelectList & write, 00386 SelectList & except, 00387 const PTimeInterval & timeout 00388 ); 00390 00393 00394 inline static WORD Host2Net(WORD v) { return htons(v); } 00396 inline static DWORD Host2Net(DWORD v) { return htonl(v); } 00397 00399 inline static WORD Net2Host(WORD v) { return ntohs(v); } 00401 inline static DWORD Net2Host(DWORD v) { return ntohl(v); } 00403 00404 protected: 00405 /*This function calls os_socket() with the correct parameters for the 00406 socket protocol type. 00407 */ 00408 virtual PBoolean OpenSocket() = 0; 00409 00412 virtual const char * GetProtocolName() const = 0; 00413 00414 00415 int os_close(); 00416 int os_socket(int af, int type, int proto); 00417 PBoolean os_connect( 00418 struct sockaddr * sin, 00419 PINDEX size 00420 ); 00421 PBoolean os_recvfrom( 00422 void * buf, 00423 PINDEX len, 00424 int flags, 00425 struct sockaddr * from, 00426 PINDEX * fromlen 00427 ); 00428 PBoolean os_sendto( 00429 const void * buf, 00430 PINDEX len, 00431 int flags, 00432 struct sockaddr * to, 00433 PINDEX tolen 00434 ); 00435 PBoolean os_accept( 00436 PSocket & listener, 00437 struct sockaddr * addr, 00438 PINDEX * size 00439 ); 00440 00441 00442 // Member variables 00444 WORD port; 00445 00446 #if P_HAS_RECVMSG 00447 PBoolean catchReceiveToAddr; 00448 virtual void SetLastReceiveAddr(void * /*addr*/, int /*addrLen*/) 00449 { } 00450 #endif 00451 00452 // Include platform dependent part of class 00453 #ifdef _WIN32 00454 #include "msos/ptlib/socket.h" 00455 #else 00456 #include "unix/ptlib/socket.h" 00457 #endif 00458 }; 00459 00460 00461 // Utility classes 00462 00463 class P_fd_set { 00464 public: 00465 P_fd_set(); 00466 P_fd_set(SOCKET fd); 00467 ~P_fd_set() 00468 { 00469 free(set); 00470 } 00471 00472 P_fd_set & operator=(SOCKET fd); 00473 P_fd_set & operator+=(SOCKET fd); 00474 P_fd_set & operator-=(SOCKET fd); 00475 00476 void Zero(); 00477 00478 PBoolean IsPresent(SOCKET fd) const 00479 { 00480 return FD_ISSET(fd, set); 00481 } 00482 00483 operator fd_set*() const 00484 { 00485 return set; 00486 } 00487 00488 protected: 00489 void Construct(); 00490 00491 SOCKET max_fd; 00492 fd_set * set; 00493 00494 private: 00495 P_fd_set(const P_fd_set &) {} 00496 void operator=(const P_fd_set &) {} 00497 }; 00498 00499 00500 class P_timeval { 00501 public: 00502 P_timeval(); 00503 P_timeval(const PTimeInterval & time) 00504 { 00505 operator=(time); 00506 } 00507 00508 P_timeval & operator=(const PTimeInterval & time); 00509 00510 operator timeval*() 00511 { 00512 return infinite ? NULL : &tval; 00513 } 00514 00515 timeval * operator->() 00516 { 00517 return &tval; 00518 } 00519 00520 timeval & operator*() 00521 { 00522 return tval; 00523 } 00524 00525 private: 00526 struct timeval tval; 00527 PBoolean infinite; 00528 }; 00529 00530 #ifdef _WIN32 00531 class PWinSock : public PSocket 00532 { 00533 PCLASSINFO(PWinSock, PSocket) 00534 // Must be one and one only instance of this class, and it must be static!. 00535 public: 00536 PWinSock(); 00537 ~PWinSock(); 00538 private: 00539 virtual PBoolean OpenSocket(); 00540 virtual const char * GetProtocolName() const; 00541 }; 00542 #endif 00543 00544 00545 #endif // PTLIB_SOCKET_H 00546 00547 00548 // End Of File ///////////////////////////////////////////////////////////////