本文整理汇总了C++中SelectServer类的典型用法代码示例。如果您正苦于以下问题:C++ SelectServer类的具体用法?C++ SelectServer怎么用?C++ SelectServer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SelectServer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SelectServer
/*
* Setup the select server
*/
void AdvancedTCPConnectorTest::setUp() {
m_tcp_socket_factory.reset(new ola::network::TCPSocketFactory(
ola::NewCallback(this, &AdvancedTCPConnectorTest::OnConnect)));
m_connected_socket = NULL;
ola::InitLogging(ola::OLA_LOG_INFO, ola::OLA_LOG_STDERR);
m_ss = new SelectServer(NULL, &m_clock);
m_timeout_id = m_ss->RegisterSingleTimeout(
ABORT_TIMEOUT_IN_MS,
ola::NewSingleCallback(this, &AdvancedTCPConnectorTest::Timeout));
OLA_ASSERT_TRUE(m_timeout_id);
}
示例2: LoopbackDescriptor
/*
* Check that RemoveWriteDescriptor is reentrant.
* We use the Execute() method to close a write descriptor during the same
* cycle in which it becomes writeable. See
* https://github.com/OpenLightingProject/ola/pull/429 for details.
*/
void SelectServerTest::testRemoveWriteWhenOtherReadable() {
Descriptors read_set, write_set, delete_set;
LoopbackDescriptor *loopback = new LoopbackDescriptor();
loopback->Init();
loopback->SetOnWritable(NewCallback(this, &SelectServerTest::NullHandler));
write_set.insert(loopback);
delete_set.insert(loopback);
OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(loopback));
OLA_ASSERT_EQ(1, write_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
m_ss->Execute(NewSingleCallback(
this, &SelectServerTest::RemoveAndDeleteDescriptors,
read_set, write_set, delete_set));
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());
}
示例3: SelectServer
/*
* Setup the select server
*/
void SocketTest::setUp() {
m_ss = new SelectServer();
m_timeout_closure = ola::NewSingleCallback(this, &SocketTest::Timeout);
OLA_ASSERT_TRUE(m_ss->RegisterSingleTimeout(ABORT_TIMEOUT_IN_MS,
m_timeout_closure));
#if _WIN32
WSADATA wsa_data;
int result = WSAStartup(MAKEWORD(2, 0), &wsa_data);
OLA_ASSERT_EQ(result, 0);
#endif // _WIN32
}
示例4: 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);
}
示例5: testRemoteEndClose
/*
* Confirm we correctly detect the remote end closing the connection.
*/
void SelectServerTest::testRemoteEndClose() {
Descriptors read_set, write_set, delete_set;
LoopbackDescriptor loopback;
loopback.Init();
read_set.insert(&loopback);
loopback.SetOnClose(NewSingleCallback(
this, &SelectServerTest::RemoveAndDeleteDescriptors,
read_set, write_set, delete_set));
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback));
OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
// now the Write end closes
loopback.CloseClient();
m_ss->Run();
OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get());
OLA_ASSERT_EQ(0, read_descriptor_count->Get());
}
示例6: CPPUNIT_ASSERT
/*
* Setup the select server
*/
void AdvancedTCPConnectorTest::setUp() {
m_tcp_socket_factory.reset(new ola::network::TCPSocketFactory(
ola::NewCallback(this, &AdvancedTCPConnectorTest::OnConnect)));
m_connected_socket = NULL;
ola::InitLogging(ola::OLA_LOG_INFO, ola::OLA_LOG_STDERR);
string localhost_str = "127.0.0.1";
CPPUNIT_ASSERT(IPV4Address::FromString(localhost_str, &m_localhost));
m_ss = new SelectServer(NULL, &m_clock);
m_timeout_id = m_ss->RegisterSingleTimeout(
ABORT_TIMEOUT_IN_MS,
ola::NewSingleCallback(this, &AdvancedTCPConnectorTest::Timeout));
CPPUNIT_ASSERT(m_timeout_id);
}
示例7: testAddRemoveReadDescriptor
/*
* Check AddReadDescriptor/RemoveReadDescriptor works correctly and that the
* export map is updated.
*/
void SelectServerTest::testAddRemoveReadDescriptor() {
LoopbackDescriptor bad_socket;
IntegerVariable *connected_socket_count =
m_map->GetIntegerVar(PollerInterface::K_CONNECTED_DESCRIPTORS_VAR);
IntegerVariable *socket_count =
m_map->GetIntegerVar(PollerInterface::K_READ_DESCRIPTOR_VAR);
OLA_ASSERT_EQ(1, connected_socket_count->Get()); // internal socket
OLA_ASSERT_EQ(0, socket_count->Get());
// adding and removin a non-connected socket should fail
OLA_ASSERT_FALSE(m_ss->AddReadDescriptor(&bad_socket));
OLA_ASSERT_FALSE(m_ss->RemoveReadDescriptor(&bad_socket));
LoopbackDescriptor loopback_socket;
loopback_socket.Init();
OLA_ASSERT_EQ(1, connected_socket_count->Get());
OLA_ASSERT_EQ(0, socket_count->Get());
OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&loopback_socket));
// Adding a second time should fail
OLA_ASSERT_FALSE(m_ss->AddReadDescriptor(&loopback_socket));
OLA_ASSERT_EQ(2, connected_socket_count->Get());
OLA_ASSERT_EQ(0, socket_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_FALSE(m_ss->AddReadDescriptor(&udp_socket));
OLA_ASSERT_EQ(2, connected_socket_count->Get());
OLA_ASSERT_EQ(1, socket_count->Get());
// Check remove works
OLA_ASSERT_TRUE(m_ss->RemoveReadDescriptor(&loopback_socket));
OLA_ASSERT_EQ(1, connected_socket_count->Get());
OLA_ASSERT_EQ(1, socket_count->Get());
OLA_ASSERT_TRUE(m_ss->RemoveReadDescriptor(&udp_socket));
OLA_ASSERT_EQ(1, connected_socket_count->Get());
OLA_ASSERT_EQ(0, socket_count->Get());
// Remove again should fail
OLA_ASSERT_FALSE(m_ss->RemoveReadDescriptor(&loopback_socket));
}
示例8: SelectServer
/*
* Setup the select server
*/
void AdvancedTCPConnectorTest::setUp() {
m_tcp_socket_factory.reset(new ola::network::TCPSocketFactory(
ola::NewCallback(this, &AdvancedTCPConnectorTest::OnConnect)));
m_connected_socket = NULL;
m_ss = new SelectServer(NULL, &m_clock);
m_timeout_id = m_ss->RegisterSingleTimeout(
ABORT_TIMEOUT_IN_MS,
ola::NewSingleCallback(this, &AdvancedTCPConnectorTest::Timeout));
OLA_ASSERT_TRUE(m_timeout_id);
#if _WIN32
WSADATA wsa_data;
int result = WSAStartup(MAKEWORD(2, 0), &wsa_data);
OLA_ASSERT_EQ(result, 0);
#endif
}
示例9: 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);
}
示例10: AcceptedConnection
/*
* Accept a new TCP connection.
*/
void AdvancedTCPConnectorTest::AcceptedConnection(TcpSocket *new_socket) {
CPPUNIT_ASSERT(new_socket);
IPV4Address address;
uint16_t port;
CPPUNIT_ASSERT(new_socket->GetPeer(&address, &port));
OLA_INFO << "Connection from " << address << ":" << port;
// terminate the ss when this connection is closed
new_socket->SetOnClose(
ola::NewSingleCallback(this, &AdvancedTCPConnectorTest::TerminateOnClose));
m_ss->AddReadDescriptor(new_socket, true);
}
示例11: DeviceDisconnected
void LogicReader::DeviceDisconnected(U64 device) {
OLA_FATAL << "Device " << device << " disconnected";
MutexLocker lock(&m_mu);
if (device != m_device_id) {
return;
}
m_device_id = 0;
m_logic = NULL;
m_ss->Terminate();
}
示例12: UDPReceiveAndTerminate
/*
* Receive some data and check it.
*/
void SocketTest::UDPReceiveAndTerminate(UDPSocket *socket) {
IPV4Address expected_address;
OLA_ASSERT_TRUE(IPV4Address::FromString("127.0.0.1", &expected_address));
IPV4SocketAddress source;
uint8_t buffer[sizeof(test_cstring) + 10];
ssize_t data_read = sizeof(buffer);
socket->RecvFrom(buffer, &data_read, &source);
OLA_ASSERT_EQ(static_cast<ssize_t>(sizeof(test_cstring)), data_read);
OLA_ASSERT_EQ(expected_address, source.Host());
m_ss->Terminate();
}
示例13: SocketClosed
void SocketClosed(TCPSocket *socket) {
OLA_INFO << "Socket @ " << socket << " was closed";
vector<TCPSocket*>::iterator iter = m_sockets.begin();
for (; iter != m_sockets.end(); ++iter) {
if (*iter == socket) {
m_ss.RemoveReadDescriptor(socket);
socket->Close();
delete socket;
m_sockets.erase(iter);
break;
}
}
}
示例14: NewConnectionSend
/*
* Accept a new connection and send some test data
*/
void SocketTest::NewConnectionSend(TCPSocket *new_socket) {
OLA_ASSERT_TRUE(new_socket);
GenericSocketAddress address = new_socket->GetPeerAddress();
OLA_ASSERT_TRUE(address.Family() == AF_INET);
OLA_INFO << "Connection from " << address;
ssize_t bytes_sent = new_socket->Send(
static_cast<const uint8_t*>(test_cstring),
sizeof(test_cstring));
OLA_ASSERT_EQ(static_cast<ssize_t>(sizeof(test_cstring)), bytes_sent);
new_socket->SetOnClose(ola::NewSingleCallback(this,
&SocketTest::TerminateOnClose));
m_ss->AddReadDescriptor(new_socket, true);
}
示例15: main
/*
* Main
*/
int main(int argc, char *argv[]) {
ola::InitLogging(ola::OLA_LOG_INFO, ola::OLA_LOG_STDERR);
udp_socket.SetOnData(ola::NewCallback(&SocketReady));
if (!udp_socket.Init()) {
OLA_WARN << "Failed to init";
return 1;
}
if (!udp_socket.Bind(6038)) {
OLA_WARN << "Failed to bind";
return 1;
}
if (!udp_socket.EnableBroadcast()) {
OLA_WARN << "Failed to enabl bcast";
return 1;
}
ss.AddSocket(&udp_socket);
ss.Run();
return 0;
}