Конструктор клиент-серверных протоколов19.10.2022 08:31
#include
using namespace std;
using namespace awh;
class WebSocket {
private:
log_t * _log;
public:
string password(const string & login){
this->_log->print("USER: %s, PASS: %s", log_t::flag_t::INFO, login.c_str(), "password");
return "password";
}
bool auth(const string & login, const string & password){
this->_log->print("USER: %s, PASS: %s", log_t::flag_t::INFO, login.c_str(), password.c_str());
return true;
}
public:
bool accept(const string & ip, const string & mac, const u_int port, server::ws_t * ws){
this->_log->print("ACCEPT: ip = %s, mac = %s, port = %d", log_t::flag_t::INFO, ip.c_str(), mac.c_str(), port);
return true;
}
void active(const size_t aid, const server::ws_t::mode_t mode, server::ws_t * ws){
this->_log->print("%s client", log_t::flag_t::INFO, (mode == server::ws_t::mode_t::CONNECT ? "Connect" : "Disconnect"));
}
void error(const size_t aid, const u_int code, const string & mess, server::ws_t * ws){
this->_log->print("%s [%u]", log_t::flag_t::CRITICAL, mess.c_str(), code);
}
void message(const size_t aid, const vector & buffer, const bool utf8, server::ws_t * ws){
if(!buffer.empty()){
this->_log->print("message: %s [%s]", log_t::flag_t::INFO, string(buffer.begin(), buffer.end()).c_str(), ws->sub(aid).c_str());
ws->send(aid, buffer.data(), buffer.size(), utf8);
}
}
public:
WebSocket(log_t * log) : _log(log) {}
};
int main(int argc, char * argv[]){
fmk_t fmk;
log_t log(&fmk);
WebSocket executor(&log);
server::core_t core(&fmk, &log);
server::ws_t ws(&core, &fmk, &log);
log.setLogName("WebSocket Server");
log.setLogFormat("%H:%M:%S %d.%m.%Y");
core.clusterSize();
core.verifySSL(false);
core.sonet(awh::scheme_t::sonet_t::TLS);
core.affiliation(awh::core_t::affiliation_t::PRIMARY);
core.certificate("./ca/certs/server-cert.pem", "./ca/certs/server-key.pem");
ws.realm("ANYKS");
ws.opaque("keySession");
ws.subs({"test1", "test2", "test3"});
// ws.authType(auth_t::type_t::BASIC);
ws.authType(auth_t::type_t::DIGEST, auth_t::hash_t::MD5);
ws.init(2222, "127.0.0.1", http_t::compress_t::DEFLATE);
ws.on((function ) bind(&WebSocket::password, &executor, _1));
// ws.on((function ) bind(&WebSocket::auth, &executor, _1, _2));
ws.on((function ) bind(&WebSocket::active, &executor, _1, _2, _3));
ws.on((function ) bind(&WebSocket::error, &executor, _1, _2, _3, _4));
ws.on((function ) bind(&WebSocket::accept, &executor, _1, _2, _3, _4));
ws.on((function &, const bool, server::ws_t *)>) bind(&WebSocket::message, &executor, _1, _2, _3, _4));
ws.start();
return 0;
}
© Habrahabr.ru