5 #include <msp/core/systemerror.h>
6 #include <msp/io/handle_private.h>
7 #include <msp/strings/format.h>
8 #include "datagramsocket.h"
9 #include "sockaddr_private.h"
10 #include "socket_private.h"
15 DatagramSocket::DatagramSocket(Family f, int p):
16 ClientSocket(f, SOCK_DGRAM, p)
19 WSAEventSelect(priv->handle, *priv->event, FD_READ|FD_CLOSE);
21 set_events(IO::P_INPUT);
24 bool DatagramSocket::connect(const SockAddr &addr)
26 SockAddr::SysAddr sa = addr.to_sys();
28 int err = ::connect(priv->handle, reinterpret_cast<sockaddr *>(&sa.addr), sa.size);
32 throw system_error("connect", WSAGetLastError());
34 throw system_error("connect");
39 peer_addr = addr.copy();
42 SockAddr::SysAddr lsa;
43 getsockname(priv->handle, reinterpret_cast<sockaddr *>(&lsa.addr), &lsa.size);
44 local_addr = SockAddr::new_from_sys(lsa);
51 unsigned DatagramSocket::sendto(const char *buf, unsigned size, const SockAddr &addr)
56 SockAddr::SysAddr sa = addr.to_sys();
58 int ret = ::sendto(priv->handle, buf, size, 0, reinterpret_cast<sockaddr *>(&sa.addr), sa.size);
66 throw system_error("sendto", WSAGetLastError());
68 throw system_error("sendto");
76 unsigned DatagramSocket::recvfrom(char *buf, unsigned size, SockAddr *&from_addr)
82 int ret = ::recvfrom(priv->handle, buf, size, 0, reinterpret_cast<sockaddr *>(&sa.addr), &sa.size);
90 throw system_error("recvfrom", WSAGetLastError());
92 throw system_error("recvfrom");
97 from_addr = SockAddr::new_from_sys(sa);