Compare commits
5 Commits
43c0ef3507
...
0e7240462d
Author | SHA1 | Date | |
---|---|---|---|
0e7240462d | |||
15cb2c14c0 | |||
ee00551116 | |||
93e0d41276 | |||
c180cb9400 |
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,8 +1,8 @@
|
||||
tinypb.pb.cc
|
||||
tinypb.pb.h
|
||||
bin/
|
||||
build/
|
||||
lib/
|
||||
protobuf/
|
||||
third_party/
|
||||
# third_party/
|
||||
*.pb.cc
|
||||
*.pb.h
|
||||
|
||||
|
@ -106,4 +106,5 @@ add_executable(test_tinyrpc
|
||||
${TEST_SRC_LIST}
|
||||
)
|
||||
|
||||
|
||||
target_link_libraries(test_tinyrpc PRIVATE tinyrpc)
|
||||
|
@ -4,17 +4,37 @@
|
||||
|
||||
// #define LOGGER (std::cout << __FILE__ << ":" << __LINE__)
|
||||
|
||||
struct logger {
|
||||
logger() = default;
|
||||
struct Logger {
|
||||
Logger() = default;
|
||||
|
||||
template<typename T>
|
||||
std::ostream& operator << (T&& msg) {
|
||||
template <typename T>
|
||||
std::ostream& operator<<(T&& msg)
|
||||
{
|
||||
return std::cout << msg;
|
||||
}
|
||||
|
||||
~logger() {
|
||||
~Logger()
|
||||
{
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#define logger() (logger() << __FILE__ << ":" << __LINE__ << " ")
|
||||
struct IgnoreLogger {
|
||||
IgnoreLogger() = default;
|
||||
|
||||
template <typename T>
|
||||
IgnoreLogger& operator<<(T&& msg)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
~IgnoreLogger() = default;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#define logger() (Logger() << __FILE__ << ":" << __LINE__ << " ")
|
||||
|
||||
// #define logger1() (Logger() << __FILE__ << ":" << __LINE__ << " ")
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
namespace tinyrpc {
|
||||
class FdEvent;
|
||||
class Coroutine;
|
||||
class Reactor {
|
||||
|
||||
public:
|
||||
@ -26,6 +27,7 @@ namespace tinyrpc {
|
||||
void addFdEvent(FdEvent* fdEvent);
|
||||
void delFdEvent(FdEvent* fdEvent);
|
||||
void modFdEvent(FdEvent* fdEvent);
|
||||
void addCoroutine(Coroutine& cor);
|
||||
void stop();
|
||||
void rouse();
|
||||
void addTask(Task task, bool needRouse = false);
|
||||
|
@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include "logger.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
@ -17,8 +16,7 @@ namespace tinyrpc {
|
||||
}
|
||||
void reserved(std::size_t spaceSize) { // 预留空间
|
||||
if(getWriteable() <= spaceSize) {
|
||||
|
||||
resize((getReadable() + spaceSize) * 2);
|
||||
resize((getReadable() + spaceSize) * 1.5);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,10 +2,8 @@
|
||||
|
||||
#include "abstract_coder.hpp"
|
||||
#include "client_tcp_connect.hpp"
|
||||
#include "coroutine.hpp"
|
||||
#include "net_address.hpp"
|
||||
#include "reactor.hpp"
|
||||
#include "tinypb_data.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace tinyrpc {
|
||||
@ -17,9 +15,9 @@ namespace tinyrpc {
|
||||
const NetAddress& getPeerAddr() const {return m_peer_addr;}
|
||||
bool writeToSendBuffer(const AbstractData& data);
|
||||
int sendAndRecvData(const std::string& msg_req, std::shared_ptr<AbstractData>& res);
|
||||
void addCoroutine(Coroutine& cor);
|
||||
// void addCoroutine(Coroutine& cor);
|
||||
bool connectToServer();
|
||||
void start();
|
||||
// void start();
|
||||
~TcpClient();
|
||||
private:
|
||||
int m_fd{-1};
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "reactor.hpp"
|
||||
#include "coroutine.hpp"
|
||||
#include "fd_event.hpp"
|
||||
#include "logger.hpp"
|
||||
// #include "coroutine_hook.hpp"
|
||||
@ -266,6 +267,14 @@ namespace tinyrpc {
|
||||
rouse();
|
||||
}
|
||||
|
||||
void Reactor::addCoroutine(Coroutine& cor) {
|
||||
|
||||
Reactor::Task task = [&cor] {
|
||||
cor.resume();
|
||||
};
|
||||
addTask(task);
|
||||
}
|
||||
|
||||
Reactor::~Reactor()
|
||||
{
|
||||
m_is_stop = true;
|
||||
|
@ -35,7 +35,7 @@ namespace tinyrpc {
|
||||
}
|
||||
|
||||
void AbstractTcpConnection::clearClient() {
|
||||
logger() << "clearClient";
|
||||
logger() << "clearClient:" << m_fdEvent->getFd();
|
||||
m_state = State::Disconnected;
|
||||
m_reactor.delFdEvent(m_fdEvent);
|
||||
m_fdEvent->reset();
|
||||
|
@ -15,6 +15,7 @@ namespace tinyrpc {
|
||||
m_buffer.swap(newBuffer);
|
||||
m_write_index -= m_read_index;
|
||||
m_read_index = 0;
|
||||
logger() << " adjustBuffer end size=" << m_buffer.size();
|
||||
|
||||
}
|
||||
|
||||
@ -29,7 +30,7 @@ namespace tinyrpc {
|
||||
if(getWriteable() < m_read_index) {
|
||||
adjustBuffer();
|
||||
}
|
||||
|
||||
logger() << " readOffset end size=" << m_buffer.size();
|
||||
}
|
||||
|
||||
void TcpBuffer::writeOffset(std::size_t offset) {
|
||||
@ -43,6 +44,7 @@ namespace tinyrpc {
|
||||
if(getWriteable() < m_read_index) {
|
||||
adjustBuffer();
|
||||
}
|
||||
logger() << " writeOffset end size=" << m_buffer.size();
|
||||
|
||||
}
|
||||
|
||||
@ -91,9 +93,10 @@ namespace tinyrpc {
|
||||
std::vector<char> newBuffer(size);
|
||||
int cnt = std::min(size, getReadable());
|
||||
memcpy(newBuffer.data(), getReadAddress(), cnt);
|
||||
m_buffer.swap(newBuffer);
|
||||
m_write_index = cnt;
|
||||
m_read_index = 0;
|
||||
logger() << " resize end";
|
||||
// logger() << " resize end size=" << m_buffer.size();
|
||||
}
|
||||
|
||||
}
|
@ -30,8 +30,9 @@ namespace tinyrpc {
|
||||
}
|
||||
|
||||
TcpClient::~TcpClient() {
|
||||
logger() << "~TcpClient";
|
||||
m_reactor.delFdEvent(FdEventPool::getInstance()->getFdEvent(m_fd));
|
||||
if(m_fd != -1) close(m_fd);
|
||||
// if(m_fd != -1) close(m_fd);
|
||||
delete m_coder;
|
||||
}
|
||||
bool TcpClient::connectToServer() {
|
||||
@ -74,15 +75,16 @@ namespace tinyrpc {
|
||||
}
|
||||
|
||||
|
||||
void TcpClient::addCoroutine(Coroutine& cor) {
|
||||
Reactor::Task task = [&cor] {
|
||||
cor.resume();
|
||||
};
|
||||
// void TcpClient::addCoroutine(Coroutine& cor) {
|
||||
|
||||
m_reactor.addTask(task);
|
||||
}
|
||||
void TcpClient::start() {
|
||||
Coroutine::getMainCoroutine();
|
||||
m_reactor.loop();
|
||||
}
|
||||
// Reactor::Task task = [&cor] {
|
||||
// cor.resume();
|
||||
// };
|
||||
// m_reactor.addTask(task);
|
||||
// }
|
||||
|
||||
// void TcpClient::start() {
|
||||
// Coroutine::getMainCoroutine();
|
||||
// m_reactor.loop();
|
||||
// }
|
||||
}
|
@ -2,8 +2,6 @@
|
||||
#include "logger.hpp"
|
||||
#include "net_address.hpp"
|
||||
#include "reactor.hpp"
|
||||
#include "tcp_client.hpp"
|
||||
#include "tcp_server.hpp"
|
||||
#include "tinypb.pb.h"
|
||||
#include "tinypb_channel.hpp"
|
||||
#include "tinypb_closure.hpp"
|
||||
@ -12,12 +10,14 @@
|
||||
using namespace std;
|
||||
using namespace tinyrpc;
|
||||
|
||||
int n = 10;
|
||||
int n = 100000;
|
||||
|
||||
void test()
|
||||
{
|
||||
NetAddress addr("127.0.0.1", 9001);
|
||||
TinypbChannel channel(addr);
|
||||
// TcpClient client(NetAddress("127.0.0.1", 9001));
|
||||
|
||||
|
||||
void test1() {
|
||||
TinypbChannel channel(NetAddress("127.0.0.1", 9001));
|
||||
int n = 10;
|
||||
while (n--) {
|
||||
logger() << "============== test no:" << n << "===============";
|
||||
|
||||
@ -55,12 +55,56 @@ void test()
|
||||
}
|
||||
}
|
||||
|
||||
void test()
|
||||
{
|
||||
|
||||
TinypbChannel channel(NetAddress("127.0.0.1", 9001));
|
||||
while (n--) {
|
||||
logger() << "============== test no:" << n << "===============";
|
||||
|
||||
queryNameReq req_name;
|
||||
req_name.set_req_no(20220315);
|
||||
req_name.set_id(1100110001);
|
||||
req_name.set_type(1);
|
||||
queryNameRes res_name;
|
||||
|
||||
queryAgeReq req_age;
|
||||
req_age.set_req_no(00001111);
|
||||
req_age.set_id(6781);
|
||||
queryAgeRes res_age;
|
||||
|
||||
TinypbClosure cb([]() {
|
||||
logger() << "==========================";
|
||||
logger() << "succ call rpc";
|
||||
logger() << "==========================";
|
||||
});
|
||||
|
||||
QueryService_Stub stub(&channel);
|
||||
TinypbController rpc_controller;
|
||||
|
||||
stub.query_name(&rpc_controller, &req_name, &res_name, &cb);
|
||||
|
||||
if (rpc_controller.ErrorCode() != 0) {
|
||||
logger() << "call rpc method query_name failed, errcode=" << rpc_controller.ErrorCode() << ",error=" << rpc_controller.ErrorText();
|
||||
}
|
||||
if (res_name.ret_code() != 0) {
|
||||
logger() << "query name error, errcode=" << res_name.ret_code() << ", res_info=" << res_name.res_info();
|
||||
} else {
|
||||
logger() << "get res_name.age = " << res_name.name();
|
||||
}
|
||||
|
||||
}
|
||||
// test1();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// TcpServer server;
|
||||
TcpClient client(NetAddress("127.0.0.1", 9001));
|
||||
|
||||
Coroutine cor(test);
|
||||
client.addCoroutine(cor);
|
||||
client.start();
|
||||
|
||||
Reactor* reactor = Reactor::getReactor();
|
||||
reactor->addCoroutine(cor);
|
||||
reactor->loop();
|
||||
|
||||
return 0;
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
#include "tcp_server.hpp"
|
||||
#include "tinypb.pb.h"
|
||||
#include <google/protobuf/service.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace tinyrpc;
|
||||
|
||||
@ -25,7 +25,7 @@ public:
|
||||
|
||||
response->set_req_no(request->req_no());
|
||||
response->set_id(request->id());
|
||||
response->set_name("yyy");
|
||||
response->set_name("11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111y");
|
||||
|
||||
done->Run();
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
2
third_party/abseil-cpp
vendored
2
third_party/abseil-cpp
vendored
@ -1 +1 @@
|
||||
Subproject commit d7aaad83b488fd62bd51c81ecf16cd938532cc0a
|
||||
Subproject commit f6ac87ae3250f4ad9c427f466fabecc704fd2781
|
Loading…
Reference in New Issue
Block a user