2019-07-17 19:37:18 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <pipes/buffer.h>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <event.h>
|
|
|
|
|
|
|
|
namespace ts {
|
2020-01-24 02:57:58 +01:00
|
|
|
namespace server {
|
2020-01-26 18:04:38 +01:00
|
|
|
class VirtualServer;
|
2020-01-24 02:57:58 +01:00
|
|
|
}
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
namespace weblist {
|
|
|
|
class TSWebClient {
|
|
|
|
public:
|
|
|
|
enum State {
|
|
|
|
S_UNINITIALIZED,
|
|
|
|
S_INITIALIZING,
|
|
|
|
S_DATA_EXCHANGE,
|
|
|
|
S_FINALIZING,
|
|
|
|
S_FINALIZED
|
|
|
|
};
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-26 18:04:38 +01:00
|
|
|
TSWebClient(const std::shared_ptr <server::VirtualServer> &server, struct event_base *event_base, uint16_t session_index, bool /* resend name */);
|
2020-01-24 02:57:58 +01:00
|
|
|
virtual ~TSWebClient();
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
void report();
|
|
|
|
void abort();
|
|
|
|
void abort_sync();
|
|
|
|
inline bool running() { return this->state != S_FINALIZED && this->state != S_UNINITIALIZED; }
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
std::function<void(const std::string&, bool)> callback_error;
|
|
|
|
std::function<void()> callback_success;
|
|
|
|
private:
|
2020-01-26 18:04:38 +01:00
|
|
|
std::shared_ptr<server::VirtualServer> server;
|
2020-01-24 02:57:58 +01:00
|
|
|
struct event_base* event_base = nullptr;
|
|
|
|
std::thread close_thread;
|
|
|
|
State state;
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
sockaddr_in remote_address{};
|
|
|
|
std::mutex write_lock;
|
|
|
|
std::deque<pipes::buffer> write_buffer{};
|
|
|
|
int file_descriptor{0};
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
struct event* event_read = nullptr;
|
|
|
|
struct event* event_write = nullptr;
|
|
|
|
struct event* event_timeout = nullptr;
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
void unregister_events(bool /* blocking */);
|
|
|
|
void reset_timeout(bool /* reschedule */);
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
static void _handle_message_read(int, short, void*);
|
|
|
|
static void _handle_message_write(int, short, void*);
|
|
|
|
static void _handle_timeout(int, short,void*);
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
void trigger_fail_later(const std::string& /* message */, bool /* retry */);
|
|
|
|
void trigger_success_later();
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
void write_message(const pipes::buffer_view& /* buffer */);
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
private:
|
|
|
|
void handle_message_read(const pipes::buffer_view& /* buffer */);
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
uint16_t session_index = 1;
|
|
|
|
uint32_t session_id = 0;
|
|
|
|
bool send_name = false;
|
2019-07-17 19:37:18 +02:00
|
|
|
|
2020-01-24 02:57:58 +01:00
|
|
|
void request_session();
|
|
|
|
void send_status(bool /* name */);
|
|
|
|
};
|
|
|
|
}
|
2019-07-17 19:37:18 +02:00
|
|
|
}
|