SocketPtr Socket::Accept() { if(!IsConnected()) return SocketPtr(); struct sockaddr_in remote_address; unsigned int address_length = sizeof(remote_address); #ifdef _WIN32 int new_client_socket = accept(m_native_socket, (struct sockaddr*) &remote_address, (int *)&address_length); #else int new_client_socket = accept(m_native_socket, (struct sockaddr*) &remote_address, &address_length); #endif __update_last_error(); if(new_client_socket < 0) return SocketPtr(); SocketPtr client_socket(new Socket(m_protocol, new_client_socket, remote_address)); client_socket->m_timeout_milliseconds = m_timeout_milliseconds; client_socket->m_socket_role = SocketRole::Client; client_socket->__update_remote_host(); return client_socket; }
SocketPtr ServerSocket::AcceptConnection() const { assert(socketId != NET_INVALID_SOCKET_ID); errno = 0; const NetSocketId newSocket = accept(socketId, nullptr, nullptr); if (newSocket == NET_INVALID_SOCKET_ID) { // Non-blocking call / no request pending #ifdef _WIN32 if (WSAGetLastError() == WSAEWOULDBLOCK) { return nullptr; } #else // !_WIN32 if (errno == EAGAIN) { return nullptr; } #endif // _WIN32 throw SocketError("'accept()' failed with an invalid socketId!"); } return SocketPtr(new Socket(newSocket)); }
SocketPtr WorldSocketMgr::CreateSocket( NetworkThread& owner ) { return SocketPtr( new WorldSocket( *this, owner ) ); }
SocketPtr RemoteAdminSocketMgr::CreateSocket(NetworkThread& owner) { return SocketPtr(new RASocket(*this, owner)); }
void HttpTaskRunnable::run() { m_tcpSocket = SocketPtr(new QTcpSocket); if (!m_tcpSocket->setSocketDescriptor(m_socketDescriptor, QAbstractSocket::ConnectingState)) { return; } QByteArray buff; buff.reserve(HTTP_MAX_HEADER_SIZE); if(!m_tcpSocket->waitForReadyRead(5000)) { m_tcpSocket->close(); return; } int attempts = 0; size_t bodySize = 0; forever { if(m_tcpSocket->state() == QAbstractSocket::ClosingState) { return; } if(!m_tcpSocket->canReadLine()) { m_tcpSocket->waitForReadyRead(5000); ++attempts; if(attempts==3) { m_tcpSocket->close(); return; } continue; } QByteArray line = m_tcpSocket->readLine(); buff.append(line); line.chop(2); auto header = line.split(':'); if(header.count()==2) { if(header[0]=="Content-Length") { bodySize = header[1].mid(1, header[1].size()-1).toUInt(); } } if(line.isEmpty()) { break; } } if(bodySize) { QByteArray body; while(body.size()!=bodySize) { body.append(m_tcpSocket->readAll()); } if(m_tcpSocket->state() == QAbstractSocket::ClosingState) { return; } buff.append(body); buff.append('\r'); buff.append('\n'); } parseRequest(buff); callCalback(); m_tcpSocket->flush(); m_tcpSocket->close(); }
void LuaDebug::async_accept() { SocketPtr s = SocketPtr(new ip::tcp::socket(mIos)); mAcceptor.async_accept(*s,boost::bind(&LuaDebug::accept_handler,this,placeholders::error,s)); }