6 #define _WINSOCK_DEPRECATED_NO_WARNINGS
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
12 #include <sys/socket.h>
15 #if defined(_AIX) || defined(__sun__)
21 #include <sys/filio.h>
32#if __DCL_HAVE_THIS_FILE__
39 #define SOCKET_TYPE UINT_PTR
40 #define INVALID_SOCKET (UINT_PTR) -1
41 #define __ERRNO WSAGetLastError()
43 #define SOCKET_TYPE int
44 #define INVALID_HANDLE_VALUE -1
45 #define INVALID_SOCKET -1
46 #define closesocket(s) close(s)
47 #define ioctlsocket(s, cmd, argp) ioctl(s, cmd, argp)
67 socklen_t n =
sizeof(addr);
69 String foreign = addr.toString();
70 if (!foreign.isEmpty()) {
73 r +=
__T(
" foreign ") + foreign;
81void Socket::open(
const String& _addr, uint16_t _port)
87 Addr addr(_addr, _port);
94 unsigned long arg = 0;
101size_t Socket::read(
void* _buf,
size_t _n)
104 return recv(_buf, _n, 0);
107size_t Socket::write(
const void* _buf,
size_t _n)
110 return send(_buf, _n, 0);
115 memset((
void*)
this, 0,
sizeof(Addr));
118static void __init_inet_addr(Socket::Addr* _dest,
const char* _addr, uint16_t _port)
121 if (*_addr !=
'\0') {
122 if (strchr(_addr,
':') &&
123 inet_pton(AF_INET6, _addr, &(_dest->sa_in6.sin6_addr)) > 0) {
124 _dest->sa_family = AF_INET6;
125 _dest->sa_in6.sin6_port = htons(_port);
128 else if(inet_pton(AF_INET, _addr, &(_dest->sa_in.sin_addr)) > 0) {
129 _dest->sa_family = AF_INET;
130 _dest->sa_in.sin_port = htons(_port);
135 struct hostent* ent = gethostbyname(_addr);
139 ent->h_name ? ent->h_name :
"(null)",
140 ent->h_aliases ? ent->h_aliases[0] :
"(null)",
141 ent->h_addrtype, ent->h_length
144 _dest->sa_family = ent->h_addrtype;
145 switch (_dest->sa_family) {
148 char** list = ent->h_addr_list;
149 for (
int i = 0; list[i] !=
NULL; i++) {
151 for (
int j = 0; j < ent->h_length; j++) {
154 printf(
"%u", (
unsigned char) list[i][j]);
159 _dest->sa_in.sin_addr.s_addr = *(u_long*) ent->h_addr_list[0];
160 _dest->sa_in.sin_port = htons(_port);
164 memcpy(&(_dest->sa_in6), ent->h_addr_list[0], ent->h_length);
165 _dest->sa_in6.sin6_port = htons(_port);
176 String s = dec.
decode(_addr);
177 throw new IOException(s,
186Socket::Addr::Addr(
const char *_addr, uint16_t _port)
190 memset((
void*)
this, 0,
sizeof(Addr));
192 __init_inet_addr(
this, _addr, _port);
195Socket::Addr::Addr(
const String& _addr, uint16_t _port)
198 memset((
void*)
this, 0,
sizeof(Addr));
203 addr = enc.encode(_addr);
205 catch (CharsetConvertException* cause) {
206 throw new IOException(_addr, cause);
209 __init_inet_addr(
this, addr, _port);
213Socket::Addr::Addr(
const char* _path)
216 memset((
void*)
this, 0,
sizeof(Addr));
219 size_t n = ByteString::length(_path);
220 if (n >
sizeof(sa_un.sun_path)) {
224 sa_un.sun_family = AF_UNIX;
225 strncpy(sa_un.sun_path, _path, n);
228Socket::Addr::Addr(
const String& _path)
231 memset((
void*)
this, 0,
sizeof(Addr));
236 throw new IOException(_path, EILSEQ);
238 if (nmbs >
sizeof(sa_un.sun_path))
239 throw new IOException(_path, ENAMETOOLONG);
241 sa_un.sun_family = AF_UNIX;
242 strncpy(sa_un.sun_path, mbs, nmbs);
246String Socket::Addr::toString()
const
252 strncpy(buf, sa_un.sun_path,
__countof(sa_un.sun_path,
char));
259 if (inet_ntop(AF_INET, (
void*) &(sa_in.sin_addr), buf,
sizeof(buf))) {
261 StringBuilder
r = dec.
decode(buf);
269 if (inet_ntop(AF_INET6, (
void*) &(sa_in6.sin6_addr), buf,
sizeof(buf))) {
271 StringBuilder
r = dec.
decode(buf);
272 if (sa_in6.sin6_flowinfo) {
287 return String(
__T(
"Invalid !!"));
290void Socket::bind(
const Addr& _addr,
int _type,
int _protocol,
bool _reuse)
294 create(_addr.sa_family, _type, _protocol);
296 socklen_t n =
sizeof(_addr);
297 switch (_addr.sa_family) {
300 n =
sizeof(_addr.sa_un);
305 n =
sizeof(_addr.sa_in);
309 n =
sizeof(_addr.sa_in6);
321 SOL_SOCKET, SO_REUSEADDR, (
const char*)&reuse,
sizeof(reuse)
323 throw new IOException(
__path, WSAGetLastError());
328 SOL_SOCKET, SO_REUSEADDR, (
const void*)&reuse,
sizeof(reuse)
330 throw new IOException(
__path, errno);
335 bind((
const sockaddr*) &_addr, n);
338void Socket::accept(Socket& _r)
342 socklen_t n =
sizeof(addr);
343 accept(_r, (
struct sockaddr*) &addr, &n);
346void Socket::connect(
const Addr& _addr)
350 create(_addr.sa_family, SOCK_STREAM, IPPROTO_TCP);
352 socklen_t n =
sizeof(_addr);
353 switch (_addr.sa_family) {
356 n =
sizeof(_addr.sa_un);
361 n =
sizeof(_addr.sa_in);
365 n =
sizeof(_addr.sa_in6);
373 connect((
const sockaddr*) &_addr, n);
376socklen_t Socket::getsockname(Addr& _addr)
380 socklen_t n =
sizeof(_addr);
386socklen_t Socket::getpeername(Addr& _addr)
390 socklen_t n =
sizeof(_addr);
400 __waitEvent = WSA_INVALID_EVENT;
404Socket::Socket(
const String& _addr, uint16_t _port)
409 __waitEvent = WSA_INVALID_EVENT;
420 catch (IOException* cause) {
426 if (__waitEvent != WSA_INVALID_EVENT) {
427 WSACloseEvent(__waitEvent);
428 __waitEvent = WSA_INVALID_EVENT;
447#if defined(_MSC_VER) && !defined(__DCL_CORE_EXPORTS)
448 #pragma optimize ( "", off )
449 #pragma warning ( push )
450 #pragma warning ( disable : 4748 )
453void Socket::create(
int _domain,
int _type,
int _protocol)
464void Socket::setNonblock()
469 unsigned long arg = 1;
474void Socket::bind(
const struct sockaddr* _my_addr, socklen_t _addrlen)
484 __path = raddr.toString();
487void Socket::listen(
unsigned _backlog)
496void Socket::accept(Socket&
r,
struct sockaddr* _addr, socklen_t* _addrlen)
507 r.__handle = (HandleType)
handle;
509 r.getsockname(raddr);
513void Socket::connect(
const sockaddr* _serv_addr, socklen_t _addrlen)
523 __path = raddr.toString();
526size_t Socket::send(
const void* _buf,
size_t _n,
int _flags)
534 if (n == SOCKET_ERROR) {
536 if (WSAGetLastError() == WSAEWOULDBLOCK)
539 throw new IOException(
toString(), WSAGetLastError());
546 if (errno == EAGAIN || errno == EWOULDBLOCK)
549 throw new IOException(
toString(), errno);
556size_t Socket::recv(
void* _buf,
size_t _n,
int _flags)
564 if (n == SOCKET_ERROR) {
566 if (WSAGetLastError() == WSAEWOULDBLOCK)
569 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)
#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