From be47ddbcc65b100c3634b5a4c35a89c8eac94a7e Mon Sep 17 00:00:00 2001 From: chatty Date: Tue, 28 Nov 2000 17:07:45 +0000 Subject: * Removed Smart pointers * Added ReuseAddress and AllowBroadcast --- comm/Socket.cc | 82 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 25 deletions(-) (limited to 'comm/Socket.cc') diff --git a/comm/Socket.cc b/comm/Socket.cc index 1bdb4f8..b90fe66 100644 --- a/comm/Socket.cc +++ b/comm/Socket.cc @@ -3,7 +3,7 @@ * * by Michel Beaudouin-Lafon * - * Copyright 1990-1993 + * Copyright 1990-1997 * Laboratoire de Recherche en Informatique (LRI) * * Sockets @@ -17,25 +17,36 @@ #include #include #include -extern int errno; +extern int errno; /*?class UchSocket -The class \typ{UchSocket} derives from \typ{UchChannel}. -It is a virtual base class: no objects of class \typ{UchSocket} are ever created. -It implements a Unix socket, that is a file descriptor and two addresses: -the address the socket is bound to, and the address it is connected to. -An address needs to be bound to a socket only if another process wants to connect to this socket; -a socket needs to be connected to an address only for streams or connected datagrams. +The class \typ{UchSocket} derives from \typ{UchChannel}. It is a +virtual base class: no objects of class \typ{UchSocket} are ever +created. It implements Unix sockets, that is file descriptors with +two addresses: the address the socket is bound to, and the address it +is connected to. An address needs to be bound to a socket only if +another process wants to connect to this socket; a socket needs to be +connected to an address only for streams or connected datagrams. Thus, none of the addresses is mandatory. -Addresses are always referenced by pointers. -Smart pointers to addresses (\typ{^{pUchAddress}}) can be used anywhere a pointer is used. +A socket can have potentially two addresses (the one it is BoundTo and +the one it is ConnectedTo. Once given to a socket, an \typ{UchAddress} +instance pointer is no more usable because it can be deleted and +reallocated by the Socket when it is bound/connected. These address +pointers are also deleted by the socket destructor. Thus, to know the +address of a socket always use \fun{BoundTo} and \fun{ConnectedTo}. ?*/ /*? -Construct a closed socket bound to address \var{bound} and connected to address \var{connected}. -Each or both arguments may be 0. +Construct a closed socket bound to address \var{bound} and connected +to address \var{connected}. Each or both arguments may be 0. These +addresses have to be freshly allocated objects and will +be deleted by the socket's destructor so it's not +necessary (and dangerous) to delete them. +always allocated by new. Theses addresses are deleted and +reinstanciated after a \fun{Bind}. To obtain the addresses of a +socket, use \fun{BoundTo} and \fun{ConnectedTo}. ?*/ UchSocket :: UchSocket (UchAddress* bound, UchAddress* connected) : UchChannel (), @@ -58,9 +69,8 @@ UchSocket :: UchSocket (const UchSocket& s) /*?nodoc?*/ UchSocket :: ~UchSocket () { - // will unreference addresses and destroy them if necessary - BAddr = 0; - CAddr = 0; + if (BAddr) delete BAddr; + if (CAddr) delete CAddr; } #ifdef DOC @@ -79,6 +89,8 @@ UchSocket :: SockType () void UchSocket :: BindTo (UchAddress* a) { + if (BAddr) + delete BAddr; BAddr = a; } @@ -88,6 +100,8 @@ Set the address a socket is to be bound to or connected to. void UchSocket :: ConnectTo (UchAddress* a) { + if (CAddr) + delete CAddr; CAddr = a; } @@ -123,11 +137,12 @@ int UchSocket :: Bind (UchAddress* addr) { if (addr) - BAddr = addr; + BindTo (addr); if (! BAddr || ! BAddr->IsValid ()) return -1; if (! Open ()) return -1; + int ret = bind (Fd, BAddr->GetSockAddr (), BAddr->Length ()); if (ret < 0) return ret; @@ -136,7 +151,7 @@ UchSocket :: Bind (UchAddress* addr) int alen = sizeof (naddr); if (getsockname (Fd, &naddr.sa, &alen) < 0) return -1; - BAddr = UchAddress::Decode (&naddr, alen); + BindTo (UchAddress::Decode (&naddr, alen)); return ret; } @@ -151,7 +166,7 @@ int UchSocket :: Connect (UchAddress* addr) { if (addr) - CAddr = addr; + ConnectTo (addr); if (! CAddr || ! CAddr->IsValid ()) return -1; if (! Open ()) @@ -164,7 +179,7 @@ UchSocket :: Connect (UchAddress* addr) int alen = sizeof (naddr); if (getpeername (Fd, &naddr.sa, &alen) < 0) return -1; - CAddr = UchAddress::Decode (&naddr, alen); + ConnectTo (UchAddress::Decode (&naddr, alen)); return ret; } @@ -184,12 +199,12 @@ UchSocket :: Setup () return Ready = false; if (BAddr && BAddr->IsValid ()) - if (Bind () < 0) - return Ready = false; + if (Bind () < 0) + return Ready = false; if (CAddr && CAddr->IsValid ()) - if (Connect () < 0) - return Ready = false; + if (Connect () < 0) + return Ready = false; return Ready = true; } @@ -205,6 +220,23 @@ UchSocket :: Accept () } +bool +UchSocket :: ReuseAddress (bool on) +{ + int parm = on; + int res = setsockopt (Fd, SOL_SOCKET, SO_REUSEADDR, (char *)&parm, sizeof (parm)); + return res < 0 ? false : true; +} + +bool +UchSocket :: AllowBroadcast (bool on) +{ + int parm = on; + int res = setsockopt (Fd, SOL_SOCKET, SO_BROADCAST, (char *)&parm, sizeof (parm)); + return res < 0 ? false : true; +} + + /*?nodoc?*/ char* UchSocket :: StrRepr (char* buf) @@ -212,12 +244,12 @@ UchSocket :: StrRepr (char* buf) UchChannel :: StrRepr (buf); strcat (buf, " / "); if (BAddr) - BAddr -> StrRepr (buf + strlen (buf)); + BAddr->StrRepr (buf + strlen (buf)); else strcat (buf, "null"); strcat (buf, " / "); if (CAddr) - CAddr -> StrRepr (buf + strlen (buf)); + CAddr->StrRepr (buf + strlen (buf)); else strcat (buf, "null"); return buf; -- cgit v1.1