bool Accept(TcpSocket& clientSocket) const { clientSocket.Close(); InetAddr& clientAddr = clientSocket.Addr(); int connFd = accept(m_Fd, (struct sockaddr*)&clientAddr.m_Addr, &clientAddr.m_AddrLen); clientSocket.m_Fd = connFd; return connFd != -1; }
void Server::AcceptClients() { TcpSocket c; Address from; if(c.Accept(server, from, false)) { std::wstring name = c.GetPeer().ToString(false); // Check password. Packet p; if(c.Receive(&p, sizeof(p), 1000) == sizeof(p) && (p.Control == C_CONNECT || p.Control == C_RESUME)) { if(password == 0 || password == ntohl(p.Password)) { // Check if a client is being booted by the new client. if(client.IsValid()) { std::wstring name = client.GetPeer().ToString(false); client.Close(); if(p.Control != C_RESUME) Log(OL_INFO, L"Replacing client %s\r\n", name.c_str()); } if(p.Control == C_CONNECT) Log(OL_NOTIFY | OL_INFO, L"Client connected from %s\r\n", name.c_str()); else Log(OL_INFO, L"Client resumed\r\n"); p.Control = C_CONNECT; c.Send(&p, sizeof(p)); client.Take(c); } else { p.Control = C_DISCONNECT; p.Reason = 1; Log(OL_INFO, L"Rejected client %s: Bad password\r\n", name.c_str()); } } else { p.Control = C_DISCONNECT; p.Reason = 0; Log(OL_INFO, L"Client failed to connect from %s\r\n", name.c_str()); } if(p.Control == C_DISCONNECT) { c.Send(&p, sizeof(p)); c.Close(); } } }
bool Accept(TcpSocket& connection, bool blocking = true) const { if (!IsListening()) { throw std::runtime_error("TcpSocket.Accept - Called on a socket that does not have listening mode enabled"); } auto newSocket = SOCKET(0); auto address = sockaddr_in(); auto tryAgain = bool(); do { tryAgain = false; if (!Winsock::Accept(socket, address, newSocket)) { switch (WSAGetLastError()) { case WSAEWOULDBLOCK: //if (!IsBlocking()) //{ // throw std::runtime_error("TcpSocket.Accept - Received would-block error for non-blocking socket"); //} return false; case WSAECONNRESET: tryAgain = true; break; default: return false; } } } while (tryAgain); if (connection.IsOpen()) { connection.Close(); } connection.socket = newSocket; connection.isListening = false; if (!Winsock::IoctlSocket(connection.socket, blocking)) { throw std::runtime_error("TcpSocket.Accept - Unable to set blocking mode on new connection"); } connection.isBlocking = blocking; return true; }
/** * Test that pausing a connection works. */ void AdvancedTCPConnectorTest::testPause() { ola::network::TCPSocketFactory socket_factory( ola::NewCallback(this, &AdvancedTCPConnectorTest::AcceptedConnection)); TcpAcceptingSocket listening_socket(&socket_factory); SetupListeningSocket(&listening_socket); AdvancedTCPConnector connector( m_ss, m_tcp_socket_factory.get(), TimeInterval(0, CONNECT_TIMEOUT_IN_MS * 1000)); // 5 per attempt, up to a max of 30 LinearBackoffPolicy policy(TimeInterval(5, 0), TimeInterval(30, 0)); // add endpoint, but make sure it's paused connector.AddEndpoint(m_localhost, SERVER_PORT, &policy, true); CPPUNIT_ASSERT_EQUAL(1u, connector.EndpointCount()); ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT, AdvancedTCPConnector::PAUSED, 0); m_ss->RunOnce(0, 500000); // now unpause connector.Resume(m_localhost, SERVER_PORT); ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT, AdvancedTCPConnector::DISCONNECTED, 0); m_ss->Run(); CPPUNIT_ASSERT_EQUAL(1u, connector.EndpointCount()); ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT, AdvancedTCPConnector::CONNECTED, 0); // check our socket exists CPPUNIT_ASSERT(m_connected_socket); m_connected_socket->Close(); delete m_connected_socket; OLA_INFO << "disconnecting"; connector.Disconnect(m_localhost, SERVER_PORT, true); // state should be updated ConfirmState(__LINE__, connector, m_localhost, SERVER_PORT, AdvancedTCPConnector::PAUSED, 0); // clean up connector.RemoveEndpoint(m_localhost, SERVER_PORT); CPPUNIT_ASSERT_EQUAL(0u, connector.EndpointCount()); m_ss->RemoveReadDescriptor(&listening_socket); }
static void Server(String r) { TcpSocket server; if(server.Listen(4000, 10)) { TcpSocket socket; LOG("Waiting..."); bool b = socket.Accept(server); if(b) { LOG("Connection accepted"); HttpHeader http; http.Read(socket); socket.Put(r); socket.Close(); } } }
Socket::Status TcpListener::Accept(TcpSocket& socket) { if (GetHandle() == be::SocketBE::invalidSocket()) { return Error; } sockaddr_in address; be::SocketBE::AddrLength length = sizeof(address); SocketHandle remote = ::accept(GetHandle(), reinterpret_cast<sockaddr*>(&address), &length); if (remote == be::SocketBE::invalidSocket()) return be::SocketBE::getErrorStatus(); socket.Close(); socket.Create(remote); return Done; }
Socket::Status TcpListener::Accept(TcpSocket& socket) { // Make sure that we're listening if (GetHandle() == priv::SocketImpl::InvalidSocket()) { Err() << "Failed to accept a new connection, the socket is not listening" << std::endl; return Error; } // Accept a new connection sockaddr_in address; priv::SocketImpl::AddrLength length = sizeof(address); SocketHandle remote = accept(GetHandle(), reinterpret_cast<sockaddr*>(&address), &length); // Check for errors if (remote == priv::SocketImpl::InvalidSocket()) return priv::SocketImpl::GetErrorStatus(); // Initialize the new connected socket socket.Close(); socket.Create(remote); return Done; }