MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
communicator.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11//
12// Created by Wangyunlai on 2022/11/17.
13//
14
15#pragma once
16
17#include <string>
18#include <event.h>
19#include "common/rc.h"
20
21struct ConnectionContext;
22class SessionEvent;
23class Session;
24class BufferedWriter;
25
42{
43public:
44 virtual ~Communicator();
45
49 virtual RC init(int fd, Session *session, const std::string &addr);
50
55 virtual RC read_event(SessionEvent *&event) = 0;
56
64 virtual RC write_result(SessionEvent *event, bool &need_disconnect) = 0;
65
70 {
71 return session_;
72 }
73
77 struct event &read_event()
78 {
79 return read_event_;
80 }
81
86 const char *addr() const
87 {
88 return addr_.c_str();
89 }
90
91protected:
92 Session *session_ = nullptr;
93 struct event read_event_;
94 std::string addr_;
95 BufferedWriter *writer_ = nullptr;
96 int fd_ = -1;
97};
98
104{
105 PLAIN,
106 CLI,
107 MYSQL,
108};
109
115{
116public:
117 Communicator *create(CommunicateProtocol protocol);
118};
支持以缓存模式写入数据到文件/socket
Definition: buffered_writer.h:26
通讯协议工厂
Definition: communicator.h:115
负责与客户端通讯
Definition: communicator.h:42
Session * session() const
关联的会话信息
Definition: communicator.h:69
const char * addr() const
对端地址 如果是unix socket,可能没有意义
Definition: communicator.h:86
virtual RC init(int fd, Session *session, const std::string &addr)
接收到一个新的连接时,进行初始化
Definition: communicator.cpp:24
virtual RC read_event(SessionEvent *&event)=0
监听到有新的数据到达,调用此函数进行接收消息 如果需要创建新的任务来处理,那么就创建一个SessionEvent 对象并通过event参数返回。
virtual RC write_result(SessionEvent *event, bool &need_disconnect)=0
在任务处理完成后,通过此接口将结果返回给客户端
struct event & read_event()
libevent使用的数据,参考server.cpp
Definition: communicator.h:77
表示一个SQL请求
Definition: session_event.h:32
表示会话
Definition: session.h:28
CommunicateProtocol
当前支持的通讯协议
Definition: communicator.h:104
@ MYSQL
mysql通讯协议。具体实现参考 MysqlCommunicator
@ CLI
与客户端进行交互的协议
@ PLAIN
以'\0'结尾的协议