2021-11-28 15:29:26 +08:00
|
|
|
// Copyright 2021, Roman Gershman. All rights reserved.
|
|
|
|
// See LICENSE for licensing terms.
|
2021-11-16 16:39:38 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-11-24 20:09:53 +08:00
|
|
|
#include <absl/container/fixed_array.h>
|
|
|
|
|
|
|
|
#include <deque>
|
2022-02-19 19:34:45 +08:00
|
|
|
#include <variant>
|
2021-11-16 16:39:38 +08:00
|
|
|
|
2021-11-16 21:04:32 +08:00
|
|
|
#include "base/io_buf.h"
|
2021-12-21 04:46:51 +08:00
|
|
|
#include "core/resp_expr.h"
|
2021-11-24 20:09:53 +08:00
|
|
|
#include "server/common_types.h"
|
|
|
|
#include "util/connection.h"
|
|
|
|
#include "util/fibers/event_count.h"
|
2021-11-16 21:04:32 +08:00
|
|
|
|
2021-11-20 00:00:14 +08:00
|
|
|
typedef struct ssl_ctx_st SSL_CTX;
|
2022-02-24 04:18:05 +08:00
|
|
|
typedef struct mi_heap_s mi_heap_t;
|
2021-11-20 00:00:14 +08:00
|
|
|
|
2021-11-16 16:39:38 +08:00
|
|
|
namespace dfly {
|
|
|
|
|
2021-11-17 22:38:32 +08:00
|
|
|
class ConnectionContext;
|
2021-11-20 00:00:14 +08:00
|
|
|
class RedisParser;
|
|
|
|
class Service;
|
2021-11-23 18:39:35 +08:00
|
|
|
class MemcacheParser;
|
2021-11-16 16:39:38 +08:00
|
|
|
|
|
|
|
class Connection : public util::Connection {
|
|
|
|
public:
|
2021-11-23 18:39:35 +08:00
|
|
|
Connection(Protocol protocol, Service* service, SSL_CTX* ctx);
|
2021-11-16 16:39:38 +08:00
|
|
|
~Connection();
|
|
|
|
|
|
|
|
using error_code = std::error_code;
|
|
|
|
using ShutdownCb = std::function<void()>;
|
|
|
|
using ShutdownHandle = unsigned;
|
|
|
|
|
|
|
|
ShutdownHandle RegisterShutdownHook(ShutdownCb cb);
|
|
|
|
void UnregisterShutdownHook(ShutdownHandle id);
|
|
|
|
|
2021-11-24 20:09:53 +08:00
|
|
|
Protocol protocol() const {
|
|
|
|
return protocol_;
|
|
|
|
}
|
2021-11-23 18:39:35 +08:00
|
|
|
|
2021-11-16 16:39:38 +08:00
|
|
|
protected:
|
|
|
|
void OnShutdown() override;
|
|
|
|
|
|
|
|
private:
|
2021-11-16 21:04:32 +08:00
|
|
|
enum ParserStatus { OK, NEED_MORE, ERROR };
|
|
|
|
|
2021-11-16 16:39:38 +08:00
|
|
|
void HandleRequests() final;
|
|
|
|
|
2022-01-26 18:51:29 +08:00
|
|
|
//
|
|
|
|
io::Result<bool> CheckForHttpProto(util::FiberSocketBase* peer);
|
2022-02-19 19:34:45 +08:00
|
|
|
|
|
|
|
void ConnectionFlow(util::FiberSocketBase* peer);
|
|
|
|
std::variant<std::error_code, ParserStatus> IoLoop(util::FiberSocketBase* peer);
|
|
|
|
|
2021-11-24 20:09:53 +08:00
|
|
|
void DispatchFiber(util::FiberSocketBase* peer);
|
2021-11-16 16:39:38 +08:00
|
|
|
|
2022-01-26 18:51:29 +08:00
|
|
|
ParserStatus ParseRedis();
|
|
|
|
ParserStatus ParseMemcache();
|
2021-11-16 16:39:38 +08:00
|
|
|
|
2022-01-26 18:51:29 +08:00
|
|
|
base::IoBuf io_buf_;
|
2021-11-16 21:04:32 +08:00
|
|
|
std::unique_ptr<RedisParser> redis_parser_;
|
2021-11-23 18:39:35 +08:00
|
|
|
std::unique_ptr<MemcacheParser> memcache_parser_;
|
2021-11-20 00:00:14 +08:00
|
|
|
Service* service_;
|
|
|
|
SSL_CTX* ctx_;
|
2021-11-17 22:38:32 +08:00
|
|
|
std::unique_ptr<ConnectionContext> cc_;
|
|
|
|
|
2022-02-24 04:18:05 +08:00
|
|
|
struct Request;
|
2021-11-24 20:09:53 +08:00
|
|
|
|
2022-02-24 04:18:05 +08:00
|
|
|
static Request* FromArgs(RespVec args, mi_heap_t* heap);
|
2021-11-24 20:09:53 +08:00
|
|
|
|
|
|
|
std::deque<Request*> dispatch_q_; // coordinated via evc_.
|
|
|
|
util::fibers_ext::EventCount evc_;
|
2021-11-16 21:04:32 +08:00
|
|
|
unsigned parser_error_ = 0;
|
2021-11-23 18:39:35 +08:00
|
|
|
Protocol protocol_;
|
2021-11-16 16:39:38 +08:00
|
|
|
struct Shutdown;
|
|
|
|
std::unique_ptr<Shutdown> shutdown_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dfly
|