10 #include <sys/ioctl.h>
11 #include <sys/socket.h>
32 #define SOCKET_TYPE UINT_PTR
33 #define INVALID_SOCKET (UINT_PTR) -1
34 #define __ERRNO WSAGetLastError()
36 #define SOCKET_TYPE int
37 #define INVALID_HANDLE_VALUE -1
38 #define INVALID_SOCKET -1
39 #define closesocket(s) close(s)
40 #define ioctlsocket(s, cmd, argp) ioctl(s, cmd, argp)
60 socklen_t
n =
sizeof(addr);
62 String foreign = addr.toString();
63 if (!foreign.isEmpty()) {
66 r +=
__T(
" foreign ") + foreign;
74void Socket::open(
const String& _addr, uint16_t _port)
80 Addr addr(_addr, _port);
94size_t Socket::read(
void* _buf,
size_t _n)
97 return recv(_buf, _n, 0);
100size_t Socket::write(
const void* _buf,
size_t _n)
103 return send(_buf, _n, 0);
108 memset((
void*)
this, 0,
sizeof(Addr));
111static void __init_inet_addr(Socket::Addr* _dest,
const char* _addr, uint16_t _port)
114 if (*_addr !=
'\0') {
115 if (strchr(_addr,
':') &&
116 inet_pton(AF_INET6, _addr, &(_dest->sa_in6.sin6_addr)) > 0) {
117 _dest->sa_family = AF_INET6;
118 _dest->sa_in6.sin6_port = htons(_port);
121 else if(inet_pton(AF_INET, _addr, &(_dest->sa_in.sin_addr)) > 0) {
122 _dest->sa_family = AF_INET;
123 _dest->sa_in.sin_port = htons(_port);
128 struct hostent* ent = gethostbyname(_addr);
132 ent->h_name ? ent->h_name :
"(null)",
133 ent->h_aliases ? ent->h_aliases[0] :
"(null)",
134 ent->h_addrtype, ent->h_length
137 _dest->sa_family = ent->h_addrtype;
138 switch (_dest->sa_family) {
142 char** list = ent->h_addr_list;
143 for (
int i = 0; list[i] !=
NULL; i++) {
145 for (
int j = 0; j < ent->h_length; j++) {
148 printf(
"%u", (
unsigned char) list[i][j]);
154 _dest->sa_in.sin_addr.s_addr = *(u_long*) ent->h_addr_list[0];
155 _dest->sa_in.sin_port = htons(_port);
159 memcpy(&(_dest->sa_in6), ent->h_addr_list[0], ent->h_length);
160 _dest->sa_in6.sin6_port = htons(_port);
171 String s = dec.
decode(_addr);
172 throw new IOException(s,
181Socket::Addr::Addr(
const char *_addr, uint16_t _port)
185 memset((
void*)
this, 0,
sizeof(Addr));
187 __init_inet_addr(
this, _addr, _port);
190Socket::Addr::Addr(
const String& _addr, uint16_t _port)
193 memset((
void*)
this, 0,
sizeof(Addr));
198 addr = enc.encode(_addr);
200 catch (CharsetConvertException* cause) {
201 throw new IOException(_addr, cause);
204 __init_inet_addr(
this, addr, _port);
208Socket::Addr::Addr(
const char* _path)
211 memset((
void*)
this, 0,
sizeof(Addr));
214 size_t n = ByteString::length(_path);
215 if (
n >
sizeof(sa_un.sun_path))
221 sa_un.sun_family = AF_UNIX;
222 strncpy(sa_un.sun_path, _path,
n);
225Socket::Addr::Addr(
const String& _path)
228 memset((
void*)
this, 0,
sizeof(Addr));
233 throw new IOException(_path, EILSEQ);
235 if (nmbs >
sizeof(sa_un.sun_path))
236 throw new IOException(_path, ENAMETOOLONG);
238 sa_un.sun_family = AF_UNIX;
239 strncpy(sa_un.sun_path, mbs, nmbs);
243String Socket::Addr::toString()
const
250 strncpy(
buf, sa_un.sun_path,
__countof(sa_un.sun_path,
char));
257 if (inet_ntop(AF_INET, (
void*) &(sa_in.sin_addr),
buf,
sizeof(
buf))) {
267 if (inet_ntop(AF_INET6, (
void*) &(sa_in6.sin6_addr),
buf,
sizeof(
buf))) {
270 if (sa_in6.sin6_flowinfo) {
285 return String(
__T(
"Invalid !!"));
288void Socket::bind(
const Addr& _addr,
int _type,
int _protocol,
bool _reuse)
292 create(_addr.sa_family, _type, _protocol);
294 socklen_t
n =
sizeof(_addr);
295 switch (_addr.sa_family) {
298 n =
sizeof(_addr.sa_un);
303 n =
sizeof(_addr.sa_in);
307 n =
sizeof(_addr.sa_in6);
319 SOL_SOCKET, SO_REUSEADDR, (
const char*) &reuse,
sizeof(reuse)
321 throw new IOException(
__path, WSAGetLastError());
325 SOL_SOCKET, SO_REUSEADDR, (
const void*) &reuse,
sizeof(reuse)))
326 throw new IOException(
__path, errno);
330 bind((
const sockaddr*) &_addr,
n);
333void Socket::accept(Socket&
_r)
337 socklen_t
n =
sizeof(addr);
338 accept(
_r, (
struct sockaddr*) &addr, &
n);
341void Socket::connect(
const Addr& _addr)
345 create(_addr.sa_family, SOCK_STREAM, IPPROTO_TCP);
347 socklen_t
n =
sizeof(_addr);
348 switch (_addr.sa_family) {
351 n =
sizeof(_addr.sa_un);
356 n =
sizeof(_addr.sa_in);
360 n =
sizeof(_addr.sa_in6);
368 connect((
const sockaddr*) &_addr,
n);
371socklen_t Socket::getsockname(Addr& _addr)
375 socklen_t
n =
sizeof(_addr);
381socklen_t Socket::getpeername(Addr& _addr)
385 socklen_t
n =
sizeof(_addr);
395 __waitEvent = WSA_INVALID_EVENT;
399Socket::Socket(
const String& _addr, uint16_t _port)
404 __waitEvent = WSA_INVALID_EVENT;
416 catch (IOException* cause) {
422 if (__waitEvent != WSA_INVALID_EVENT) {
423 WSACloseEvent(__waitEvent);
424 __waitEvent = WSA_INVALID_EVENT;
443#if defined(_MSC_VER) && !defined(__DCL_CORE_EXPORTS)
444 #pragma optimize ( "", off )
445 #pragma warning ( push )
446 #pragma warning ( disable : 4748 )
449void Socket::create(
int _domain,
int _type,
int _protocol)
460void Socket::setNonblock()
470void Socket::bind(
const struct sockaddr* _my_addr, socklen_t _addrlen)
480 __path = raddr.toString();
483void Socket::listen(
unsigned _backlog)
492void Socket::accept(Socket&
r,
struct sockaddr* _addr, socklen_t* _addrlen)
503 r.__handle = (HandleType)
handle;
505 r.getsockname(raddr);
506 r.__path = raddr.toString();
509void Socket::connect(
const sockaddr* _serv_addr, socklen_t _addrlen)
519 __path = raddr.toString();
522size_t Socket::send(
const void* _buf,
size_t _n,
int _flags)
530 if (
n == SOCKET_ERROR)
533 if (WSAGetLastError() == WSAEWOULDBLOCK)
536 throw new IOException(
toString(), WSAGetLastError());
544 if (errno == EAGAIN || errno == EWOULDBLOCK)
547 throw new IOException(
toString(), errno);
554size_t Socket::recv(
void* _buf,
size_t _n,
int _flags)
562 if (
n == SOCKET_ERROR)
565 if (WSAGetLastError() == WSAEWOULDBLOCK)
568 throw new IOException(
toString(), WSAGetLastError());
576 if (errno == EAGAIN || errno == EWOULDBLOCK)
579 throw new IOException(
toString(), errno);
586#if defined(_MSC_VER) && !defined(__DCL_CORE_EXPORTS)
587 #pragma warning ( pop )
588 #pragma optimize ( "", on )
#define STRTOMBS(str, mbs)
#define __countof(array, type)
#define INVALID_HANDLE_VALUE
#define __DCL_TRACE1(fmt, arg1)
#define __DCL_ASSERT_PARAM(expr)
#define __DCL_ASSERT(expr)
#define __DCL_TRACE4(fmt, arg1, arg2, arg3, arg4)
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
#define __DCL_ASSERT_HANDLE(expr)
void CharsetConvertException *size_t n
#define ioctlsocket(s, cmd, argp)
static String decode(const char *_mbs, size_t _mbslen=(size_t) -1)
HandleType handle() const
virtual void close() __DCL_THROWS1(IOException *)
virtual String toString() const
const String & path() const
virtual size_t available() const __DCL_THROWS1(IOException *)
virtual String toString() const
virtual bool onEvent(short _revents, PollThread *_pPollThread) __DCL_THROWS1(IOException *)
String toString(unsigned _base=10) const