本文整理汇总了C++中SelectServer::AddReadDescriptor方法的典型用法代码示例。如果您正苦于以下问题:C++ SelectServer::AddReadDescriptor方法的具体用法?C++ SelectServer::AddReadDescriptor怎么用?C++ SelectServer::AddReadDescriptor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SelectServer
的用法示例。
在下文中一共展示了SelectServer::AddReadDescriptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testUDPSocket
/*
* Test UDP sockets work correctly.
* The client connects and the server sends some data. The client checks the
* data matches and then closes the connection.
*/
void SocketTest::testUDPSocket() {
IPV4SocketAddress socket_address(IPV4Address::Loopback(), 0);
UDPSocket socket;
OLA_ASSERT_TRUE(socket.Init());
OLA_ASSERT_FALSE(socket.Init());
OLA_ASSERT_TRUE(socket.Bind(socket_address));
OLA_ASSERT_FALSE(socket.Bind(socket_address));
IPV4SocketAddress local_address;
OLA_ASSERT_TRUE(socket.GetSocketAddress(&local_address));
OLA_ASSERT_EQ(static_cast<uint16_t>(AF_INET), local_address.Family());
socket.SetOnData(
ola::NewCallback(this, &SocketTest::UDPReceiveAndSend, &socket));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&socket));
UDPSocket client_socket;
OLA_ASSERT_TRUE(client_socket.Init());
OLA_ASSERT_FALSE(client_socket.Init());
client_socket.SetOnData(
ola::NewCallback(
this, &SocketTest::UDPReceiveAndTerminate,
static_cast<UDPSocket*>(&client_socket)));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&client_socket));
ssize_t bytes_sent = client_socket.SendTo(
static_cast<const uint8_t*>(test_cstring),
sizeof(test_cstring),
local_address);
OLA_ASSERT_EQ(static_cast<ssize_t>(sizeof(test_cstring)), bytes_sent);
m_ss->Run();
m_ss->RemoveReadDescriptor(&socket);
m_ss->RemoveReadDescriptor(&client_socket);
}
示例2: SocketServerClose
/**
* Generic method to test server initiated close
*/
void SocketTest::SocketServerClose(ConnectedDescriptor *socket,
ConnectedDescriptor *socket2) {
OLA_ASSERT_NOT_NULL(socket);
socket->SetOnData(ola::NewCallback(
this, &SocketTest::Receive,
static_cast<ConnectedDescriptor*>(socket)));
socket->SetOnClose(
ola::NewSingleCallback(this, &SocketTest::TerminateOnClose));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(socket));
OLA_ASSERT_TRUE(socket2);
socket2->SetOnData(ola::NewCallback(
this, &SocketTest::ReceiveSendAndClose,
static_cast<ConnectedDescriptor*>(socket2)));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(socket2));
ssize_t bytes_sent = socket->Send(
static_cast<const uint8_t*>(test_cstring),
sizeof(test_cstring));
OLA_ASSERT_EQ(static_cast<ssize_t>(sizeof(test_cstring)), bytes_sent);
m_ss->Run();
m_ss->RemoveReadDescriptor(socket);
m_ss->RemoveReadDescriptor(socket2);
delete socket2;
}
示例3: testIOQueueUDPSend
/*
* Test UDP sockets with an IOQueue work correctly.
* The client connects and the server sends some data. The client checks the
* data matches and then closes the connection.
*/
void SocketTest::testIOQueueUDPSend() {
IPV4SocketAddress socket_address(IPV4Address::Loopback(), 9010);
UDPSocket socket;
OLA_ASSERT_TRUE(socket.Init());
OLA_ASSERT_FALSE(socket.Init());
OLA_ASSERT_TRUE(socket.Bind(socket_address));
OLA_ASSERT_FALSE(socket.Bind(socket_address));
socket.SetOnData(
ola::NewCallback(this, &SocketTest::UDPReceiveAndSend, &socket));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&socket));
UDPSocket client_socket;
OLA_ASSERT_TRUE(client_socket.Init());
OLA_ASSERT_FALSE(client_socket.Init());
client_socket.SetOnData(
ola::NewCallback(
this, &SocketTest::UDPReceiveAndTerminate,
static_cast<UDPSocket*>(&client_socket)));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&client_socket));
IOQueue output;
output.Write(static_cast<const uint8_t*>(test_cstring), sizeof(test_cstring));
ssize_t bytes_sent = client_socket.SendTo(&output, socket_address);
OLA_ASSERT_EQ(static_cast<ssize_t>(sizeof(test_cstring)), bytes_sent);
m_ss->Run();
m_ss->RemoveReadDescriptor(&socket);
m_ss->RemoveReadDescriptor(&client_socket);
}
示例4: testRemoveOthersWhenReadable
/*
* Check that we don't invalid iterators by removing descriptors during an
* on_read callback.
*/
void SelectServerTest::testRemoveOthersWhenReadable() {
Descriptors read_set, write_set, delete_set;
LoopbackDescriptor loopback1, loopback2, loopback3;
loopback1.Init();
loopback2.Init();
loopback3.Init();
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback1));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback2));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback3));
read_set.insert(&loopback1);
read_set.insert(&loopback2);
read_set.insert(&loopback3);
loopback2.SetOnClose(NewSingleCallback(
this,
&SelectServerTest::RemoveAndDeleteDescriptors,
read_set, write_set, delete_set));
OLA_ASSERT_EQ(0, write_descriptor_count->Get());
OLA_ASSERT_EQ(4, connected_read_descriptor_count->Get());
loopback2.CloseClient();
m_ss->Run();
OLA_ASSERT_EQ(0, write_descriptor_count->Get());
OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
示例5: testTCPSocketServerClose
/*
* Test TCP sockets work correctly.
* The client connects and the server then sends some data and closes the
* connection.
*/
void SocketTest::testTCPSocketServerClose() {
IPV4SocketAddress socket_address(IPV4Address::Loopback(), 0);
ola::network::TCPSocketFactory socket_factory(
ola::NewCallback(this, &SocketTest::NewConnectionSendAndClose));
TCPAcceptingSocket socket(&socket_factory);
OLA_ASSERT_TRUE_MSG(socket.Listen(socket_address),
"Check for another instance of olad running");
OLA_ASSERT_FALSE(socket.Listen(socket_address));
GenericSocketAddress local_address = socket.GetLocalAddress();
OLA_ASSERT_EQ(static_cast<uint16_t>(AF_INET), local_address.Family());
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&socket));
// The client socket checks the response and terminates on close
TCPSocket *client_socket = TCPSocket::Connect(local_address);
OLA_ASSERT_NOT_NULL(client_socket);
client_socket->SetOnData(ola::NewCallback(
this, &SocketTest::Receive,
static_cast<ConnectedDescriptor*>(client_socket)));
client_socket->SetOnClose(
ola::NewSingleCallback(this, &SocketTest::TerminateOnClose));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(client_socket));
m_ss->Run();
m_ss->RemoveReadDescriptor(&socket);
m_ss->RemoveReadDescriptor(client_socket);
delete client_socket;
}
示例6: testAddRemoveReadDescriptor
/*
* Check AddReadDescriptor/RemoveReadDescriptor works correctly and that the
* export map is updated.
*/
void SelectServerTest::testAddRemoveReadDescriptor() {
OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
OLA_ASSERT_EQ(0, write_descriptor_count->Get());
LoopbackDescriptor loopback;
loopback.Init();
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback));
OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
OLA_ASSERT_EQ(0, write_descriptor_count->Get());
// Add a udp socket
UDPSocket udp_socket;
OLA_ASSERT_TRUE(udp_socket.Init());
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&udp_socket));
OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(1, read_descriptor_count->Get());
OLA_ASSERT_EQ(0, write_descriptor_count->Get());
// Check remove works
m_ss->RemoveReadDescriptor(&loopback);
OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(1, read_descriptor_count->Get());
OLA_ASSERT_EQ(0, write_descriptor_count->Get());
m_ss->RemoveReadDescriptor(&udp_socket);
OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
OLA_ASSERT_EQ(0, write_descriptor_count->Get());
}
示例7: testDoubleAddAndRemove
/*
* Confirm we can't add the same descriptor twice.
*/
void SelectServerTest::testDoubleAddAndRemove() {
OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); // internal socket
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
OLA_ASSERT_EQ(0, write_descriptor_count->Get());
LoopbackDescriptor loopback_socket;
loopback_socket.Init();
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback_socket));
OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
OLA_ASSERT_EQ(0, write_descriptor_count->Get());
OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&loopback_socket));
OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
OLA_ASSERT_EQ(1, write_descriptor_count->Get());
m_ss->RemoveReadDescriptor(&loopback_socket);
OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
OLA_ASSERT_EQ(1, write_descriptor_count->Get());
m_ss->RemoveWriteDescriptor(&loopback_socket);
OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
OLA_ASSERT_EQ(0, write_descriptor_count->Get());
// Trying to remove a second time shouldn't crash
m_ss->RemoveReadDescriptor(&loopback_socket);
m_ss->RemoveWriteDescriptor(&loopback_socket);
}
示例8: testNonBlockingConnect
/*
* Test non-blocking TCP connects work correctly.
*/
void TCPConnectorTest::testNonBlockingConnect() {
ola::network::TCPSocketFactory socket_factory(
ola::NewCallback(this, &TCPConnectorTest::AcceptedConnection));
TCPAcceptingSocket listening_socket(&socket_factory);
IPV4SocketAddress listen_address(m_localhost, 0);
OLA_ASSERT_TRUE_MSG(listening_socket.Listen(listen_address),
"Failed to listen");
GenericSocketAddress addr = listening_socket.GetLocalAddress();
OLA_ASSERT_TRUE(addr.IsValid());
// calling listen a second time should fail
OLA_ASSERT_FALSE(listening_socket.Listen(listen_address));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&listening_socket));
// now attempt a non-blocking connect
// because we're connecting to the localhost this may run this callback
// immediately.
TCPConnector connector(m_ss);
TimeInterval connect_timeout(0, CONNECT_TIMEOUT_IN_MS * 1000);
TCPConnector::TCPConnectionID id = connector.Connect(
addr.V4Addr(),
connect_timeout,
ola::NewSingleCallback(this, &TCPConnectorTest::OnConnect));
OLA_ASSERT_TRUE(id);
m_ss->Run();
OLA_ASSERT_EQ(0u, connector.ConnectionsPending());
m_ss->RemoveReadDescriptor(&listening_socket);
}
示例9: testRemoveWriteWhenReadable
/*
* Check that RemoveWriteDescriptor is reentrant.
* Similar to the case above, but this removes & deletes the descriptor from
* within the OnRead callback of the same descriptor.
*/
void SelectServerTest::testRemoveWriteWhenReadable() {
// Ownership is transferred to the SelectServer.
LoopbackDescriptor *loopback = new LoopbackDescriptor();
loopback->Init();
loopback->SetOnData(NewCallback(
this, &SelectServerTest::ReadDataAndRemove,
static_cast<ConnectedDescriptor*>(loopback)));
loopback->SetOnWritable(NewCallback(this, &SelectServerTest::NullHandler));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(loopback));
OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(loopback));
OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(1, write_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
// Send some data to make this descriptor readable.
uint8_t data[] = {'a'};
loopback->Send(data, arraysize(data));
m_ss->Run();
OLA_ASSERT_EQ(0, write_descriptor_count->Get());
OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
示例10: testShutdownWithActiveDescriptors
/*
* Confirm we don't leak memory when the SelectServer is destroyed without all
* the descriptors being removed.
*/
void SelectServerTest::testShutdownWithActiveDescriptors() {
LoopbackDescriptor loopback_socket;
loopback_socket.Init();
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback_socket));
OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&loopback_socket));
}
示例11: testPauseAndResume
/**
* Check pausing doesn't mark the channel as bad.
*/
void HealthCheckedConnectionTest::testPauseAndResume() {
MockHealthCheckedConnection connection(&socket,
&m_ss,
heartbeat_interval,
options,
&m_clock);
socket.SetOnData(
NewCallback(&connection, &MockHealthCheckedConnection::ReadData));
connection.Setup();
m_ss.AddReadDescriptor(&socket);
connection.Setup();
m_ss.RegisterSingleTimeout(
TimeInterval(1, 0),
NewSingleCallback(this,
&HealthCheckedConnectionTest::PauseReading,
&connection));
m_ss.RegisterSingleTimeout(
TimeInterval(3, 0),
NewSingleCallback(this,
&HealthCheckedConnectionTest::ResumeReading,
&connection));
m_ss.Run();
OLA_ASSERT_TRUE(connection.ChannelOk());
}
示例12: OnTCPConnect
void OnTCPConnect(TCPSocket *socket) {
OLA_INFO << "New connection: " << socket;
socket->SetOnData(
NewCallback(this, &Server::ReceiveTCPData, socket));
socket->SetOnClose(
NewSingleCallback(this, &Server::SocketClosed, socket));
m_ss.AddReadDescriptor(socket);
m_sockets.push_back(socket);
}
示例13: setUp
void RpcChannelTest::setUp() {
m_socket = new LoopbackDescriptor();
m_socket->Init();
m_service = new TestServiceImpl(&m_ss);
m_channel = new RpcChannel(m_service, m_socket);
m_ss.AddReadDescriptor(m_socket);
m_stub = new TestService_Stub(m_channel);
}
示例14: SetupListeningSocket
/**
* Setup a TCP socket that accepts connections
*/
void AdvancedTCPConnectorTest::SetupListeningSocket(
TcpAcceptingSocket *listening_socket) {
CPPUNIT_ASSERT_MESSAGE(
"Check for another instance of olad running",
listening_socket->Listen(m_localhost, SERVER_PORT));
// calling listen a second time should fail
CPPUNIT_ASSERT(!listening_socket->Listen(m_localhost, SERVER_PORT));
OLA_INFO << "listening on " <<SERVER_PORT;
CPPUNIT_ASSERT(m_ss->AddReadDescriptor(listening_socket));
}
示例15: AcceptedConnection
/*
* Accept a new TCP connection.
*/
void AdvancedTCPConnectorTest::AcceptedConnection(TCPSocket *new_socket) {
OLA_ASSERT_NOT_NULL(new_socket);
GenericSocketAddress address = new_socket->GetPeerAddress();
OLA_ASSERT_TRUE(address.Family() == AF_INET);
OLA_INFO << "Connection from " << address;
// terminate the ss when this connection is closed
new_socket->SetOnClose(
ola::NewSingleCallback(this, &AdvancedTCPConnectorTest::TerminateOnClose));
m_ss->AddReadDescriptor(new_socket, true);
}