summaryrefslogtreecommitdiff
path: root/comm
diff options
context:
space:
mode:
authorchatty1994-05-10 11:17:28 +0000
committerchatty1994-05-10 11:17:28 +0000
commitcca1cb8ea9a36199c399f2c30b55e6dfbc9efdd4 (patch)
tree5a0a20f45957837c0ed5cf3fa3f03ca212388a7c /comm
parent829aaaec22657d1e4da5283d938653988e3091b8 (diff)
downloadivy-league-cca1cb8ea9a36199c399f2c30b55e6dfbc9efdd4.zip
ivy-league-cca1cb8ea9a36199c399f2c30b55e6dfbc9efdd4.tar.gz
ivy-league-cca1cb8ea9a36199c399f2c30b55e6dfbc9efdd4.tar.bz2
ivy-league-cca1cb8ea9a36199c399f2c30b55e6dfbc9efdd4.tar.xz
Moved Accept from UchChannel
Removed useless default constructor replaced TRUE/FALSE by true/false Ok -> Ready
Diffstat (limited to 'comm')
-rw-r--r--comm/Socket.cc137
1 files changed, 66 insertions, 71 deletions
diff --git a/comm/Socket.cc b/comm/Socket.cc
index 2021aff..998514b 100644
--- a/comm/Socket.cc
+++ b/comm/Socket.cc
@@ -17,9 +17,20 @@
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
-
extern int errno;
+#ifdef __osf__
+extern "C" {
+ int socket (int, int, int);
+ int bind (int, struct sockaddr*, int);
+ int getsockname (int, struct sockaddr*, int*);
+ int connect (int, struct sockaddr*, int);
+ int getpeername (int, struct sockaddr*, int*);
+ int accept (int, struct sockaddr*, int*);
+}
+#endif
+
+
/*?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.
@@ -34,17 +45,6 @@ Smart pointers to addresses (\typ{^{pUchAddress}}) can be used anywhere a pointe
?*/
/*?
-Construct a closed socket.
-?*/
-UchSocket :: UchSocket ()
-: UchChannel (),
- BAddr (0),
- CAddr (0),
- AddrFamily (AF_UNSPEC)
-{
-}
-
-/*?
Construct a closed socket bound to address \var{bound} and connected to address \var{connected}.
Each or both arguments may be 0.
?*/
@@ -52,7 +52,8 @@ UchSocket :: UchSocket (UchAddress* bound, UchAddress* connected)
: UchChannel (),
BAddr (bound),
CAddr (connected),
- AddrFamily (AF_UNSPEC)
+ AddrFamily (AF_UNSPEC),
+ Ready (false)
{
}
@@ -60,7 +61,8 @@ UchSocket :: UchSocket (UchAddress* bound, UchAddress* connected)
UchSocket :: UchSocket (const UchSocket& s)
: UchChannel (s),
BAddr (s.BAddr),
- CAddr (s.CAddr)
+ CAddr (s.CAddr),
+ Ready (s.Ready)
{
}
@@ -72,13 +74,7 @@ UchSocket :: ~UchSocket ()
CAddr = 0;
}
-/*?nodoc?*/
-UchChannel*
-UchSocket :: Copy () const
-{
- return new UchSocket (*this);
-}
-
+#ifdef DOC
/*?
This virtual function returns the type of the socket:
one of \var{SOCK\_UNSPEC}, \var{SOCK\_STREAM}, \var{SOCK\_DGRAM},
@@ -87,8 +83,8 @@ depending on the class.
int
UchSocket :: SockType ()
{
- return SOCK_UNSPEC;
}
+#endif
/*?nextdoc?*/
void
@@ -110,47 +106,27 @@ UchSocket :: ConnectTo (UchAddress* a)
Open the socket (with the \fun{socket} system call) if it is not already open.
The socket must have an address bound or connected to get the protocol family,
or its family must have been defined with \fun{SetFamily}.
-Return FALSE if the family is undefined or if a system error occurred.
+Return false if the family is undefined or if a system error occurred.
?*/
bool
UchSocket :: Open ()
{
errno = 0;
- if (FilDes () >= 0)
- return TRUE;
+ if (Fd >= 0)
+ return true;
if (AddrFamily == AF_UNSPEC) {
if (BAddr)
AddrFamily = BAddr->Family ();
else if (CAddr)
AddrFamily = CAddr->Family ();
else
- return FALSE;
+ return false;
}
int fd = socket (AddrFamily, SockType (), 0);
if (fd < 0)
- return FALSE;
+ return false;
UchChannel::Open (fd);
- return TRUE;
-}
-
-/*?nextdoc?*/
-int
-UchSocket :: Bind ()
-{
- if (! BAddr || ! BAddr->IsValid ())
- return -1;
- if (! Open ())
- return -1;
- int ret = bind (FilDes (), BAddr->GetSockAddr (), BAddr->Length ());
- if (ret < 0)
- return ret;
-
- GEN_ADDR addr;
- int alen = sizeof (addr);
- if (getsockname (FilDes (), &addr.sa, &alen) < 0)
- return -1;
- BAddr = UchAddress::Decode (&addr, alen);
- return ret;
+ return true;
}
/*?nextdoc?*/
@@ -159,26 +135,19 @@ UchSocket :: Bind (UchAddress* addr)
{
if (addr)
BAddr = addr;
- return Bind ();
-}
-
-/*?nextdoc?*/
-int
-UchSocket :: Connect ()
-{
- if (! CAddr || ! CAddr->IsValid ())
+ if (! BAddr || ! BAddr->IsValid ())
return -1;
if (! Open ())
return -1;
- int ret = connect (FilDes (), CAddr->GetSockAddr (), CAddr->Length ());
+ int ret = bind (Fd, BAddr->GetSockAddr (), BAddr->Length ());
if (ret < 0)
return ret;
- GEN_ADDR addr;
- int alen = sizeof (addr);
- if (getpeername (FilDes (), &addr.sa, &alen) < 0)
+ GEN_ADDR naddr;
+ int alen = sizeof (naddr);
+ if (getsockname (Fd, &naddr.sa, &alen) < 0)
return -1;
- CAddr = UchAddress::Decode (&addr, alen);
+ BAddr = UchAddress::Decode (&naddr, alen);
return ret;
}
@@ -194,33 +163,59 @@ UchSocket :: Connect (UchAddress* addr)
{
if (addr)
CAddr = addr;
- return Connect ();
+ if (! CAddr || ! CAddr->IsValid ())
+ return -1;
+ if (! Open ())
+ return -1;
+ int ret = connect (Fd, CAddr->GetSockAddr (), CAddr->Length ());
+ if (ret < 0)
+ return ret;
+
+ GEN_ADDR naddr;
+ int alen = sizeof (naddr);
+ if (getpeername (Fd, &naddr.sa, &alen) < 0)
+ return -1;
+ CAddr = UchAddress::Decode (&naddr, alen);
+ return ret;
}
+
/*?
Open the socket if it is not already open.
Bind and connect it depending on the addresses that are defined.
You should use this function instead of calling \fun{Open}, \fun{Bind} and
\fun{Connect}, for it is simpler.
-This function returns FALSE if a system error occurred. In this case, the caller can
+This function returns false if a system error occurred. In this case, the caller can
call \fun{SysError} to report the error.
?*/
bool
UchSocket :: Setup ()
{
if (! Open ())
- return FALSE;
- if (BAddr)
- if (BAddr->IsValid ())
+ return Ready = false;
+
+ if (BAddr && BAddr->IsValid ())
if (Bind () < 0)
- return FALSE;
- if (CAddr)
- if (CAddr->IsValid ())
+ return Ready = false;
+
+ if (CAddr && CAddr->IsValid ())
if (Connect () < 0)
- return FALSE;
- return TRUE;
+ return Ready = false;
+ return Ready = true;
+}
+
+/*?
+This function is intended for sockets that accept connections.
+It returns the file descriptor of the new connection, or -1 in case of failure.
+?*/
+int
+UchSocket :: Accept ()
+{
+ errno = 0;
+ return accept (Fd, 0, 0);
}
+
/*?nodoc?*/
char*
UchSocket :: StrRepr (char* buf)