代码拉取完成,页面将自动刷新
#pragma once
/*
* TCP server for anet implementation
* anet��TCP������
* Copyright (C) 2019 - 2023 gavingqf(gavingqf@126.com)
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#include <any>
#include "acceptor.hpp"
#include <string>
#include <assert.h>
#include "event_loop.hpp"
#include "anet.hpp"
#include "asio/detail/noncopyable.hpp"
#include "http_info.hpp"
namespace anet {
namespace tcp {
static const char *gDefaultAddr = "0";
class CServer : asio::noncopyable {
public:
explicit CServer(CEventLoop &loop) : m_acceptor(loop) {
}
virtual ~CServer() {
this->stop();
}
public:
// set max connection num, where num == 0 is unlimited.
void setMaxConnectNum(int num) {
m_acceptor.setMaxConnectNum(num);
}
void setAny(std::any any) {
this->m_attach = any;
}
std::any getAny() {
return m_attach;
}
void setSessionFactory(ISessionFactory *factory) {
m_acceptor.setSessionFactory(factory);
}
void setPacketParser(ICodec *parser) {
m_acceptor.setPacketParser(parser);
}
// start server with ip and port.
bool start(const std::string &addr, unsigned short port) {
std::string realIP(addr);
if (addr == gDefaultAddr) {
realIP = "0.0.0.0";
}
return m_acceptor.start(realIP, port);
}
bool start(unsigned short port) {
return start(gDefaultAddr, port);
}
// start server with ip:port address
bool start(const std::string& addr) {
std::vector<std::string> vec;
anet::http::strSplits(addr, ":", vec);
if (vec.size() >= 2) {
return this->start(vec[0], std::stoi(vec[1]));
} else {
return false;
}
}
// stop server
void stop() {
m_acceptor.stop();
}
protected:
CAcceptor m_acceptor;
std::any m_attach;
};
// simple server class for user.
class Server final {
public:
explicit Server(unsigned int size = 0) : m_loop(size), m_server(m_loop) {}
virtual ~Server() = default;
public:
CServer& proxy() {
return m_server;
}
CServer* operator->() {
return &m_server;
}
protected:
// event loop object.
CEventLoop m_loop;
// server object.
CServer m_server;
};
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。