Рисуем квадратик
This commit is contained in:
@@ -9,55 +9,34 @@ namespace LV::Net {
|
||||
|
||||
using namespace TOS;
|
||||
|
||||
Server::~Server() {
|
||||
stop();
|
||||
wait();
|
||||
bool SocketServer::isStopped() {
|
||||
return !Acceptor.is_open();
|
||||
}
|
||||
|
||||
bool Server::isStopped() {
|
||||
return !IsAlive;
|
||||
}
|
||||
|
||||
void Server::stop() {
|
||||
NeedClose = true;
|
||||
NeedClose.notify_all();
|
||||
if(Acceptor.is_open())
|
||||
Acceptor.close();
|
||||
}
|
||||
|
||||
void Server::wait() {
|
||||
while(bool val = IsAlive)
|
||||
IsAlive.wait(val);
|
||||
}
|
||||
|
||||
coro<void> Server::async_wait() {
|
||||
co_await Lock.async_wait();
|
||||
}
|
||||
|
||||
coro<void> Server::run() {
|
||||
IsAlive.store(true);
|
||||
|
||||
try {
|
||||
while(true) { // TODO: ловить ошибки на async_accept
|
||||
co_spawn(OnConnect(co_await Acceptor.async_accept()));
|
||||
coro<void> SocketServer::run(std::function<coro<>(tcp::socket)> onConnect) {
|
||||
while(true) { // TODO: ловить ошибки на async_accept
|
||||
try {
|
||||
co_spawn(onConnect(co_await Acceptor.async_accept()));
|
||||
} catch(const std::exception &exc) {
|
||||
if(const boost::system::system_error *errc = dynamic_cast<const boost::system::system_error*>(&exc);
|
||||
errc && (errc->code() == asio::error::operation_aborted || errc->code() == asio::error::bad_descriptor))
|
||||
break;
|
||||
}
|
||||
} catch(const std::exception &exc) {
|
||||
//if(!NeedClose)
|
||||
// TODO: std::cout << exc.what() << std::endl;
|
||||
}
|
||||
|
||||
Lock.cancel();
|
||||
IsAlive.store(false);
|
||||
IsAlive.notify_all();
|
||||
}
|
||||
|
||||
|
||||
AsyncSocket::~AsyncSocket() {
|
||||
boost::lock_guard lock(SendPackets.Mtx);
|
||||
|
||||
if(SendPackets.Context)
|
||||
SendPackets.Context->NeedShutdown = true;
|
||||
|
||||
boost::lock_guard lock(SendPackets.Mtx);
|
||||
|
||||
if(Socket.is_open())
|
||||
try { Socket.close(); } catch(...) {}
|
||||
|
||||
|
||||
SendPackets.SenderGuard.cancel();
|
||||
WorkDeadline.cancel();
|
||||
}
|
||||
@@ -70,7 +49,7 @@ void AsyncSocket::pushPackets(std::vector<Packet> *simplePackets, std::vector<Sm
|
||||
|| SendPackets.SmartBuffer.size() + (smartPackets ? smartPackets->size() : 0) >= MAX_SMART_PACKETS
|
||||
|| SendPackets.SizeInQueue >= MAX_PACKETS_SIZE_IN_WAIT))
|
||||
{
|
||||
Socket.close();
|
||||
try { Socket.close(); } catch(...) {}
|
||||
// TODO: std::cout << "Передоз пакетами, сокет закрыт" << std::endl;
|
||||
}
|
||||
|
||||
@@ -143,7 +122,7 @@ coro<> AsyncSocket::read(std::byte *data, uint32_t size) {
|
||||
void AsyncSocket::closeRead() {
|
||||
if(Socket.is_open() && !ReadShutdowned) {
|
||||
ReadShutdowned = true;
|
||||
Socket.shutdown(boost::asio::socket_base::shutdown_receive);
|
||||
try { Socket.shutdown(boost::asio::socket_base::shutdown_receive); } catch(...) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,41 +9,30 @@
|
||||
#include <boost/asio/write.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/circular_buffer.hpp>
|
||||
#include <condition_variable>
|
||||
|
||||
namespace LV::Net {
|
||||
|
||||
class Server : public AsyncObject {
|
||||
class SocketServer : public AsyncObject {
|
||||
protected:
|
||||
std::atomic_bool IsAlive = false, NeedClose = false;
|
||||
tcp::acceptor Acceptor;
|
||||
asio::deadline_timer Lock;
|
||||
std::function<coro<>(tcp::socket)> OnConnect;
|
||||
|
||||
public:
|
||||
Server(asio::io_context &ioc, std::function<coro<>(tcp::socket)> &&onConnect, uint16_t port = 0)
|
||||
: AsyncObject(ioc), Acceptor(ioc, tcp::endpoint(tcp::v4(), port)), Lock(ioc, boost::posix_time::pos_infin), OnConnect(std::move(onConnect))
|
||||
SocketServer(asio::io_context &ioc, std::function<coro<>(tcp::socket)> &&onConnect, uint16_t port = 0)
|
||||
: AsyncObject(ioc), Acceptor(ioc, tcp::endpoint(tcp::v4(), port))
|
||||
{
|
||||
assert(OnConnect);
|
||||
assert(onConnect);
|
||||
|
||||
co_spawn(run());
|
||||
IsAlive.store(true);
|
||||
IsAlive.notify_all();
|
||||
co_spawn(run(std::move(onConnect)));
|
||||
}
|
||||
|
||||
~Server();
|
||||
|
||||
bool isStopped();
|
||||
uint16_t getPort() {
|
||||
return Acceptor.local_endpoint().port();
|
||||
}
|
||||
|
||||
void stop();
|
||||
void wait();
|
||||
|
||||
coro<void> async_wait();
|
||||
|
||||
protected:
|
||||
coro<void> run();
|
||||
coro<void> run(std::function<coro<>(tcp::socket)> onConnect);
|
||||
};
|
||||
|
||||
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
|
||||
Reference in New Issue
Block a user