summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchatty2000-11-28 17:07:45 +0000
committerchatty2000-11-28 17:07:45 +0000
commitbe47ddbcc65b100c3634b5a4c35a89c8eac94a7e (patch)
tree5db5dd2c890ede8d2f8ae267d0f98cb58ceeec26
parentf0540b018ae6c266126162a96abf70a4c7a2f53a (diff)
downloadivy-league-be47ddbcc65b100c3634b5a4c35a89c8eac94a7e.zip
ivy-league-be47ddbcc65b100c3634b5a4c35a89c8eac94a7e.tar.gz
ivy-league-be47ddbcc65b100c3634b5a4c35a89c8eac94a7e.tar.bz2
ivy-league-be47ddbcc65b100c3634b5a4c35a89c8eac94a7e.tar.xz
* Removed Smart pointers
* Added ReuseAddress and AllowBroadcast
-rw-r--r--comm/Socket.cc82
-rw-r--r--comm/Socket.h14
2 files changed, 65 insertions, 31 deletions
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 <stdlib.h>
#include <string.h>
#include <sys/socket.h>
-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;
diff --git a/comm/Socket.h b/comm/Socket.h
index b534a90..7c0e323 100644
--- a/comm/Socket.h
+++ b/comm/Socket.h
@@ -3,7 +3,7 @@
*
* by Michel Beaudouin-Lafon
*
- * Copyright 1990-1993
+ * Copyright 1990-1997
* Laboratoire de Recherche en Informatique (LRI)
*
* Sockets
@@ -19,7 +19,7 @@
#include "Channel.h"
#include "Address.h"
-extern char* StrReprBuf;
+extern char* StrReprBuf;
#define SOCK_UNSPEC 0
@@ -29,15 +29,15 @@ extern char* StrReprBuf;
//
class UchSocket : public UchChannel {
protected:
- pUchAddress BAddr;
- pUchAddress CAddr;
+ UchAddress* BAddr;
+ UchAddress* CAddr;
int AddrFamily;
- bool Ready;
+ bool Ready;
/*?public?*/
UchSocket (UchAddress*, UchAddress*);
- ~UchSocket ();
UchSocket (const UchSocket& s);
+ ~UchSocket ();
public:
@@ -54,6 +54,8 @@ inline UchAddress* ConnectedTo () { return CAddr; }
int Connect (UchAddress* = 0);
int Accept ();
bool Setup ();
+ bool ReuseAddress (bool = true);
+ bool AllowBroadcast (bool = true);
char* StrRepr (char* = StrReprBuf);
};