11 #include <sys/socket.h> 13 #define setsockopt(a,b,c,d,e) setsockopt(a,b,c, (const char*) d,e) 14 #define recvfrom(a,b,c,d,e,f) recvfrom(a, (char *) b, c, d, e, f) 15 #define sendto(a,b,c,d,e,f) sendto(a,(const char*) b,c,d,e,f) 17 #define errno GetLastError() 23 # define socklen_t int 28 localaddr(), remaddr(_rem)
31 setfd(socket(PF_INET, SOCK_DGRAM, 0));
33 || setsockopt(
getfd(), SOL_SOCKET, SO_REUSEADDR, &x,
sizeof(x)) < 0)
42 struct sockaddr *sa = _local.sockaddr();
43 if (bind(
getfd(), sa, _local.sockaddr_len()))
51 struct sockaddr_in nsa;
52 socklen_t nsalen =
sizeof(nsa);
53 if (getsockname(
getfd(), (sockaddr *)&nsa, &nsalen) < 0)
62 struct sockaddr *sa = _rem.sockaddr();
63 if (connect(
getfd(), sa, _rem.sockaddr_len()))
74 WvUDPStream::~WvUDPStream()
85 const WvAddr *WvUDPStream::local()
const 93 if (!
isok() || !buf || !count)
return 0;
95 struct sockaddr_in from;
96 socklen_t fromlen =
sizeof(from);
97 int in = recvfrom(
getfd(), buf, count, 0, (sockaddr *)&from, &fromlen);
103 return in < 0 ? 0 : in;
109 if (!
isok() || !buf || !count)
return 0;
112 if (remaddr.is_zero())
return count;
114 struct sockaddr *to = remaddr.sockaddr();
115 size_t tolen = remaddr.sockaddr_len();
118 out = sendto(
getfd(), buf, count, 0, to, tolen);
120 if (out < 0 && errno == EACCES)
129 return out < 0 ? 0 : out;
133 void WvUDPStream::enable_broadcasts()
139 setsockopt(
getfd(), SOL_SOCKET, SO_BROADCAST, &value,
sizeof(value));
void setfd(int fd)
Sets the file descriptor for both reading and writing.
virtual size_t uread(void *buf, size_t count)
unbuffered I/O functions; these ignore the buffer, which is handled by read().
void set_close_on_exec(bool close_on_exec)
Make the fds on this stream close-on-exec or not.
An IP+Port address also includes a port number, with the resulting form www.xxx.yyy.zzz:pppp.
virtual const WvAddr * src() const
return the remote address (source of incoming packets, target of outgoing packets).
An IP address is made up of a "dotted quad" – four decimal numbers in the form www.xxx.yyy.zzz.
int getfd() const
Returns the Unix file descriptor for reading and writing.
virtual size_t uwrite(const void *buf, size_t count)
unbuffered I/O functions; these ignore the buffer, which is handled by write().
virtual bool isok() const
return true if the stream is actually usable right now
void set_nonblock(bool nonblock)
Make the fds on this stream blocking or non-blocking.
virtual void seterr(int _errnum)
Override seterr() from WvError so that it auto-closes the stream.
WvUDPStream(const WvIPPortAddr &_local, const WvIPPortAddr &_rem)
connect a new socket
Base class for different address types, each of which will have the ability to convert itself to/from...