CArchSocket CArchNetworkBSD::acceptSocket(CArchSocket s, CArchNetAddress* addr) { assert(s != NULL); // if user passed NULL in addr then use scratch space CArchNetAddress dummy; if (addr == NULL) { addr = &dummy; } // create new socket and address CArchSocketImpl* newSocket = new CArchSocketImpl; *addr = new CArchNetAddressImpl; // accept on socket ACCEPT_TYPE_ARG3 len = (ACCEPT_TYPE_ARG3)((*addr)->m_len); int fd = accept(s->m_fd, &(*addr)->m_addr, &len); (*addr)->m_len = (socklen_t)len; if (fd == -1) { int err = errno; delete newSocket; delete *addr; *addr = NULL; if (err == EAGAIN) { return NULL; } throwError(err); } try { setBlockingOnSocket(fd, false); } catch (...) { close(fd); delete newSocket; delete *addr; *addr = NULL; throw; } // initialize socket newSocket->m_fd = fd; newSocket->m_refCount = 1; // discard address if not requested if (addr == &dummy) { ARCH->closeAddr(dummy); } return newSocket; }
CArchSocket CArchNetworkWinsock::acceptSocket(CArchSocket s, CArchNetAddress* addr) { assert(s != NULL); // create new socket and temporary address CArchSocketImpl* socket = new CArchSocketImpl; CArchNetAddress tmp = CArchNetAddressImpl::alloc(sizeof(struct sockaddr)); // accept on socket SOCKET fd = accept_winsock(s->m_socket, &tmp->m_addr, &tmp->m_len); if (fd == INVALID_SOCKET) { int err = getsockerror_winsock(); delete socket; free(tmp); *addr = NULL; if (err == WSAEWOULDBLOCK) { return NULL; } throwError(err); } try { setBlockingOnSocket(fd, false); } catch (...) { close_winsock(fd); delete socket; free(tmp); *addr = NULL; throw; } // initialize socket socket->m_socket = fd; socket->m_refCount = 1; socket->m_event = WSACreateEvent_winsock(); socket->m_pollWrite = true; // copy address if requested if (addr != NULL) { *addr = ARCH->copyAddr(tmp); } free(tmp); return socket; }
CArchSocket CArchNetworkBSD::newSocket(EAddressFamily family, ESocketType type) { // create socket int fd = socket(s_family[family], s_type[type], 0); if (fd == -1) { throwError(errno); } try { setBlockingOnSocket(fd, false); } catch (...) { close(fd); throw; } // allocate socket object CArchSocketImpl* newSocket = new CArchSocketImpl; newSocket->m_fd = fd; newSocket->m_refCount = 1; return newSocket; }
const int* CArchNetworkBSD::getUnblockPipeForThread(CArchThread thread) { CArchMultithreadPosix* mt = CArchMultithreadPosix::getInstance(); int* unblockPipe = (int*)mt->getNetworkDataForThread(thread); if (unblockPipe == NULL) { unblockPipe = new int[2]; if (pipe(unblockPipe) != -1) { try { setBlockingOnSocket(unblockPipe[0], false); mt->setNetworkDataForCurrentThread(unblockPipe); } catch (...) { delete[] unblockPipe; unblockPipe = NULL; } } else { delete[] unblockPipe; unblockPipe = NULL; } } return unblockPipe; }
CArchSocket CArchNetworkWinsock::newSocket(EAddressFamily family, ESocketType type) { // create socket SOCKET fd = socket_winsock(s_family[family], s_type[type], 0); if (fd == INVALID_SOCKET) { throwError(getsockerror_winsock()); } try { setBlockingOnSocket(fd, false); } catch (...) { close_winsock(fd); throw; } // allocate socket object CArchSocketImpl* socket = new CArchSocketImpl; socket->m_socket = fd; socket->m_refCount = 1; socket->m_event = WSACreateEvent_winsock(); socket->m_pollWrite = true; return socket; }