socket.h
Go to the documentation of this file.
/*
* socket.h
*
* Berkley Socket channel ancestor class.
*
* Portable Tools Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Revision: 27568 $
* $Author: ededu $
* $Date: 2012-05-03 07:57:49 -0500 (Thu, 03 May 2012) $
*/
#ifndef PTLIB_SOCKET_H
#define PTLIB_SOCKET_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
#include <ptlib/channel.h>
#ifdef __NUCLEUS_PLUS__
#include <sys/socket.h>
#endif
class PSocket;
PLIST(PSocketList, PSocket);
class PSocket : public PChannel
{
PCLASSINFO(PSocket, PChannel);
protected:
PSocket();
public:
virtual PBoolean Connect(
const PString & address
);
enum Reusability {
CanReuseAddress,
AddressIsExclusive
};
virtual PBoolean Listen(
unsigned queueSize = 5,
WORD port = 0,
Reusability reuse = AddressIsExclusive
);
virtual PBoolean Accept(
PSocket & socket
);
virtual PBoolean Shutdown(
ShutdownValue option
);
PBoolean SetOption(
int option,
int value,
int level = SOL_SOCKET
);
PBoolean SetOption(
int option,
const void * valuePtr,
PINDEX valueSize,
int level = SOL_SOCKET
);
PBoolean GetOption(
int option,
int & value,
int level = SOL_SOCKET
);
PBoolean GetOption(
int option,
void * valuePtr,
PINDEX valueSize,
int level = SOL_SOCKET
);
static WORD GetProtocolByName(
const PString & name
);
static PString GetNameByProtocol(
WORD proto
);
virtual WORD GetPortByService(
const PString & service
) const;
static WORD GetPortByService(
const char * protocol,
const PString & service
);
virtual PString GetServiceByPort(
WORD port
) const;
static PString GetServiceByPort(
const char * protocol,
WORD port
);
void SetPort(
WORD port
);
void SetPort(
const PString & service
);
WORD GetPort() const;
PString GetService() const;
class SelectList : public PSocketList
{
PCLASSINFO(SelectList, PSocketList)
public:
SelectList()
{ DisallowDeleteObjects(); }
void operator+=(PSocket & sock )
{ Append(&sock); }
void operator-=(PSocket & sock )
{ Remove(&sock); }
};
static int Select(
PSocket & sock1,
PSocket & sock2
);
static int Select(
PSocket & sock1,
PSocket & sock2,
const PTimeInterval & timeout
);
static Errors Select(
SelectList & read
);
static Errors Select(
SelectList & read,
const PTimeInterval & timeout
);
static Errors Select(
SelectList & read,
SelectList & write
);
static Errors Select(
SelectList & read,
SelectList & write,
const PTimeInterval & timeout
);
static Errors Select(
SelectList & read,
SelectList & write,
SelectList & except
);
static Errors Select(
SelectList & read,
SelectList & write,
SelectList & except,
const PTimeInterval & timeout
);
inline static WORD Host2Net(WORD v) { return htons(v); }
inline static DWORD Host2Net(DWORD v) { return htonl(v); }
inline static WORD Net2Host(WORD v) { return ntohs(v); }
inline static DWORD Net2Host(DWORD v) { return ntohl(v); }
protected:
/*This function calls os_socket() with the correct parameters for the
socket protocol type.
*/
virtual PBoolean OpenSocket() = 0;
virtual const char * GetProtocolName() const = 0;
int os_close();
int os_socket(int af, int type, int proto);
PBoolean os_connect(
struct sockaddr * sin,
PINDEX size
);
PBoolean os_recvfrom(
void * buf,
PINDEX len,
int flags,
struct sockaddr * from,
PINDEX * fromlen
);
PBoolean os_sendto(
const void * buf,
PINDEX len,
int flags,
struct sockaddr * to,
PINDEX tolen
);
PBoolean os_accept(
PSocket & listener,
struct sockaddr * addr,
PINDEX * size
);
// Member variables
WORD port;
#if P_HAS_RECVMSG
PBoolean catchReceiveToAddr;
virtual void SetLastReceiveAddr(void * /*addr*/, int /*addrLen*/)
{ }
#endif
// Include platform dependent part of class
#ifdef _WIN32
#include "msos/ptlib/socket.h"
#else
#include "unix/ptlib/socket.h"
#endif
};
// Utility classes
class P_fd_set {
public:
P_fd_set();
P_fd_set(SOCKET fd);
~P_fd_set()
{
free(set);
}
P_fd_set & operator=(SOCKET fd);
P_fd_set & operator+=(SOCKET fd);
P_fd_set & operator-=(SOCKET fd);
void Zero();
PBoolean IsPresent(SOCKET fd) const
{
return FD_ISSET(fd, set);
}
operator fd_set*() const
{
return set;
}
protected:
void Construct();
SOCKET max_fd;
fd_set * set;
private:
#ifdef __MINGW32__
// avoid compile error when this declaration is private
// this could be a gcc bug
public:
P_fd_set(const P_fd_set &) {}
private:
#else
P_fd_set(const P_fd_set &) {}
#endif
void operator=(const P_fd_set &) {}
};
class P_timeval {
public:
P_timeval();
P_timeval(const PTimeInterval & time)
{
operator=(time);
}
P_timeval & operator=(const PTimeInterval & time);
operator timeval*()
{
return infinite ? NULL : &tval;
}
timeval * operator->()
{
return &tval;
}
timeval & operator*()
{
return tval;
}
private:
struct timeval tval;
PBoolean infinite;
};
#ifdef _WIN32
class PWinSock : public PSocket
{
PCLASSINFO(PWinSock, PSocket)
// Must be one and one only instance of this class, and it must be static!.
public:
PWinSock();
~PWinSock();
private:
virtual PBoolean OpenSocket();
virtual const char * GetProtocolName() const;
};
#endif
#endif // PTLIB_SOCKET_H
// End Of File ///////////////////////////////////////////////////////////////
PSocket::AddressIsExclusive
Definition: socket.h:86
P_timeval::P_timeval
P_timeval(const PTimeInterval &time)
Definition: socket.h:511
PSocket::os_close
int os_close()
PSocket::Host2Net
static DWORD Host2Net(DWORD v)
Convert from host to network byte order.
Definition: socket.h:396
PTimeInterval
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
PSocket::GetService
PString GetService() const
Get a service name for the port number the TCP socket channel object instance is using.
PCLASSINFO
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PLIST
PLIST(PSocketList, PSocket)
PChannel::operator=
PChannel & operator=(const PChannel &)
PSocket::os_connect
PBoolean os_connect(struct sockaddr *sin, PINDEX size)
PSocket::PSocket
PSocket()
PSocket::GetProtocolByName
static WORD GetProtocolByName(const PString &name)
Get the number of the protocol associated with the specified name.
PSocket::Select
static int Select(PSocket &sock1, PSocket &sock2)
Select a socket with available data.
PSocket::SelectList
List of sockets used for Select() function.
Definition: socket.h:311
PSocket::Shutdown
virtual PBoolean Shutdown(ShutdownValue option)
Close one or both of the data streams associated with a socket.
PSocket::port
WORD port
Port to be used by the socket when opening the channel.
Definition: socket.h:444
PChannel::ShutdownValue
ShutdownValue
Definition: channel.h:433
PSocket::GetNameByProtocol
static PString GetNameByProtocol(WORD proto)
Get the name of the protocol number specified.
PChannel::Errors
Errors
Normalised error codes.
Definition: channel.h:529
PSocket::Connect
virtual PBoolean Connect(const PString &address)
Connect a socket to a remote host on the specified port number.
P_timeval::operator->
timeval * operator->()
Definition: socket.h:523
PBoolean
BOOL PBoolean
Definition: object.h:102
channel.h
PSocket::Net2Host
static DWORD Net2Host(DWORD v)
Convert from network to host byte order.
Definition: socket.h:401
PSocket::GetOption
PBoolean GetOption(int option, int &value, int level=SOL_SOCKET)
Get options on the socket.
PSocket::CanReuseAddress
Definition: socket.h:85
PSocket::os_accept
PBoolean os_accept(PSocket &listener, struct sockaddr *addr, PINDEX *size)
free
#define free(p)
Override of system call for memory check system.
Definition: object.h:864
PSocket::OpenSocket
virtual PBoolean OpenSocket()=0
PSocket::GetPortByService
virtual WORD GetPortByService(const PString &service) const
Get the port number for the specified service name.
PSocket::Host2Net
static WORD Host2Net(WORD v)
Convert from host to network byte order.
Definition: socket.h:394
PChannel
Abstract class defining I/O channel semantics.
Definition: channel.h:107
P_fd_set
Definition: socket.h:463
PString
The character string class.
Definition: pstring.h:108
P_fd_set::max_fd
SOCKET max_fd
Definition: socket.h:491
PSocket::GetServiceByPort
virtual PString GetServiceByPort(WORD port) const
Get the service name from the port number.
P_timeval
Definition: socket.h:508
PSocket::os_recvfrom
PBoolean os_recvfrom(void *buf, PINDEX len, int flags, struct sockaddr *from, PINDEX *fromlen)
PSocket::GetPort
WORD GetPort() const
Get the port the TCP socket channel object instance is using.
PSocket::SelectList::operator-=
void operator-=(PSocket &sock)
Remove a socket from list .
Definition: socket.h:321
PSocket::SelectList::operator+=
void operator+=(PSocket &sock)
Add a socket to list .
Definition: socket.h:318
P_fd_set::~P_fd_set
~P_fd_set()
Definition: socket.h:467
PSocket::GetProtocolName
virtual const char * GetProtocolName() const =0
This function returns the protocol name for the socket type.
PSocket
A network communications channel.
Definition: socket.h:58
PSocket::Net2Host
static WORD Net2Host(WORD v)
Convert from network to host byte order.
Definition: socket.h:399
PSocket::Reusability
Reusability
Flags to reuse of port numbers in Listen() function.
Definition: socket.h:84
PSocket::SetOption
PBoolean SetOption(int option, int value, int level=SOL_SOCKET)
Set options on the socket.
P_fd_set::IsPresent
PBoolean IsPresent(SOCKET fd) const
Definition: socket.h:478
PSocket::Listen
virtual PBoolean Listen(unsigned queueSize=5, WORD port=0, Reusability reuse=AddressIsExclusive)
Listen on a socket for a remote host on the specified port number.
PSocket::Accept
virtual PBoolean Accept(PSocket &socket)
Open a socket to a remote host on the specified port number.
PSocket::SetPort
void SetPort(WORD port)
Set the port number for the channel.
PSocket::os_socket
int os_socket(int af, int type, int proto)
P_timeval::operator*
timeval & operator*()
Definition: socket.h:528
PSocket::os_sendto
PBoolean os_sendto(const void *buf, PINDEX len, int flags, struct sockaddr *to, PINDEX tolen)