Compare commits
	
		
			No commits in common. "0e7240462d8927578ff6830866abb64f80330706" and "43c0ef35075f6c99543f866065d61afe9d6cea18" have entirely different histories.
		
	
	
		
			0e7240462d
			...
			43c0ef3507
		
	
		
							
								
								
									
										6
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -1,8 +1,8 @@ | |||||||
|  | tinypb.pb.cc | ||||||
|  | tinypb.pb.h | ||||||
| bin/ | bin/ | ||||||
| build/ | build/ | ||||||
| lib/ | lib/ | ||||||
| protobuf/ | protobuf/ | ||||||
| # third_party/ | third_party/ | ||||||
| *.pb.cc |  | ||||||
| *.pb.h |  | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -106,5 +106,4 @@ add_executable(test_tinyrpc | |||||||
|         ${TEST_SRC_LIST} |         ${TEST_SRC_LIST} | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| target_link_libraries(test_tinyrpc PRIVATE tinyrpc) | target_link_libraries(test_tinyrpc PRIVATE tinyrpc) | ||||||
|  | |||||||
| @ -4,37 +4,17 @@ | |||||||
| 
 | 
 | ||||||
| // #define LOGGER (std::cout << __FILE__ << ":" << __LINE__)
 | // #define LOGGER (std::cout << __FILE__ << ":" << __LINE__)
 | ||||||
| 
 | 
 | ||||||
| struct Logger { | struct logger { | ||||||
|     Logger() = default; |     logger() = default; | ||||||
| 
 | 
 | ||||||
|     template<typename T> |     template<typename T> | ||||||
|     std::ostream& operator<<(T&& msg) |     std::ostream& operator << (T&& msg) { | ||||||
|     { |  | ||||||
|         return std::cout << msg; |         return std::cout << msg; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     ~Logger() |     ~logger() { | ||||||
|     { |  | ||||||
|         std::cout << std::endl; |         std::cout << std::endl; | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| struct IgnoreLogger { | #define logger() (logger() << __FILE__ << ":" << __LINE__ << " ") | ||||||
|     IgnoreLogger() = default; |  | ||||||
| 
 |  | ||||||
|     template <typename T> |  | ||||||
|     IgnoreLogger& operator<<(T&& msg) |  | ||||||
|     { |  | ||||||
|         return *this; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     ~IgnoreLogger() = default; |  | ||||||
|      |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| #define logger() (Logger() << __FILE__ << ":" << __LINE__ << " ") |  | ||||||
| 
 |  | ||||||
| // #define logger1() (Logger() << __FILE__ << ":" << __LINE__ << " ")
 |  | ||||||
| @ -11,7 +11,6 @@ | |||||||
| 
 | 
 | ||||||
| namespace tinyrpc { | namespace tinyrpc { | ||||||
|     class FdEvent; |     class FdEvent; | ||||||
|     class Coroutine; |  | ||||||
|     class Reactor { |     class Reactor { | ||||||
|          |          | ||||||
|     public: |     public: | ||||||
| @ -27,7 +26,6 @@ namespace tinyrpc { | |||||||
|         void addFdEvent(FdEvent* fdEvent); |         void addFdEvent(FdEvent* fdEvent); | ||||||
|         void delFdEvent(FdEvent* fdEvent); |         void delFdEvent(FdEvent* fdEvent); | ||||||
|         void modFdEvent(FdEvent* fdEvent); |         void modFdEvent(FdEvent* fdEvent); | ||||||
|         void addCoroutine(Coroutine& cor); |  | ||||||
|         void stop(); |         void stop(); | ||||||
|         void rouse(); |         void rouse(); | ||||||
|         void addTask(Task task, bool needRouse = false); |         void addTask(Task task, bool needRouse = false); | ||||||
|  | |||||||
| @ -1,4 +1,5 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  | #include "logger.hpp" | ||||||
| #include <cstddef> | #include <cstddef> | ||||||
| #include <cstring> | #include <cstring> | ||||||
| #include <vector> | #include <vector> | ||||||
| @ -16,7 +17,8 @@ namespace tinyrpc { | |||||||
|         } |         } | ||||||
|         void reserved(std::size_t spaceSize) { // 预留空间
 |         void reserved(std::size_t spaceSize) { // 预留空间
 | ||||||
|             if(getWriteable() <= spaceSize) { |             if(getWriteable() <= spaceSize) { | ||||||
|                 resize((getReadable() + spaceSize) * 1.5); |                  | ||||||
|  |                 resize((getReadable() + spaceSize) * 2); | ||||||
|             } |             } | ||||||
|              |              | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -2,8 +2,10 @@ | |||||||
| 
 | 
 | ||||||
| #include "abstract_coder.hpp" | #include "abstract_coder.hpp" | ||||||
| #include "client_tcp_connect.hpp" | #include "client_tcp_connect.hpp" | ||||||
|  | #include "coroutine.hpp" | ||||||
| #include "net_address.hpp" | #include "net_address.hpp" | ||||||
| #include "reactor.hpp" | #include "reactor.hpp" | ||||||
|  | #include "tinypb_data.hpp" | ||||||
| #include <memory> | #include <memory> | ||||||
| 
 | 
 | ||||||
| namespace tinyrpc { | namespace tinyrpc { | ||||||
| @ -15,9 +17,9 @@ namespace tinyrpc { | |||||||
|         const NetAddress& getPeerAddr() const {return m_peer_addr;} |         const NetAddress& getPeerAddr() const {return m_peer_addr;} | ||||||
|         bool writeToSendBuffer(const AbstractData& data); |         bool writeToSendBuffer(const AbstractData& data); | ||||||
|         int sendAndRecvData(const std::string& msg_req, std::shared_ptr<AbstractData>& res); |         int sendAndRecvData(const std::string& msg_req, std::shared_ptr<AbstractData>& res); | ||||||
|         // void addCoroutine(Coroutine& cor);
 |         void addCoroutine(Coroutine& cor); | ||||||
|         bool connectToServer(); |         bool connectToServer(); | ||||||
|         // void start();
 |         void start(); | ||||||
|         ~TcpClient(); |         ~TcpClient(); | ||||||
|     private: |     private: | ||||||
|         int m_fd{-1}; |         int m_fd{-1}; | ||||||
|  | |||||||
| @ -1,5 +1,4 @@ | |||||||
| #include "reactor.hpp" | #include "reactor.hpp" | ||||||
| #include "coroutine.hpp" |  | ||||||
| #include "fd_event.hpp" | #include "fd_event.hpp" | ||||||
| #include "logger.hpp" | #include "logger.hpp" | ||||||
| // #include "coroutine_hook.hpp"
 | // #include "coroutine_hook.hpp"
 | ||||||
| @ -267,14 +266,6 @@ namespace tinyrpc { | |||||||
|             rouse(); |             rouse(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     void Reactor::addCoroutine(Coroutine& cor) { |  | ||||||
| 
 |  | ||||||
|         Reactor::Task task = [&cor] { |  | ||||||
|             cor.resume(); |  | ||||||
|         }; |  | ||||||
|         addTask(task); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     Reactor::~Reactor() |     Reactor::~Reactor() | ||||||
|     { |     { | ||||||
|         m_is_stop = true; |         m_is_stop = true; | ||||||
|  | |||||||
| @ -35,7 +35,7 @@ namespace tinyrpc { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     void AbstractTcpConnection::clearClient() { |     void AbstractTcpConnection::clearClient() { | ||||||
|         logger() << "clearClient:" << m_fdEvent->getFd(); |         logger() << "clearClient"; | ||||||
|         m_state = State::Disconnected; |         m_state = State::Disconnected; | ||||||
|         m_reactor.delFdEvent(m_fdEvent); |         m_reactor.delFdEvent(m_fdEvent); | ||||||
|         m_fdEvent->reset(); |         m_fdEvent->reset(); | ||||||
|  | |||||||
| @ -15,7 +15,6 @@ namespace tinyrpc { | |||||||
|         m_buffer.swap(newBuffer); |         m_buffer.swap(newBuffer); | ||||||
|         m_write_index -= m_read_index; |         m_write_index -= m_read_index; | ||||||
|         m_read_index = 0; |         m_read_index = 0; | ||||||
|         logger() << " adjustBuffer end size=" << m_buffer.size(); |  | ||||||
|          |          | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -30,7 +29,7 @@ namespace tinyrpc { | |||||||
|         if(getWriteable() < m_read_index) { |         if(getWriteable() < m_read_index) { | ||||||
|             adjustBuffer(); |             adjustBuffer(); | ||||||
|         } |         } | ||||||
|         logger() << " readOffset end size=" << m_buffer.size(); | 
 | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     void TcpBuffer::writeOffset(std::size_t offset)  { |     void TcpBuffer::writeOffset(std::size_t offset)  { | ||||||
| @ -44,7 +43,6 @@ namespace tinyrpc { | |||||||
|         if(getWriteable() < m_read_index) { |         if(getWriteable() < m_read_index) { | ||||||
|             adjustBuffer(); |             adjustBuffer(); | ||||||
|         } |         } | ||||||
|         logger() << " writeOffset end size=" << m_buffer.size(); |  | ||||||
| 
 | 
 | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -93,10 +91,9 @@ namespace tinyrpc { | |||||||
|         std::vector<char> newBuffer(size); |         std::vector<char> newBuffer(size); | ||||||
|         int cnt = std::min(size, getReadable()); |         int cnt = std::min(size, getReadable()); | ||||||
|         memcpy(newBuffer.data(), getReadAddress(), cnt); |         memcpy(newBuffer.data(), getReadAddress(), cnt); | ||||||
|         m_buffer.swap(newBuffer); |  | ||||||
|         m_write_index = cnt; |         m_write_index = cnt; | ||||||
|         m_read_index = 0; |         m_read_index = 0; | ||||||
|         // logger() << " resize end size=" << m_buffer.size();
 |         logger() << " resize end"; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
| @ -30,9 +30,8 @@ namespace tinyrpc { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     TcpClient::~TcpClient() { |     TcpClient::~TcpClient() { | ||||||
|         logger() << "~TcpClient"; |  | ||||||
|         m_reactor.delFdEvent(FdEventPool::getInstance()->getFdEvent(m_fd)); |         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; |         delete m_coder; | ||||||
|     } |     } | ||||||
|     bool TcpClient::connectToServer() { |     bool TcpClient::connectToServer() { | ||||||
| @ -75,16 +74,15 @@ namespace tinyrpc { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     // void TcpClient::addCoroutine(Coroutine& cor) {
 |     void TcpClient::addCoroutine(Coroutine& cor) { | ||||||
|  |         Reactor::Task task = [&cor] { | ||||||
|  |             cor.resume(); | ||||||
|  |         }; | ||||||
| 
 | 
 | ||||||
|     //     Reactor::Task task = [&cor] {
 |         m_reactor.addTask(task); | ||||||
|     //         cor.resume();
 |     } | ||||||
|     //     };
 |     void TcpClient::start() { | ||||||
|     //     m_reactor.addTask(task);
 |         Coroutine::getMainCoroutine(); | ||||||
|     // }
 |         m_reactor.loop(); | ||||||
| 
 |     } | ||||||
|     // void TcpClient::start() {
 |  | ||||||
|     //     Coroutine::getMainCoroutine();
 |  | ||||||
|     //     m_reactor.loop();
 |  | ||||||
|     // }
 |  | ||||||
| } | } | ||||||
| @ -2,6 +2,8 @@ | |||||||
| #include "logger.hpp" | #include "logger.hpp" | ||||||
| #include "net_address.hpp" | #include "net_address.hpp" | ||||||
| #include "reactor.hpp" | #include "reactor.hpp" | ||||||
|  | #include "tcp_client.hpp" | ||||||
|  | #include "tcp_server.hpp" | ||||||
| #include "tinypb.pb.h" | #include "tinypb.pb.h" | ||||||
| #include "tinypb_channel.hpp" | #include "tinypb_channel.hpp" | ||||||
| #include "tinypb_closure.hpp" | #include "tinypb_closure.hpp" | ||||||
| @ -10,55 +12,12 @@ | |||||||
| using namespace std; | using namespace std; | ||||||
| using namespace tinyrpc; | using namespace tinyrpc; | ||||||
| 
 | 
 | ||||||
| int n = 100000; |  | ||||||
| 
 |  | ||||||
| // TcpClient client(NetAddress("127.0.0.1", 9001));
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| void test1() { |  | ||||||
|     TinypbChannel channel(NetAddress("127.0.0.1", 9001)); |  | ||||||
| int n = 10; | int n = 10; | ||||||
|     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(); |  | ||||||
|         } |  | ||||||
|          |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| void test() | void test() | ||||||
| { | { | ||||||
|      |     NetAddress addr("127.0.0.1", 9001); | ||||||
|     TinypbChannel channel(NetAddress("127.0.0.1", 9001)); |     TinypbChannel channel(addr); | ||||||
|     while (n--) { |     while (n--) { | ||||||
|         logger() << "============== test no:" << n << "==============="; |         logger() << "============== test no:" << n << "==============="; | ||||||
| 
 | 
 | ||||||
| @ -94,17 +53,14 @@ void test() | |||||||
|         } |         } | ||||||
|          |          | ||||||
|     } |     } | ||||||
|     // test1();
 |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int main() | int main() | ||||||
| { | { | ||||||
| 
 |     // TcpServer server;
 | ||||||
|  |     TcpClient client(NetAddress("127.0.0.1", 9001)); | ||||||
|     Coroutine cor(test); |     Coroutine cor(test); | ||||||
| 
 |     client.addCoroutine(cor); | ||||||
|     Reactor* reactor = Reactor::getReactor(); |     client.start(); | ||||||
|     reactor->addCoroutine(cor); |  | ||||||
|     reactor->loop(); |  | ||||||
| 
 |  | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
| @ -2,7 +2,7 @@ | |||||||
| #include "tcp_server.hpp" | #include "tcp_server.hpp" | ||||||
| #include "tinypb.pb.h" | #include "tinypb.pb.h" | ||||||
| #include <google/protobuf/service.h> | #include <google/protobuf/service.h> | ||||||
| 
 | #include <iostream> | ||||||
| using namespace std; | using namespace std; | ||||||
| using namespace tinyrpc; | using namespace tinyrpc; | ||||||
| 
 | 
 | ||||||
| @ -25,7 +25,7 @@ public: | |||||||
|          |          | ||||||
|         response->set_req_no(request->req_no()); |         response->set_req_no(request->req_no()); | ||||||
|         response->set_id(request->id()); |         response->set_id(request->id()); | ||||||
|         response->set_name("11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111y"); |         response->set_name("yyy"); | ||||||
|          |          | ||||||
|         done->Run(); |         done->Run(); | ||||||
|     } |     } | ||||||
|  | |||||||
							
								
								
									
										1439
									
								
								test/servertest/tinypb.pb.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1439
									
								
								test/servertest/tinypb.pb.cc
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										1383
									
								
								test/servertest/tinypb.pb.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1383
									
								
								test/servertest/tinypb.pb.h
									
									
									
									
									
										Normal file
									
								
							
										
											
												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 f6ac87ae3250f4ad9c427f466fabecc704fd2781 | Subproject commit d7aaad83b488fd62bd51c81ecf16cd938532cc0a | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user