| 18 | } |
| 19 | |
| 20 | TcpSocketPtr TcpSocket::accept() { |
| 21 | ReadLocker locker(m_mutex); |
| 22 | |
| 23 | if (m_socketMode != SocketMode::Bound) |
| 24 | throw SocketClosedException("TcpSocket not bound in TcpSocket::accept"); |
| 25 | |
| 26 | struct sockaddr_storage sockAddr; |
| 27 | socklen_t sockAddrLen = sizeof(sockAddr); |
| 28 | |
| 29 | auto socketDesc = ::accept(m_impl->socketDesc, (struct sockaddr*)&sockAddr, &sockAddrLen); |
| 30 | |
| 31 | if (invalidSocketDescriptor(socketDesc)) { |
| 32 | if (netErrorInterrupt()) |
| 33 | return {}; |
| 34 | throw NetworkException(strf("Cannot accept connection: {}", netErrorString())); |
| 35 | } |
| 36 | |
| 37 | auto socketImpl = make_shared<SocketImpl>(); |
| 38 | socketImpl->socketDesc = socketDesc; |
| 39 | |
| 40 | #if defined STAR_SYSTEM_MACOS || defined STAR_SYSTEM_FREEBSD |
| 41 | // Don't generate sigpipe |
| 42 | int set = 1; |
| 43 | socketImpl->setSockOpt(SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int)); |
| 44 | #endif |
| 45 | |
| 46 | TcpSocketPtr sockPtr(new TcpSocket(m_localAddress.address().mode(), socketImpl)); |
| 47 | |
| 48 | sockPtr->m_localAddress = m_localAddress; |
| 49 | setAddressFromNative(sockPtr->m_remoteAddress, m_localAddress.address().mode(), &sockAddr); |
| 50 | Logger::debug("accept from {} ({})", sockPtr->m_remoteAddress, sockPtr->m_impl->socketDesc); |
| 51 | |
| 52 | return sockPtr; |
| 53 | } |
| 54 | |
| 55 | void TcpSocket::setNoDelay(bool noDelay) { |
| 56 | ReadLocker locker(m_mutex); |
no test coverage detected