AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
AsyncFw::ListenSocket Class Reference

The ListenSocket class. More...

#include <AsyncFw/ListenSocket>

Inheritance diagram for AsyncFw::ListenSocket:
[legend]
Collaboration diagram for AsyncFw::ListenSocket:
[legend]

Public Member Functions

std::string address () const
 
virtual void close ()
 
virtual void destroy ()
 
bool listen (const std::string &, uint16_t)
 
uint16_t port () const
 

Public Attributes

AsyncFw::FunctionConnectorProtected< ListenSocket >::Connector< int, const std::string &, bool * > incoming {AsyncFw::AbstractFunctionConnector::SyncOnly}
 The FunctionConnector for incoming connections.
 

Protected Member Functions

void incomingEvent () override
 

Detailed Description

The ListenSocket class.

Example:

#include <AsyncFw/DataArray>
#include <AsyncFw/AbstractTlsSocket>
#include <AsyncFw/ListenSocket>
#include <AsyncFw/MainThread>
#include <AsyncFw/LogStream>
class TcpSocket : public AsyncFw::AbstractTlsSocket {
public:
static TcpSocket *create() { return new TcpSocket; }
void stateEvent() {
stateChanged(state_);
logDebug() << "State event:" << static_cast<int>(state_);
}
void readEvent() { received(read()); }
AsyncFw::FunctionConnectorProtected<TcpSocket>::Connector<const AsyncFw::DataArray &> received;
AsyncFw::FunctionConnectorProtected<TcpSocket>::Connector<AsyncFw::AbstractSocket::State> stateChanged;
};
int main(int argc, char *argv[]) {
ls.incoming([](int fd, const std::string &address, bool *accept) {
TcpSocket *socket = TcpSocket::create();
socket->setDescriptor(fd);
socket->received([socket](const AsyncFw::DataArray &data) {
logNotice() << "received:" << data;
socket->write("Answer\n");
if (data == AsyncFw::DataArray('q')) AsyncFw::MainThread::exit(0);
});
logInfo() << "Incoming:" << fd << address;
*accept = true;
});
ls.listen("0.0.0.0", 18080);
if (argc == 2 && std::string(argv[1]) == "--tst") {
TcpSocket *_socket = TcpSocket::create();
_socket->stateChanged([_socket](const AsyncFw::AbstractSocket::State state) {
if (state == AsyncFw::AbstractSocket::State::Active) {
logDebug() << "Send request";
_socket->write("q");
}
});
_socket->connect("127.0.0.1", 18080);
}
logNotice() << "Start Applicaiton";
int ret = AsyncFw::MainThread::exec();
logNotice() << "End Applicaiton" << ret;
return ret;
}

Member Function Documentation

◆ incomingEvent()

void ListenSocket::incomingEvent ( )
overrideprotectedvirtual

Reimplemented from AsyncFw::AbstractSocket.

◆ close()

void AbstractSocket::close ( )
virtual

Reimplemented from AsyncFw::AbstractSocket.

◆ destroy()

void AbstractSocket::destroy ( )
virtual

Reimplemented from AsyncFw::AbstractSocket.


The documentation for this class was generated from the following files: