├── .gitignore ├── .idea ├── .gitignore ├── compiler.xml ├── encodings.xml ├── jarRepositories.xml ├── misc.xml ├── uiDesigner.xml └── vcs.xml ├── LICENSE ├── README.md ├── pom.xml ├── qrpc-core ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── qrpc │ │ ├── Banner.java │ │ ├── ConfigEnvironment.java │ │ ├── Environment.java │ │ ├── EventCaster.java │ │ ├── Factory.java │ │ ├── Listener.java │ │ ├── Meta.java │ │ ├── PropertyKeys.java │ │ ├── RpcStatus.java │ │ ├── ThreadService.java │ │ ├── client │ │ ├── Channel │ │ │ ├── QRpcChannel.java │ │ │ ├── SimpleBlockQRpcChannel.java │ │ │ └── SimpleQRpcChannel.java │ │ ├── ConnectionConfig.java │ │ ├── ConnectionFactory.java │ │ ├── EncryptType.java │ │ ├── Endpoint.java │ │ ├── QRpcClientEndpoint.java │ │ ├── RequestCache.java │ │ ├── RequestFuture.java │ │ └── ResponseHandler.java │ │ ├── exception │ │ ├── ClientChannelNotActive.java │ │ ├── DuplicateServiceException.java │ │ └── UnKnownResponseException.java │ │ ├── net │ │ ├── NettyClientEndpoint.java │ │ ├── NettyClientInboundHandler.java │ │ ├── NettyServerInboundHandler.java │ │ ├── NettyServerSender.java │ │ ├── NettyTCPServer.java │ │ ├── Receiver.java │ │ ├── Responsible.java │ │ ├── Sender.java │ │ └── Server.java │ │ ├── server │ │ ├── AbstractServer.java │ │ ├── ListableServiceFactory.java │ │ ├── MethodInfoHolder.java │ │ ├── QRpcController.java │ │ ├── RespondCallBack.java │ │ ├── ServerBuilder.java │ │ ├── ServiceExecutor.java │ │ ├── ServiceFactory.java │ │ ├── ServiceHolder.java │ │ └── ServiceRunner.java │ │ └── utils │ │ └── ResponseManager.java │ └── resources │ └── meta.proto ├── qrpc-demo ├── pom.xml └── src │ └── main │ ├── java │ ├── Client.java │ ├── Demo.java │ ├── DemoServiceImpl.java │ └── Server.java │ └── resources │ └── demo.proto └── qrpc-project.iml /.gitignore: -------------------------------------------------------------------------------- 1 | **/target/** 2 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | 34 | 35 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | qRPC简介 2 | ------ 3 | 4 | qRPC是一个基于TCP协议、基于Protobuf序列化和代码生成,跨语言的高效率轻量级RPC框架。 5 | 6 | 框架具有如下特点: 7 | 8 | * 基于TCP协议的非阻塞IO(NIO)实现底层网络通信,吞吐量高。 9 | 10 | 11 | * 基于Protobuf序列化,紧凑高效、高性能、跨语言、可拓展。 12 | 13 | 14 | * 通过IDL(Interface Define Language)进行接口定义并生成代码 15 | 16 | 17 | * 跨语言,支持Java、Go、C++、Python等多种语言 18 | 19 | 20 | 使用方法 21 | ---- 22 | 23 | 需要了解RPC(Rmote Procedure Call),会用Protobuf([Proto3官方文档](https://protobuf.dev/programming-guides/proto3/ "Proto3官方文档")),如果你使用过Google gRPC,你将会非常容易上手。 24 | 25 | #### Java: 26 | 27 | * 引入Maven依赖 28 | 29 | ``` 30 | 31 | io.github.barackyoung 32 | qrpc-core 33 | 1.0.0.SNAPSHOT 34 | 35 | ``` 36 | 37 | * 定义数据结构和服务接口 38 | 39 | ``` 40 | Syntax="proto3"; 41 | Option Java_Multiple_Files=false; 42 | Option Java_Package="com.qrpc.demo"; 43 | Option Java_Outer_Classname="Demo"; 44 | Option Java_Generic_Services=true; 45 | Package qrpc.demo; 46 | 47 | Message request { 48 | string message=1; 49 | } 50 | 51 | Message response { 52 | string message=1; 53 | } 54 | Service DemoService{ 55 | Rpc sendMessage (request) returns (response) {}; 56 | } 57 | ``` 58 | 59 | * 生成数据结构和接口定义 60 | 61 | 62 | ``` 63 | Protoc -- java out=-- proto path= 64 | ``` 65 | 66 | * 服务端实现接口 67 | 68 | 69 | ``` 70 | Public class DemoServiceImpl extensions Demo.DemoService { 71 | @Overrides 72 | Public void sendMessage(RpcController controller, Demo.request request, RpcCallback done){ 73 | System.out.println("received from client, message:"+request. getMessage ()); 74 | Demo.response response = Demo.response.newBuilder().setMessage("hi client").build(); 75 | Done.run(response); 76 | } 77 | } 78 | ``` 79 | 80 | * 客户端调用服务接口 81 | 82 | 83 | ``` 84 | Demo.requestrequest=Demo.request.newBuilder() 85 | .setMessage("hi, server!") 86 | .build(); 87 | 88 | //Synchronous call 89 | UsageDemo.DemoService.BlockingInterface blockingStub=Demo.DemoService.newBlockingStub(SimpleBlockQRpcChannel.forAddress("127.0.0.1", 8888).build()); 90 | Demo.response response = blockingStub.sendMessage(null, request); 91 | System.out.println("synchronous response:" + response.getMessage ()); 92 | 93 | //Asynchronous call 94 | UsageDemo.DemoService.Stub stub = Demo.DemoService.newStub(SimpleQRpcChannel.forAddress("127.0.0.1", 8888).build()); 95 | Stub.sendMessage(null, request, new RpcCallback() { 96 | @ Override 97 | Public void run(Demo.response parameter){ 98 | System.out.println ("asynchronous response:"+response. getMessage ()); 99 | } 100 | }); 101 | ``` 102 | 103 | #### Go: TODO 104 | 105 | #### C++: TODO 106 | 107 | 待开发点 108 | ---- 109 | 110 | * spring-boot-starter,springboot自动配置,支持与springboot无缝结合 111 | 112 | 113 | * 服务治理(支持ectd、zookeeper等服务发现),负载均衡调用 114 | 115 | 116 | * 配置系统,比如线程数等参数可配置 117 | 118 | 119 | * 云原生相关支持 120 | 121 | 122 | * 测试用例 123 | 124 | 125 | 提示 126 | -- 127 | 128 | 项目处于起步阶段,代码易读懂,欢迎广大社会程序员、学生一起加入,一起学习造轮子,把理论与实践相结合,一起提高! 129 | 130 | 131 | 132 | Introduction to qRPC 133 | ------ 134 | QRPC is an efficient and lightweight RPC framework based on the TCP protocol, Protobuf serialization, and code generation, spanning multiple languages. 135 | The framework has the following characteristics: 136 | * Non blocking IO (NIO) based on TCP protocol enables low-level network communication with high throughput. 137 | * Based on Protobuf serialization, it is compact, efficient, high-performance, cross language, and scalable. 138 | * Define interfaces and generate code through IDL (Interface Definition Language) 139 | * Cross language support for multiple languages such as Java, Go, C++, Python, etc 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | Usage 151 | ---- 152 | Need to understand RPC (Rmote Procedure Call) and use Protobuf ([Proto3 official document]( https://protobuf.dev/programming-guides/proto3/ If you have used Google gRPC, it will be very easy for you to get started. 153 | ####Java: 154 | * Introducing Maven dependencies 155 | ``` 156 | 157 | io.github.barackyoung 158 | qrpc-core 159 | 1.0.0.SNAPSHOT 160 | 161 | ``` 162 | * Define data structures and service interfaces 163 | ``` 164 | Syntax="proto3"; 165 | Option Java_Multiple_Files=false; 166 | Option Java_Package="com.qrpc.demo"; 167 | Option Java_Outer_Classname="Demo"; 168 | Option Java_Generic_Services=true; 169 | Package qrpc.demo; 170 | 171 | Message request { 172 | string message=1; 173 | } 174 | 175 | Message response { 176 | string message=1; 177 | } 178 | Service DemoService{ 179 | Rpc sendMessage (request) returns (response) {}; 180 | } 181 | ``` 182 | * Generate data structures and interface definitions 183 | ``` 184 | Protoc -- java out=-- proto path= 185 | ``` 186 | * Server implementation interface 187 | ``` 188 | Public class DemoServiceImpl extensions Demo.DemoService { 189 | @Overrides 190 | Public void sendMessage(RpcController controller, Demo.request request, RpcCallback done){ 191 | System.out.println("received from client, message:"+request. getMessage ()); 192 | Demo.response response = Demo.response.newBuilder().setMessage("hi client").build(); 193 | Done.run(response); 194 | } 195 | } 196 | ``` 197 | * Client Call Service Interface 198 | ``` 199 | Demo.requestrequest=Demo.request.newBuilder() 200 | .setMessage("hi, server!") 201 | .build(); 202 | 203 | //Synchronous call 204 | UsageDemo.DemoService.BlockingInterface blockingStub=Demo.DemoService.newBlockingStub(SimpleBlockQRpcChannel.forAddress("127.0.0.1", 8888).build()); 205 | Demo.response response = blockingStub.sendMessage(null, request); 206 | System.out.println("synchronous response:" + response.getMessage ()); 207 | 208 | //Asynchronous call 209 | UsageDemo.DemoService.Stub stub = Demo.DemoService.newStub(SimpleQRpcChannel.forAddress("127.0.0.1", 8888).build()); 210 | Stub.sendMessage(null, request, new RpcCallback() { 211 | @ Override 212 | Public void run(Demo.response parameter){ 213 | System.out.println ("asynchronous response:"+response. getMessage ()); 214 | } 215 | }); 216 | ``` 217 | #### Go: TODO 218 | #### C++: TODO 219 | 220 | 221 | Points to be developed 222 | ---- 223 | * Spring boot starter, automatically configured for spring boot, supports seamless integration with spring boot 224 | * Service governance (supporting service discovery such as ectd and zookeeper), load balancing calls 225 | * Configure the system, such as configurable parameters such as thread count 226 | * Cloud native related support 227 | * Test Cases 228 | 229 | prompt 230 | -- 231 | The project is in its early stages and the code is easy to read. We welcome programmers and students from all walks of life to join us and learn how to build wheels together. We will combine theory with practice to improve together! -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.barackyoung 8 | qrpc 9 | 1.0.0-SNAPSHOT 10 | pom 11 | 12 | qrpc-core 13 | qrpc-demo 14 | 15 | 16 | qrpc 17 | a light multi language RPC framework 18 | https://github.com/BarackYoung/qRPC 19 | 20 | 21 | 22 | 23 | The Apache Software License, Version 2.0 24 | http://www.apache.org/licenses/LICENSE-2.0.txt 25 | repo 26 | A business-friendly OSS license 27 | 28 | 29 | 30 | 31 | main 32 | https://github.com/BarackYoung/qRPC.git 33 | scm:https://github.com/BarackYoung/qRPC.git 34 | scm:git:git@github.com:BarackYoung/qRPC.git 35 | 36 | 37 | 38 | 39 | Lianhuan Yang 40 | 1330292703@qq.com 41 | https://github.com/BarackYoung/ 42 | 43 | 44 | 45 | 46 | 47 | oss 48 | https://s01.oss.sonatype.org/content/repositories/snapshots 49 | 50 | 51 | oss 52 | https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.apache.maven.plugins 60 | maven-compiler-plugin 61 | 62 | 1.8 63 | 1.8 64 | 65 | 66 | 67 | 68 | org.apache.maven.plugins 69 | maven-source-plugin 70 | 2.2.1 71 | 72 | 73 | package 74 | 75 | jar-no-fork 76 | 77 | 78 | 79 | 80 | 81 | 82 | org.apache.maven.plugins 83 | maven-javadoc-plugin 84 | 2.10.4 85 | 86 | 87 | -Xdoclint:none 88 | 89 | 90 | 91 | 92 | package 93 | 94 | jar 95 | 96 | 97 | 98 | 99 | 100 | 101 | org.apache.maven.plugins 102 | maven-gpg-plugin 103 | 1.6 104 | 105 | 106 | --pinentry-mode 107 | loopback 108 | 109 | 110 | 111 | 112 | sign-artifacts 113 | verify 114 | 115 | sign 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /qrpc-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.barackyoung 8 | qrpc 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | qrpc-core 13 | 14 | 15 | 8 16 | 8 17 | UTF-8 18 | 4.1.106.Final 19 | 3.22.2 20 | 2.0.5 21 | 1.1.3 22 | 1.18.16 23 | 4.13.2 24 | 25 | 26 | 27 | 28 | io.netty 29 | netty-all 30 | ${netty.version} 31 | 32 | 33 | com.google.protobuf 34 | protobuf-java 35 | ${protobuf.java.version} 36 | 37 | 38 | org.slf4j 39 | slf4j-api 40 | 1.7.36 41 | 42 | 43 | org.slf4j 44 | slf4j-simple 45 | 1.7.36 46 | 47 | 48 | org.projectlombok 49 | lombok 50 | ${lombok.version} 51 | compile 52 | 53 | 54 | junit 55 | junit 56 | ${junit.version} 57 | test 58 | 59 | 60 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/Banner.java: -------------------------------------------------------------------------------- 1 | package org.qrpc; 2 | 3 | /** 4 | * @author Yang Lianhuan 5 | * @version 1.0.0 6 | * @since 2023/4/4 7 | **/ 8 | public class Banner { 9 | public static void print() { 10 | System.out.println( 11 | " ---- ----- ----- -------| \n" + 12 | " / -- \\\\ / ---- \\\\ / \\\\ / /------| \n" + 13 | " | | | | | | | | | | | | | | \n" + 14 | " | | | | | | ---| | | |---/ / | | \n" + 15 | " | | | |\\\\ | |\\\\ \\\\ | | | |------| \n" + 16 | " \\ --// \\\\--\\\\ | | \\\\-\\\\ | | \\ \\----| \n"); 17 | } 18 | 19 | public static void printStarted(int port) { 20 | System.out.println("qRPC Server started at port: " + port); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/ConfigEnvironment.java: -------------------------------------------------------------------------------- 1 | package org.qrpc; 2 | 3 | import java.util.Objects; 4 | import java.util.concurrent.ConcurrentHashMap; 5 | 6 | /** 7 | * @author Yang Lianhuan 8 | * @version 1.0.0 9 | * @since 2024/2/3 10 | **/ 11 | public class ConfigEnvironment implements Environment { 12 | 13 | private ConfigEnvironment() {} 14 | private static Environment configEnvironment; 15 | private static final ConcurrentHashMap configs = new ConcurrentHashMap<>(); 16 | 17 | static { 18 | configs.put(PropertyKeys.CLIENT_TIMEOUT, "300000"); 19 | } 20 | 21 | @Override 22 | public Object getProperty(String key) { 23 | return configs.get(key); 24 | } 25 | 26 | public static Environment getInstance() { 27 | if (!Objects.isNull(configEnvironment)) { 28 | return configEnvironment; 29 | } 30 | synchronized (ConfigEnvironment.class) { 31 | if (!Objects.isNull(configEnvironment)) { 32 | return configEnvironment; 33 | } 34 | configEnvironment = new ConfigEnvironment(); 35 | return configEnvironment; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/Environment.java: -------------------------------------------------------------------------------- 1 | package org.qrpc; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author Yang Lianhuan 7 | * @version 1.0.0 8 | * @since 2023/3/26 9 | **/ 10 | public interface Environment { 11 | Object getProperty(String key); 12 | } 13 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/EventCaster.java: -------------------------------------------------------------------------------- 1 | package org.qrpc; 2 | 3 | /** 4 | * 事件分发器 5 | * 6 | * @author Yang Lianhuan 7 | * @version 1.0.0 8 | * @since 2024/1/26 9 | **/ 10 | public class EventCaster { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/Factory.java: -------------------------------------------------------------------------------- 1 | package org.qrpc; 2 | 3 | import com.google.protobuf.Service; 4 | 5 | /** 6 | * @author Yang Lianhuan 7 | * @version 1.0.0 8 | * @since 2023/3/26 9 | **/ 10 | public interface Factory { 11 | void register(String key, T value); 12 | 13 | boolean contains(String key); 14 | 15 | T get(String key); 16 | 17 | void remove(String key); 18 | } 19 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/Listener.java: -------------------------------------------------------------------------------- 1 | package org.qrpc; 2 | 3 | /** 4 | * @author Yang Lianhuan 5 | * @version 1.0.0 6 | * @since 2024/1/26 7 | **/ 8 | public interface Listener { 9 | 10 | /** 11 | * 启动监听器 12 | */ 13 | void run(); 14 | } 15 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/Meta.java: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: meta.proto 3 | 4 | package org.qrpc; 5 | 6 | public final class Meta { 7 | private Meta() {} 8 | public static void registerAllExtensions( 9 | com.google.protobuf.ExtensionRegistryLite registry) { 10 | } 11 | 12 | public static void registerAllExtensions( 13 | com.google.protobuf.ExtensionRegistry registry) { 14 | registerAllExtensions( 15 | (com.google.protobuf.ExtensionRegistryLite) registry); 16 | } 17 | /** 18 | * Protobuf enum {@code qrpc.meta.Status} 19 | */ 20 | public enum Status 21 | implements com.google.protobuf.ProtocolMessageEnum { 22 | /** 23 | * SUCCESS = 0; 24 | */ 25 | SUCCESS(0), 26 | /** 27 | * TIME_OUT = 2; 28 | */ 29 | TIME_OUT(2), 30 | /** 31 | * SERVICE_NOT_FOUND = 3; 32 | */ 33 | SERVICE_NOT_FOUND(3), 34 | /** 35 | * SYSTEM_ERROR = 4; 36 | */ 37 | SYSTEM_ERROR(4), 38 | UNRECOGNIZED(-1), 39 | ; 40 | 41 | /** 42 | * SUCCESS = 0; 43 | */ 44 | public static final int SUCCESS_VALUE = 0; 45 | /** 46 | * TIME_OUT = 2; 47 | */ 48 | public static final int TIME_OUT_VALUE = 2; 49 | /** 50 | * SERVICE_NOT_FOUND = 3; 51 | */ 52 | public static final int SERVICE_NOT_FOUND_VALUE = 3; 53 | /** 54 | * SYSTEM_ERROR = 4; 55 | */ 56 | public static final int SYSTEM_ERROR_VALUE = 4; 57 | 58 | 59 | public final int getNumber() { 60 | if (this == UNRECOGNIZED) { 61 | throw new java.lang.IllegalArgumentException( 62 | "Can't get the number of an unknown enum value."); 63 | } 64 | return value; 65 | } 66 | 67 | /** 68 | * @param value The numeric wire value of the corresponding enum entry. 69 | * @return The enum associated with the given numeric wire value. 70 | * @deprecated Use {@link #forNumber(int)} instead. 71 | */ 72 | @java.lang.Deprecated 73 | public static Status valueOf(int value) { 74 | return forNumber(value); 75 | } 76 | 77 | /** 78 | * @param value The numeric wire value of the corresponding enum entry. 79 | * @return The enum associated with the given numeric wire value. 80 | */ 81 | public static Status forNumber(int value) { 82 | switch (value) { 83 | case 0: return SUCCESS; 84 | case 2: return TIME_OUT; 85 | case 3: return SERVICE_NOT_FOUND; 86 | case 4: return SYSTEM_ERROR; 87 | default: return null; 88 | } 89 | } 90 | 91 | public static com.google.protobuf.Internal.EnumLiteMap 92 | internalGetValueMap() { 93 | return internalValueMap; 94 | } 95 | private static final com.google.protobuf.Internal.EnumLiteMap< 96 | Status> internalValueMap = 97 | new com.google.protobuf.Internal.EnumLiteMap() { 98 | public Status findValueByNumber(int number) { 99 | return Status.forNumber(number); 100 | } 101 | }; 102 | 103 | public final com.google.protobuf.Descriptors.EnumValueDescriptor 104 | getValueDescriptor() { 105 | if (this == UNRECOGNIZED) { 106 | throw new java.lang.IllegalStateException( 107 | "Can't get the descriptor of an unrecognized enum value."); 108 | } 109 | return getDescriptor().getValues().get(ordinal()); 110 | } 111 | public final com.google.protobuf.Descriptors.EnumDescriptor 112 | getDescriptorForType() { 113 | return getDescriptor(); 114 | } 115 | public static final com.google.protobuf.Descriptors.EnumDescriptor 116 | getDescriptor() { 117 | return org.qrpc.Meta.getDescriptor().getEnumTypes().get(0); 118 | } 119 | 120 | private static final Status[] VALUES = values(); 121 | 122 | public static Status valueOf( 123 | com.google.protobuf.Descriptors.EnumValueDescriptor desc) { 124 | if (desc.getType() != getDescriptor()) { 125 | throw new java.lang.IllegalArgumentException( 126 | "EnumValueDescriptor is not for this type."); 127 | } 128 | if (desc.getIndex() == -1) { 129 | return UNRECOGNIZED; 130 | } 131 | return VALUES[desc.getIndex()]; 132 | } 133 | 134 | private final int value; 135 | 136 | private Status(int value) { 137 | this.value = value; 138 | } 139 | 140 | // @@protoc_insertion_point(enum_scope:qrpc.meta.Status) 141 | } 142 | 143 | /** 144 | * Protobuf enum {@code qrpc.meta.Type} 145 | */ 146 | public enum Type 147 | implements com.google.protobuf.ProtocolMessageEnum { 148 | /** 149 | * REQUEST = 0; 150 | */ 151 | REQUEST(0), 152 | /** 153 | * RESPOND = 1; 154 | */ 155 | RESPOND(1), 156 | /** 157 | * PEER = 2; 158 | */ 159 | PEER(2), 160 | UNRECOGNIZED(-1), 161 | ; 162 | 163 | /** 164 | * REQUEST = 0; 165 | */ 166 | public static final int REQUEST_VALUE = 0; 167 | /** 168 | * RESPOND = 1; 169 | */ 170 | public static final int RESPOND_VALUE = 1; 171 | /** 172 | * PEER = 2; 173 | */ 174 | public static final int PEER_VALUE = 2; 175 | 176 | 177 | public final int getNumber() { 178 | if (this == UNRECOGNIZED) { 179 | throw new java.lang.IllegalArgumentException( 180 | "Can't get the number of an unknown enum value."); 181 | } 182 | return value; 183 | } 184 | 185 | /** 186 | * @param value The numeric wire value of the corresponding enum entry. 187 | * @return The enum associated with the given numeric wire value. 188 | * @deprecated Use {@link #forNumber(int)} instead. 189 | */ 190 | @java.lang.Deprecated 191 | public static Type valueOf(int value) { 192 | return forNumber(value); 193 | } 194 | 195 | /** 196 | * @param value The numeric wire value of the corresponding enum entry. 197 | * @return The enum associated with the given numeric wire value. 198 | */ 199 | public static Type forNumber(int value) { 200 | switch (value) { 201 | case 0: return REQUEST; 202 | case 1: return RESPOND; 203 | case 2: return PEER; 204 | default: return null; 205 | } 206 | } 207 | 208 | public static com.google.protobuf.Internal.EnumLiteMap 209 | internalGetValueMap() { 210 | return internalValueMap; 211 | } 212 | private static final com.google.protobuf.Internal.EnumLiteMap< 213 | Type> internalValueMap = 214 | new com.google.protobuf.Internal.EnumLiteMap() { 215 | public Type findValueByNumber(int number) { 216 | return Type.forNumber(number); 217 | } 218 | }; 219 | 220 | public final com.google.protobuf.Descriptors.EnumValueDescriptor 221 | getValueDescriptor() { 222 | if (this == UNRECOGNIZED) { 223 | throw new java.lang.IllegalStateException( 224 | "Can't get the descriptor of an unrecognized enum value."); 225 | } 226 | return getDescriptor().getValues().get(ordinal()); 227 | } 228 | public final com.google.protobuf.Descriptors.EnumDescriptor 229 | getDescriptorForType() { 230 | return getDescriptor(); 231 | } 232 | public static final com.google.protobuf.Descriptors.EnumDescriptor 233 | getDescriptor() { 234 | return org.qrpc.Meta.getDescriptor().getEnumTypes().get(1); 235 | } 236 | 237 | private static final Type[] VALUES = values(); 238 | 239 | public static Type valueOf( 240 | com.google.protobuf.Descriptors.EnumValueDescriptor desc) { 241 | if (desc.getType() != getDescriptor()) { 242 | throw new java.lang.IllegalArgumentException( 243 | "EnumValueDescriptor is not for this type."); 244 | } 245 | if (desc.getIndex() == -1) { 246 | return UNRECOGNIZED; 247 | } 248 | return VALUES[desc.getIndex()]; 249 | } 250 | 251 | private final int value; 252 | 253 | private Type(int value) { 254 | this.value = value; 255 | } 256 | 257 | // @@protoc_insertion_point(enum_scope:qrpc.meta.Type) 258 | } 259 | 260 | public interface RpcMetaDataOrBuilder extends 261 | // @@protoc_insertion_point(interface_extends:qrpc.meta.RpcMetaData) 262 | com.google.protobuf.MessageOrBuilder { 263 | 264 | /** 265 | * string service_name = 1; 266 | * @return The serviceName. 267 | */ 268 | java.lang.String getServiceName(); 269 | /** 270 | * string service_name = 1; 271 | * @return The bytes for serviceName. 272 | */ 273 | com.google.protobuf.ByteString 274 | getServiceNameBytes(); 275 | 276 | /** 277 | * int32 method_id = 2; 278 | * @return The methodId. 279 | */ 280 | int getMethodId(); 281 | 282 | /** 283 | * bytes content = 3; 284 | * @return The content. 285 | */ 286 | com.google.protobuf.ByteString getContent(); 287 | 288 | /** 289 | * .qrpc.meta.Status status = 4; 290 | * @return The enum numeric value on the wire for status. 291 | */ 292 | int getStatusValue(); 293 | /** 294 | * .qrpc.meta.Status status = 4; 295 | * @return The status. 296 | */ 297 | org.qrpc.Meta.Status getStatus(); 298 | 299 | /** 300 | * .qrpc.meta.Type type = 5; 301 | * @return The enum numeric value on the wire for type. 302 | */ 303 | int getTypeValue(); 304 | /** 305 | * .qrpc.meta.Type type = 5; 306 | * @return The type. 307 | */ 308 | org.qrpc.Meta.Type getType(); 309 | 310 | /** 311 | * string request_id = 6; 312 | * @return The requestId. 313 | */ 314 | java.lang.String getRequestId(); 315 | /** 316 | * string request_id = 6; 317 | * @return The bytes for requestId. 318 | */ 319 | com.google.protobuf.ByteString 320 | getRequestIdBytes(); 321 | } 322 | /** 323 | * Protobuf type {@code qrpc.meta.RpcMetaData} 324 | */ 325 | public static final class RpcMetaData extends 326 | com.google.protobuf.GeneratedMessageV3 implements 327 | // @@protoc_insertion_point(message_implements:qrpc.meta.RpcMetaData) 328 | RpcMetaDataOrBuilder { 329 | private static final long serialVersionUID = 0L; 330 | // Use RpcMetaData.newBuilder() to construct. 331 | private RpcMetaData(com.google.protobuf.GeneratedMessageV3.Builder builder) { 332 | super(builder); 333 | } 334 | private RpcMetaData() { 335 | serviceName_ = ""; 336 | content_ = com.google.protobuf.ByteString.EMPTY; 337 | status_ = 0; 338 | type_ = 0; 339 | requestId_ = ""; 340 | } 341 | 342 | @java.lang.Override 343 | @SuppressWarnings({"unused"}) 344 | protected java.lang.Object newInstance( 345 | UnusedPrivateParameter unused) { 346 | return new RpcMetaData(); 347 | } 348 | 349 | public static final com.google.protobuf.Descriptors.Descriptor 350 | getDescriptor() { 351 | return org.qrpc.Meta.internal_static_qrpc_meta_RpcMetaData_descriptor; 352 | } 353 | 354 | @java.lang.Override 355 | protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 356 | internalGetFieldAccessorTable() { 357 | return org.qrpc.Meta.internal_static_qrpc_meta_RpcMetaData_fieldAccessorTable 358 | .ensureFieldAccessorsInitialized( 359 | org.qrpc.Meta.RpcMetaData.class, org.qrpc.Meta.RpcMetaData.Builder.class); 360 | } 361 | 362 | public static final int SERVICE_NAME_FIELD_NUMBER = 1; 363 | @SuppressWarnings("serial") 364 | private volatile java.lang.Object serviceName_ = ""; 365 | /** 366 | * string service_name = 1; 367 | * @return The serviceName. 368 | */ 369 | @java.lang.Override 370 | public java.lang.String getServiceName() { 371 | java.lang.Object ref = serviceName_; 372 | if (ref instanceof java.lang.String) { 373 | return (java.lang.String) ref; 374 | } else { 375 | com.google.protobuf.ByteString bs = 376 | (com.google.protobuf.ByteString) ref; 377 | java.lang.String s = bs.toStringUtf8(); 378 | serviceName_ = s; 379 | return s; 380 | } 381 | } 382 | /** 383 | * string service_name = 1; 384 | * @return The bytes for serviceName. 385 | */ 386 | @java.lang.Override 387 | public com.google.protobuf.ByteString 388 | getServiceNameBytes() { 389 | java.lang.Object ref = serviceName_; 390 | if (ref instanceof java.lang.String) { 391 | com.google.protobuf.ByteString b = 392 | com.google.protobuf.ByteString.copyFromUtf8( 393 | (java.lang.String) ref); 394 | serviceName_ = b; 395 | return b; 396 | } else { 397 | return (com.google.protobuf.ByteString) ref; 398 | } 399 | } 400 | 401 | public static final int METHOD_ID_FIELD_NUMBER = 2; 402 | private int methodId_ = 0; 403 | /** 404 | * int32 method_id = 2; 405 | * @return The methodId. 406 | */ 407 | @java.lang.Override 408 | public int getMethodId() { 409 | return methodId_; 410 | } 411 | 412 | public static final int CONTENT_FIELD_NUMBER = 3; 413 | private com.google.protobuf.ByteString content_ = com.google.protobuf.ByteString.EMPTY; 414 | /** 415 | * bytes content = 3; 416 | * @return The content. 417 | */ 418 | @java.lang.Override 419 | public com.google.protobuf.ByteString getContent() { 420 | return content_; 421 | } 422 | 423 | public static final int STATUS_FIELD_NUMBER = 4; 424 | private int status_ = 0; 425 | /** 426 | * .qrpc.meta.Status status = 4; 427 | * @return The enum numeric value on the wire for status. 428 | */ 429 | @java.lang.Override public int getStatusValue() { 430 | return status_; 431 | } 432 | /** 433 | * .qrpc.meta.Status status = 4; 434 | * @return The status. 435 | */ 436 | @java.lang.Override public org.qrpc.Meta.Status getStatus() { 437 | org.qrpc.Meta.Status result = org.qrpc.Meta.Status.forNumber(status_); 438 | return result == null ? org.qrpc.Meta.Status.UNRECOGNIZED : result; 439 | } 440 | 441 | public static final int TYPE_FIELD_NUMBER = 5; 442 | private int type_ = 0; 443 | /** 444 | * .qrpc.meta.Type type = 5; 445 | * @return The enum numeric value on the wire for type. 446 | */ 447 | @java.lang.Override public int getTypeValue() { 448 | return type_; 449 | } 450 | /** 451 | * .qrpc.meta.Type type = 5; 452 | * @return The type. 453 | */ 454 | @java.lang.Override public org.qrpc.Meta.Type getType() { 455 | org.qrpc.Meta.Type result = org.qrpc.Meta.Type.forNumber(type_); 456 | return result == null ? org.qrpc.Meta.Type.UNRECOGNIZED : result; 457 | } 458 | 459 | public static final int REQUEST_ID_FIELD_NUMBER = 6; 460 | @SuppressWarnings("serial") 461 | private volatile java.lang.Object requestId_ = ""; 462 | /** 463 | * string request_id = 6; 464 | * @return The requestId. 465 | */ 466 | @java.lang.Override 467 | public java.lang.String getRequestId() { 468 | java.lang.Object ref = requestId_; 469 | if (ref instanceof java.lang.String) { 470 | return (java.lang.String) ref; 471 | } else { 472 | com.google.protobuf.ByteString bs = 473 | (com.google.protobuf.ByteString) ref; 474 | java.lang.String s = bs.toStringUtf8(); 475 | requestId_ = s; 476 | return s; 477 | } 478 | } 479 | /** 480 | * string request_id = 6; 481 | * @return The bytes for requestId. 482 | */ 483 | @java.lang.Override 484 | public com.google.protobuf.ByteString 485 | getRequestIdBytes() { 486 | java.lang.Object ref = requestId_; 487 | if (ref instanceof java.lang.String) { 488 | com.google.protobuf.ByteString b = 489 | com.google.protobuf.ByteString.copyFromUtf8( 490 | (java.lang.String) ref); 491 | requestId_ = b; 492 | return b; 493 | } else { 494 | return (com.google.protobuf.ByteString) ref; 495 | } 496 | } 497 | 498 | private byte memoizedIsInitialized = -1; 499 | @java.lang.Override 500 | public final boolean isInitialized() { 501 | byte isInitialized = memoizedIsInitialized; 502 | if (isInitialized == 1) return true; 503 | if (isInitialized == 0) return false; 504 | 505 | memoizedIsInitialized = 1; 506 | return true; 507 | } 508 | 509 | @java.lang.Override 510 | public void writeTo(com.google.protobuf.CodedOutputStream output) 511 | throws java.io.IOException { 512 | if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(serviceName_)) { 513 | com.google.protobuf.GeneratedMessageV3.writeString(output, 1, serviceName_); 514 | } 515 | if (methodId_ != 0) { 516 | output.writeInt32(2, methodId_); 517 | } 518 | if (!content_.isEmpty()) { 519 | output.writeBytes(3, content_); 520 | } 521 | if (status_ != org.qrpc.Meta.Status.SUCCESS.getNumber()) { 522 | output.writeEnum(4, status_); 523 | } 524 | if (type_ != org.qrpc.Meta.Type.REQUEST.getNumber()) { 525 | output.writeEnum(5, type_); 526 | } 527 | if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(requestId_)) { 528 | com.google.protobuf.GeneratedMessageV3.writeString(output, 6, requestId_); 529 | } 530 | getUnknownFields().writeTo(output); 531 | } 532 | 533 | @java.lang.Override 534 | public int getSerializedSize() { 535 | int size = memoizedSize; 536 | if (size != -1) return size; 537 | 538 | size = 0; 539 | if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(serviceName_)) { 540 | size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, serviceName_); 541 | } 542 | if (methodId_ != 0) { 543 | size += com.google.protobuf.CodedOutputStream 544 | .computeInt32Size(2, methodId_); 545 | } 546 | if (!content_.isEmpty()) { 547 | size += com.google.protobuf.CodedOutputStream 548 | .computeBytesSize(3, content_); 549 | } 550 | if (status_ != org.qrpc.Meta.Status.SUCCESS.getNumber()) { 551 | size += com.google.protobuf.CodedOutputStream 552 | .computeEnumSize(4, status_); 553 | } 554 | if (type_ != org.qrpc.Meta.Type.REQUEST.getNumber()) { 555 | size += com.google.protobuf.CodedOutputStream 556 | .computeEnumSize(5, type_); 557 | } 558 | if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(requestId_)) { 559 | size += com.google.protobuf.GeneratedMessageV3.computeStringSize(6, requestId_); 560 | } 561 | size += getUnknownFields().getSerializedSize(); 562 | memoizedSize = size; 563 | return size; 564 | } 565 | 566 | @java.lang.Override 567 | public boolean equals(final java.lang.Object obj) { 568 | if (obj == this) { 569 | return true; 570 | } 571 | if (!(obj instanceof org.qrpc.Meta.RpcMetaData)) { 572 | return super.equals(obj); 573 | } 574 | org.qrpc.Meta.RpcMetaData other = (org.qrpc.Meta.RpcMetaData) obj; 575 | 576 | if (!getServiceName() 577 | .equals(other.getServiceName())) return false; 578 | if (getMethodId() 579 | != other.getMethodId()) return false; 580 | if (!getContent() 581 | .equals(other.getContent())) return false; 582 | if (status_ != other.status_) return false; 583 | if (type_ != other.type_) return false; 584 | if (!getRequestId() 585 | .equals(other.getRequestId())) return false; 586 | if (!getUnknownFields().equals(other.getUnknownFields())) return false; 587 | return true; 588 | } 589 | 590 | @java.lang.Override 591 | public int hashCode() { 592 | if (memoizedHashCode != 0) { 593 | return memoizedHashCode; 594 | } 595 | int hash = 41; 596 | hash = (19 * hash) + getDescriptor().hashCode(); 597 | hash = (37 * hash) + SERVICE_NAME_FIELD_NUMBER; 598 | hash = (53 * hash) + getServiceName().hashCode(); 599 | hash = (37 * hash) + METHOD_ID_FIELD_NUMBER; 600 | hash = (53 * hash) + getMethodId(); 601 | hash = (37 * hash) + CONTENT_FIELD_NUMBER; 602 | hash = (53 * hash) + getContent().hashCode(); 603 | hash = (37 * hash) + STATUS_FIELD_NUMBER; 604 | hash = (53 * hash) + status_; 605 | hash = (37 * hash) + TYPE_FIELD_NUMBER; 606 | hash = (53 * hash) + type_; 607 | hash = (37 * hash) + REQUEST_ID_FIELD_NUMBER; 608 | hash = (53 * hash) + getRequestId().hashCode(); 609 | hash = (29 * hash) + getUnknownFields().hashCode(); 610 | memoizedHashCode = hash; 611 | return hash; 612 | } 613 | 614 | public static org.qrpc.Meta.RpcMetaData parseFrom( 615 | java.nio.ByteBuffer data) 616 | throws com.google.protobuf.InvalidProtocolBufferException { 617 | return PARSER.parseFrom(data); 618 | } 619 | public static org.qrpc.Meta.RpcMetaData parseFrom( 620 | java.nio.ByteBuffer data, 621 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 622 | throws com.google.protobuf.InvalidProtocolBufferException { 623 | return PARSER.parseFrom(data, extensionRegistry); 624 | } 625 | public static org.qrpc.Meta.RpcMetaData parseFrom( 626 | com.google.protobuf.ByteString data) 627 | throws com.google.protobuf.InvalidProtocolBufferException { 628 | return PARSER.parseFrom(data); 629 | } 630 | public static org.qrpc.Meta.RpcMetaData parseFrom( 631 | com.google.protobuf.ByteString data, 632 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 633 | throws com.google.protobuf.InvalidProtocolBufferException { 634 | return PARSER.parseFrom(data, extensionRegistry); 635 | } 636 | public static org.qrpc.Meta.RpcMetaData parseFrom(byte[] data) 637 | throws com.google.protobuf.InvalidProtocolBufferException { 638 | return PARSER.parseFrom(data); 639 | } 640 | public static org.qrpc.Meta.RpcMetaData parseFrom( 641 | byte[] data, 642 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 643 | throws com.google.protobuf.InvalidProtocolBufferException { 644 | return PARSER.parseFrom(data, extensionRegistry); 645 | } 646 | public static org.qrpc.Meta.RpcMetaData parseFrom(java.io.InputStream input) 647 | throws java.io.IOException { 648 | return com.google.protobuf.GeneratedMessageV3 649 | .parseWithIOException(PARSER, input); 650 | } 651 | public static org.qrpc.Meta.RpcMetaData parseFrom( 652 | java.io.InputStream input, 653 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 654 | throws java.io.IOException { 655 | return com.google.protobuf.GeneratedMessageV3 656 | .parseWithIOException(PARSER, input, extensionRegistry); 657 | } 658 | 659 | public static org.qrpc.Meta.RpcMetaData parseDelimitedFrom(java.io.InputStream input) 660 | throws java.io.IOException { 661 | return com.google.protobuf.GeneratedMessageV3 662 | .parseDelimitedWithIOException(PARSER, input); 663 | } 664 | 665 | public static org.qrpc.Meta.RpcMetaData parseDelimitedFrom( 666 | java.io.InputStream input, 667 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 668 | throws java.io.IOException { 669 | return com.google.protobuf.GeneratedMessageV3 670 | .parseDelimitedWithIOException(PARSER, input, extensionRegistry); 671 | } 672 | public static org.qrpc.Meta.RpcMetaData parseFrom( 673 | com.google.protobuf.CodedInputStream input) 674 | throws java.io.IOException { 675 | return com.google.protobuf.GeneratedMessageV3 676 | .parseWithIOException(PARSER, input); 677 | } 678 | public static org.qrpc.Meta.RpcMetaData parseFrom( 679 | com.google.protobuf.CodedInputStream input, 680 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 681 | throws java.io.IOException { 682 | return com.google.protobuf.GeneratedMessageV3 683 | .parseWithIOException(PARSER, input, extensionRegistry); 684 | } 685 | 686 | @java.lang.Override 687 | public Builder newBuilderForType() { return newBuilder(); } 688 | public static Builder newBuilder() { 689 | return DEFAULT_INSTANCE.toBuilder(); 690 | } 691 | public static Builder newBuilder(org.qrpc.Meta.RpcMetaData prototype) { 692 | return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); 693 | } 694 | @java.lang.Override 695 | public Builder toBuilder() { 696 | return this == DEFAULT_INSTANCE 697 | ? new Builder() : new Builder().mergeFrom(this); 698 | } 699 | 700 | @java.lang.Override 701 | protected Builder newBuilderForType( 702 | com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { 703 | Builder builder = new Builder(parent); 704 | return builder; 705 | } 706 | /** 707 | * Protobuf type {@code qrpc.meta.RpcMetaData} 708 | */ 709 | public static final class Builder extends 710 | com.google.protobuf.GeneratedMessageV3.Builder implements 711 | // @@protoc_insertion_point(builder_implements:qrpc.meta.RpcMetaData) 712 | org.qrpc.Meta.RpcMetaDataOrBuilder { 713 | public static final com.google.protobuf.Descriptors.Descriptor 714 | getDescriptor() { 715 | return org.qrpc.Meta.internal_static_qrpc_meta_RpcMetaData_descriptor; 716 | } 717 | 718 | @java.lang.Override 719 | protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 720 | internalGetFieldAccessorTable() { 721 | return org.qrpc.Meta.internal_static_qrpc_meta_RpcMetaData_fieldAccessorTable 722 | .ensureFieldAccessorsInitialized( 723 | org.qrpc.Meta.RpcMetaData.class, org.qrpc.Meta.RpcMetaData.Builder.class); 724 | } 725 | 726 | // Construct using org.qrpc.Meta.RpcMetaData.newBuilder() 727 | private Builder() { 728 | 729 | } 730 | 731 | private Builder( 732 | com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { 733 | super(parent); 734 | 735 | } 736 | @java.lang.Override 737 | public Builder clear() { 738 | super.clear(); 739 | bitField0_ = 0; 740 | serviceName_ = ""; 741 | methodId_ = 0; 742 | content_ = com.google.protobuf.ByteString.EMPTY; 743 | status_ = 0; 744 | type_ = 0; 745 | requestId_ = ""; 746 | return this; 747 | } 748 | 749 | @java.lang.Override 750 | public com.google.protobuf.Descriptors.Descriptor 751 | getDescriptorForType() { 752 | return org.qrpc.Meta.internal_static_qrpc_meta_RpcMetaData_descriptor; 753 | } 754 | 755 | @java.lang.Override 756 | public org.qrpc.Meta.RpcMetaData getDefaultInstanceForType() { 757 | return org.qrpc.Meta.RpcMetaData.getDefaultInstance(); 758 | } 759 | 760 | @java.lang.Override 761 | public org.qrpc.Meta.RpcMetaData build() { 762 | org.qrpc.Meta.RpcMetaData result = buildPartial(); 763 | if (!result.isInitialized()) { 764 | throw newUninitializedMessageException(result); 765 | } 766 | return result; 767 | } 768 | 769 | @java.lang.Override 770 | public org.qrpc.Meta.RpcMetaData buildPartial() { 771 | org.qrpc.Meta.RpcMetaData result = new org.qrpc.Meta.RpcMetaData(this); 772 | if (bitField0_ != 0) { buildPartial0(result); } 773 | onBuilt(); 774 | return result; 775 | } 776 | 777 | private void buildPartial0(org.qrpc.Meta.RpcMetaData result) { 778 | int from_bitField0_ = bitField0_; 779 | if (((from_bitField0_ & 0x00000001) != 0)) { 780 | result.serviceName_ = serviceName_; 781 | } 782 | if (((from_bitField0_ & 0x00000002) != 0)) { 783 | result.methodId_ = methodId_; 784 | } 785 | if (((from_bitField0_ & 0x00000004) != 0)) { 786 | result.content_ = content_; 787 | } 788 | if (((from_bitField0_ & 0x00000008) != 0)) { 789 | result.status_ = status_; 790 | } 791 | if (((from_bitField0_ & 0x00000010) != 0)) { 792 | result.type_ = type_; 793 | } 794 | if (((from_bitField0_ & 0x00000020) != 0)) { 795 | result.requestId_ = requestId_; 796 | } 797 | } 798 | 799 | @java.lang.Override 800 | public Builder mergeFrom(com.google.protobuf.Message other) { 801 | if (other instanceof org.qrpc.Meta.RpcMetaData) { 802 | return mergeFrom((org.qrpc.Meta.RpcMetaData)other); 803 | } else { 804 | super.mergeFrom(other); 805 | return this; 806 | } 807 | } 808 | 809 | public Builder mergeFrom(org.qrpc.Meta.RpcMetaData other) { 810 | if (other == org.qrpc.Meta.RpcMetaData.getDefaultInstance()) return this; 811 | if (!other.getServiceName().isEmpty()) { 812 | serviceName_ = other.serviceName_; 813 | bitField0_ |= 0x00000001; 814 | onChanged(); 815 | } 816 | if (other.getMethodId() != 0) { 817 | setMethodId(other.getMethodId()); 818 | } 819 | if (other.getContent() != com.google.protobuf.ByteString.EMPTY) { 820 | setContent(other.getContent()); 821 | } 822 | if (other.status_ != 0) { 823 | setStatusValue(other.getStatusValue()); 824 | } 825 | if (other.type_ != 0) { 826 | setTypeValue(other.getTypeValue()); 827 | } 828 | if (!other.getRequestId().isEmpty()) { 829 | requestId_ = other.requestId_; 830 | bitField0_ |= 0x00000020; 831 | onChanged(); 832 | } 833 | this.mergeUnknownFields(other.getUnknownFields()); 834 | onChanged(); 835 | return this; 836 | } 837 | 838 | @java.lang.Override 839 | public final boolean isInitialized() { 840 | return true; 841 | } 842 | 843 | @java.lang.Override 844 | public Builder mergeFrom( 845 | com.google.protobuf.CodedInputStream input, 846 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 847 | throws java.io.IOException { 848 | if (extensionRegistry == null) { 849 | throw new java.lang.NullPointerException(); 850 | } 851 | try { 852 | boolean done = false; 853 | while (!done) { 854 | int tag = input.readTag(); 855 | switch (tag) { 856 | case 0: 857 | done = true; 858 | break; 859 | case 10: { 860 | serviceName_ = input.readStringRequireUtf8(); 861 | bitField0_ |= 0x00000001; 862 | break; 863 | } // case 10 864 | case 16: { 865 | methodId_ = input.readInt32(); 866 | bitField0_ |= 0x00000002; 867 | break; 868 | } // case 16 869 | case 26: { 870 | content_ = input.readBytes(); 871 | bitField0_ |= 0x00000004; 872 | break; 873 | } // case 26 874 | case 32: { 875 | status_ = input.readEnum(); 876 | bitField0_ |= 0x00000008; 877 | break; 878 | } // case 32 879 | case 40: { 880 | type_ = input.readEnum(); 881 | bitField0_ |= 0x00000010; 882 | break; 883 | } // case 40 884 | case 50: { 885 | requestId_ = input.readStringRequireUtf8(); 886 | bitField0_ |= 0x00000020; 887 | break; 888 | } // case 50 889 | default: { 890 | if (!super.parseUnknownField(input, extensionRegistry, tag)) { 891 | done = true; // was an endgroup tag 892 | } 893 | break; 894 | } // default: 895 | } // switch (tag) 896 | } // while (!done) 897 | } catch (com.google.protobuf.InvalidProtocolBufferException e) { 898 | throw e.unwrapIOException(); 899 | } finally { 900 | onChanged(); 901 | } // finally 902 | return this; 903 | } 904 | private int bitField0_; 905 | 906 | private java.lang.Object serviceName_ = ""; 907 | /** 908 | * string service_name = 1; 909 | * @return The serviceName. 910 | */ 911 | public java.lang.String getServiceName() { 912 | java.lang.Object ref = serviceName_; 913 | if (!(ref instanceof java.lang.String)) { 914 | com.google.protobuf.ByteString bs = 915 | (com.google.protobuf.ByteString) ref; 916 | java.lang.String s = bs.toStringUtf8(); 917 | serviceName_ = s; 918 | return s; 919 | } else { 920 | return (java.lang.String) ref; 921 | } 922 | } 923 | /** 924 | * string service_name = 1; 925 | * @return The bytes for serviceName. 926 | */ 927 | public com.google.protobuf.ByteString 928 | getServiceNameBytes() { 929 | java.lang.Object ref = serviceName_; 930 | if (ref instanceof String) { 931 | com.google.protobuf.ByteString b = 932 | com.google.protobuf.ByteString.copyFromUtf8( 933 | (java.lang.String) ref); 934 | serviceName_ = b; 935 | return b; 936 | } else { 937 | return (com.google.protobuf.ByteString) ref; 938 | } 939 | } 940 | /** 941 | * string service_name = 1; 942 | * @param value The serviceName to set. 943 | * @return This builder for chaining. 944 | */ 945 | public Builder setServiceName( 946 | java.lang.String value) { 947 | if (value == null) { throw new NullPointerException(); } 948 | serviceName_ = value; 949 | bitField0_ |= 0x00000001; 950 | onChanged(); 951 | return this; 952 | } 953 | /** 954 | * string service_name = 1; 955 | * @return This builder for chaining. 956 | */ 957 | public Builder clearServiceName() { 958 | serviceName_ = getDefaultInstance().getServiceName(); 959 | bitField0_ = (bitField0_ & ~0x00000001); 960 | onChanged(); 961 | return this; 962 | } 963 | /** 964 | * string service_name = 1; 965 | * @param value The bytes for serviceName to set. 966 | * @return This builder for chaining. 967 | */ 968 | public Builder setServiceNameBytes( 969 | com.google.protobuf.ByteString value) { 970 | if (value == null) { throw new NullPointerException(); } 971 | checkByteStringIsUtf8(value); 972 | serviceName_ = value; 973 | bitField0_ |= 0x00000001; 974 | onChanged(); 975 | return this; 976 | } 977 | 978 | private int methodId_ ; 979 | /** 980 | * int32 method_id = 2; 981 | * @return The methodId. 982 | */ 983 | @java.lang.Override 984 | public int getMethodId() { 985 | return methodId_; 986 | } 987 | /** 988 | * int32 method_id = 2; 989 | * @param value The methodId to set. 990 | * @return This builder for chaining. 991 | */ 992 | public Builder setMethodId(int value) { 993 | 994 | methodId_ = value; 995 | bitField0_ |= 0x00000002; 996 | onChanged(); 997 | return this; 998 | } 999 | /** 1000 | * int32 method_id = 2; 1001 | * @return This builder for chaining. 1002 | */ 1003 | public Builder clearMethodId() { 1004 | bitField0_ = (bitField0_ & ~0x00000002); 1005 | methodId_ = 0; 1006 | onChanged(); 1007 | return this; 1008 | } 1009 | 1010 | private com.google.protobuf.ByteString content_ = com.google.protobuf.ByteString.EMPTY; 1011 | /** 1012 | * bytes content = 3; 1013 | * @return The content. 1014 | */ 1015 | @java.lang.Override 1016 | public com.google.protobuf.ByteString getContent() { 1017 | return content_; 1018 | } 1019 | /** 1020 | * bytes content = 3; 1021 | * @param value The content to set. 1022 | * @return This builder for chaining. 1023 | */ 1024 | public Builder setContent(com.google.protobuf.ByteString value) { 1025 | if (value == null) { throw new NullPointerException(); } 1026 | content_ = value; 1027 | bitField0_ |= 0x00000004; 1028 | onChanged(); 1029 | return this; 1030 | } 1031 | /** 1032 | * bytes content = 3; 1033 | * @return This builder for chaining. 1034 | */ 1035 | public Builder clearContent() { 1036 | bitField0_ = (bitField0_ & ~0x00000004); 1037 | content_ = getDefaultInstance().getContent(); 1038 | onChanged(); 1039 | return this; 1040 | } 1041 | 1042 | private int status_ = 0; 1043 | /** 1044 | * .qrpc.meta.Status status = 4; 1045 | * @return The enum numeric value on the wire for status. 1046 | */ 1047 | @java.lang.Override public int getStatusValue() { 1048 | return status_; 1049 | } 1050 | /** 1051 | * .qrpc.meta.Status status = 4; 1052 | * @param value The enum numeric value on the wire for status to set. 1053 | * @return This builder for chaining. 1054 | */ 1055 | public Builder setStatusValue(int value) { 1056 | status_ = value; 1057 | bitField0_ |= 0x00000008; 1058 | onChanged(); 1059 | return this; 1060 | } 1061 | /** 1062 | * .qrpc.meta.Status status = 4; 1063 | * @return The status. 1064 | */ 1065 | @java.lang.Override 1066 | public org.qrpc.Meta.Status getStatus() { 1067 | org.qrpc.Meta.Status result = org.qrpc.Meta.Status.forNumber(status_); 1068 | return result == null ? org.qrpc.Meta.Status.UNRECOGNIZED : result; 1069 | } 1070 | /** 1071 | * .qrpc.meta.Status status = 4; 1072 | * @param value The status to set. 1073 | * @return This builder for chaining. 1074 | */ 1075 | public Builder setStatus(org.qrpc.Meta.Status value) { 1076 | if (value == null) { 1077 | throw new NullPointerException(); 1078 | } 1079 | bitField0_ |= 0x00000008; 1080 | status_ = value.getNumber(); 1081 | onChanged(); 1082 | return this; 1083 | } 1084 | /** 1085 | * .qrpc.meta.Status status = 4; 1086 | * @return This builder for chaining. 1087 | */ 1088 | public Builder clearStatus() { 1089 | bitField0_ = (bitField0_ & ~0x00000008); 1090 | status_ = 0; 1091 | onChanged(); 1092 | return this; 1093 | } 1094 | 1095 | private int type_ = 0; 1096 | /** 1097 | * .qrpc.meta.Type type = 5; 1098 | * @return The enum numeric value on the wire for type. 1099 | */ 1100 | @java.lang.Override public int getTypeValue() { 1101 | return type_; 1102 | } 1103 | /** 1104 | * .qrpc.meta.Type type = 5; 1105 | * @param value The enum numeric value on the wire for type to set. 1106 | * @return This builder for chaining. 1107 | */ 1108 | public Builder setTypeValue(int value) { 1109 | type_ = value; 1110 | bitField0_ |= 0x00000010; 1111 | onChanged(); 1112 | return this; 1113 | } 1114 | /** 1115 | * .qrpc.meta.Type type = 5; 1116 | * @return The type. 1117 | */ 1118 | @java.lang.Override 1119 | public org.qrpc.Meta.Type getType() { 1120 | org.qrpc.Meta.Type result = org.qrpc.Meta.Type.forNumber(type_); 1121 | return result == null ? org.qrpc.Meta.Type.UNRECOGNIZED : result; 1122 | } 1123 | /** 1124 | * .qrpc.meta.Type type = 5; 1125 | * @param value The type to set. 1126 | * @return This builder for chaining. 1127 | */ 1128 | public Builder setType(org.qrpc.Meta.Type value) { 1129 | if (value == null) { 1130 | throw new NullPointerException(); 1131 | } 1132 | bitField0_ |= 0x00000010; 1133 | type_ = value.getNumber(); 1134 | onChanged(); 1135 | return this; 1136 | } 1137 | /** 1138 | * .qrpc.meta.Type type = 5; 1139 | * @return This builder for chaining. 1140 | */ 1141 | public Builder clearType() { 1142 | bitField0_ = (bitField0_ & ~0x00000010); 1143 | type_ = 0; 1144 | onChanged(); 1145 | return this; 1146 | } 1147 | 1148 | private java.lang.Object requestId_ = ""; 1149 | /** 1150 | * string request_id = 6; 1151 | * @return The requestId. 1152 | */ 1153 | public java.lang.String getRequestId() { 1154 | java.lang.Object ref = requestId_; 1155 | if (!(ref instanceof java.lang.String)) { 1156 | com.google.protobuf.ByteString bs = 1157 | (com.google.protobuf.ByteString) ref; 1158 | java.lang.String s = bs.toStringUtf8(); 1159 | requestId_ = s; 1160 | return s; 1161 | } else { 1162 | return (java.lang.String) ref; 1163 | } 1164 | } 1165 | /** 1166 | * string request_id = 6; 1167 | * @return The bytes for requestId. 1168 | */ 1169 | public com.google.protobuf.ByteString 1170 | getRequestIdBytes() { 1171 | java.lang.Object ref = requestId_; 1172 | if (ref instanceof String) { 1173 | com.google.protobuf.ByteString b = 1174 | com.google.protobuf.ByteString.copyFromUtf8( 1175 | (java.lang.String) ref); 1176 | requestId_ = b; 1177 | return b; 1178 | } else { 1179 | return (com.google.protobuf.ByteString) ref; 1180 | } 1181 | } 1182 | /** 1183 | * string request_id = 6; 1184 | * @param value The requestId to set. 1185 | * @return This builder for chaining. 1186 | */ 1187 | public Builder setRequestId( 1188 | java.lang.String value) { 1189 | if (value == null) { throw new NullPointerException(); } 1190 | requestId_ = value; 1191 | bitField0_ |= 0x00000020; 1192 | onChanged(); 1193 | return this; 1194 | } 1195 | /** 1196 | * string request_id = 6; 1197 | * @return This builder for chaining. 1198 | */ 1199 | public Builder clearRequestId() { 1200 | requestId_ = getDefaultInstance().getRequestId(); 1201 | bitField0_ = (bitField0_ & ~0x00000020); 1202 | onChanged(); 1203 | return this; 1204 | } 1205 | /** 1206 | * string request_id = 6; 1207 | * @param value The bytes for requestId to set. 1208 | * @return This builder for chaining. 1209 | */ 1210 | public Builder setRequestIdBytes( 1211 | com.google.protobuf.ByteString value) { 1212 | if (value == null) { throw new NullPointerException(); } 1213 | checkByteStringIsUtf8(value); 1214 | requestId_ = value; 1215 | bitField0_ |= 0x00000020; 1216 | onChanged(); 1217 | return this; 1218 | } 1219 | @java.lang.Override 1220 | public final Builder setUnknownFields( 1221 | final com.google.protobuf.UnknownFieldSet unknownFields) { 1222 | return super.setUnknownFields(unknownFields); 1223 | } 1224 | 1225 | @java.lang.Override 1226 | public final Builder mergeUnknownFields( 1227 | final com.google.protobuf.UnknownFieldSet unknownFields) { 1228 | return super.mergeUnknownFields(unknownFields); 1229 | } 1230 | 1231 | 1232 | // @@protoc_insertion_point(builder_scope:qrpc.meta.RpcMetaData) 1233 | } 1234 | 1235 | // @@protoc_insertion_point(class_scope:qrpc.meta.RpcMetaData) 1236 | private static final org.qrpc.Meta.RpcMetaData DEFAULT_INSTANCE; 1237 | static { 1238 | DEFAULT_INSTANCE = new org.qrpc.Meta.RpcMetaData(); 1239 | } 1240 | 1241 | public static org.qrpc.Meta.RpcMetaData getDefaultInstance() { 1242 | return DEFAULT_INSTANCE; 1243 | } 1244 | 1245 | private static final com.google.protobuf.Parser 1246 | PARSER = new com.google.protobuf.AbstractParser() { 1247 | @java.lang.Override 1248 | public RpcMetaData parsePartialFrom( 1249 | com.google.protobuf.CodedInputStream input, 1250 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 1251 | throws com.google.protobuf.InvalidProtocolBufferException { 1252 | Builder builder = newBuilder(); 1253 | try { 1254 | builder.mergeFrom(input, extensionRegistry); 1255 | } catch (com.google.protobuf.InvalidProtocolBufferException e) { 1256 | throw e.setUnfinishedMessage(builder.buildPartial()); 1257 | } catch (com.google.protobuf.UninitializedMessageException e) { 1258 | throw e.asInvalidProtocolBufferException().setUnfinishedMessage(builder.buildPartial()); 1259 | } catch (java.io.IOException e) { 1260 | throw new com.google.protobuf.InvalidProtocolBufferException(e) 1261 | .setUnfinishedMessage(builder.buildPartial()); 1262 | } 1263 | return builder.buildPartial(); 1264 | } 1265 | }; 1266 | 1267 | public static com.google.protobuf.Parser parser() { 1268 | return PARSER; 1269 | } 1270 | 1271 | @java.lang.Override 1272 | public com.google.protobuf.Parser getParserForType() { 1273 | return PARSER; 1274 | } 1275 | 1276 | @java.lang.Override 1277 | public org.qrpc.Meta.RpcMetaData getDefaultInstanceForType() { 1278 | return DEFAULT_INSTANCE; 1279 | } 1280 | 1281 | } 1282 | 1283 | private static final com.google.protobuf.Descriptors.Descriptor 1284 | internal_static_qrpc_meta_RpcMetaData_descriptor; 1285 | private static final 1286 | com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 1287 | internal_static_qrpc_meta_RpcMetaData_fieldAccessorTable; 1288 | 1289 | public static com.google.protobuf.Descriptors.FileDescriptor 1290 | getDescriptor() { 1291 | return descriptor; 1292 | } 1293 | private static com.google.protobuf.Descriptors.FileDescriptor 1294 | descriptor; 1295 | static { 1296 | java.lang.String[] descriptorData = { 1297 | "\n\nmeta.proto\022\tqrpc.meta\"\235\001\n\013RpcMetaData\022" + 1298 | "\024\n\014service_name\030\001 \001(\t\022\021\n\tmethod_id\030\002 \001(\005" + 1299 | "\022\017\n\007content\030\003 \001(\014\022!\n\006status\030\004 \001(\0162\021.qrpc" + 1300 | ".meta.Status\022\035\n\004type\030\005 \001(\0162\017.qrpc.meta.T" + 1301 | "ype\022\022\n\nrequest_id\030\006 \001(\t*L\n\006Status\022\013\n\007SUC" + 1302 | "CESS\020\000\022\014\n\010TIME_OUT\020\002\022\025\n\021SERVICE_NOT_FOUN" + 1303 | "D\020\003\022\020\n\014SYSTEM_ERROR\020\004**\n\004Type\022\013\n\007REQUEST" + 1304 | "\020\000\022\013\n\007RESPOND\020\001\022\010\n\004PEER\020\002B\022\n\010org.qrpcB\004M" + 1305 | "etaP\000b\006proto3" 1306 | }; 1307 | descriptor = com.google.protobuf.Descriptors.FileDescriptor 1308 | .internalBuildGeneratedFileFrom(descriptorData, 1309 | new com.google.protobuf.Descriptors.FileDescriptor[] { 1310 | }); 1311 | internal_static_qrpc_meta_RpcMetaData_descriptor = 1312 | getDescriptor().getMessageTypes().get(0); 1313 | internal_static_qrpc_meta_RpcMetaData_fieldAccessorTable = new 1314 | com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( 1315 | internal_static_qrpc_meta_RpcMetaData_descriptor, 1316 | new java.lang.String[] { "ServiceName", "MethodId", "Content", "Status", "Type", "RequestId", }); 1317 | } 1318 | 1319 | // @@protoc_insertion_point(outer_class_scope) 1320 | } 1321 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/PropertyKeys.java: -------------------------------------------------------------------------------- 1 | package org.qrpc; 2 | 3 | /** 4 | * @author Yang Lianhuan 5 | * @version 1.0.0 6 | * @since 2023/4/4 7 | **/ 8 | public class PropertyKeys { 9 | public static final String EVENT_LOOP_BOSS_THREAD = "qrpc.event_loop_boos_thread"; 10 | public static final String EVENT_LOOP_WORK_THREAD = "qrpc.event_loop_work_thread"; 11 | public static final String SERVER_PORT = "qrpc.server.port"; 12 | public static final String CLIENT_TIMEOUT = "qrpc.client.timeout"; 13 | } 14 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/RpcStatus.java: -------------------------------------------------------------------------------- 1 | package org.qrpc; 2 | 3 | /** 4 | * @author Yang Lianhuan 5 | * @version 1.0.0 6 | * @since 2024/1/27 7 | **/ 8 | public enum RpcStatus { 9 | /** 10 | * 初始化 11 | */ 12 | INIT, 13 | 14 | /** 15 | * 成功 16 | */ 17 | SUCCESS, 18 | 19 | /** 20 | * 失败 21 | */ 22 | FAILED, 23 | 24 | /** 25 | * 取消中 26 | */ 27 | CANCELING, 28 | 29 | /** 30 | * 已取消 31 | */ 32 | CANCELED 33 | } 34 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/ThreadService.java: -------------------------------------------------------------------------------- 1 | package org.qrpc; 2 | 3 | import io.netty.channel.EventLoopGroup; 4 | import io.netty.channel.nio.NioEventLoopGroup; 5 | 6 | import java.util.concurrent.*; 7 | 8 | /** 9 | * @author Yang Lianhuan 10 | * @version 1.0.0 11 | * @since 2023/4/6 12 | **/ 13 | public class ThreadService { 14 | private volatile static EventLoopGroup serverWorkEventLoopGroup; 15 | private volatile static EventLoopGroup clientEventLoopGroup; 16 | private volatile static EventLoopGroup serverBossEventLoopGroup; 17 | private volatile static ExecutorService businessExecutor; 18 | private static final int processors; 19 | private static final int clientThreadNum; 20 | private static final int serverBossThreadNum; 21 | private static final int serverWorkThreadNum; 22 | private static final int coreThread; 23 | private static final int maxThread; 24 | private static final BlockingQueue workQueue; 25 | 26 | static { 27 | processors = Runtime.getRuntime().availableProcessors(); 28 | clientThreadNum = Math.min(Math.max(1, processors/4), 4); 29 | serverBossThreadNum = Math.max(processors/4, 1); 30 | serverWorkThreadNum = Math.max(6, processors * 3); 31 | coreThread = processors * 3; 32 | maxThread = processors * 10; 33 | workQueue = new LinkedBlockingQueue<>(); 34 | } 35 | 36 | /** 37 | * 获取Reactor线程组 38 | * 39 | * @return BossEventLoopGroup 40 | */ 41 | public static EventLoopGroup getServerBossEventLoopGroup() { 42 | if (serverBossEventLoopGroup != null && !serverBossEventLoopGroup.isShutdown() 43 | && !serverBossEventLoopGroup.isTerminated()) { 44 | return serverBossEventLoopGroup; 45 | } 46 | synchronized (ThreadService.class) { 47 | if (serverBossEventLoopGroup != null && !serverBossEventLoopGroup.isShutdown() 48 | && !serverBossEventLoopGroup.isTerminated()) { 49 | return serverBossEventLoopGroup; 50 | } 51 | serverBossEventLoopGroup = new NioEventLoopGroup(serverBossThreadNum); 52 | return serverBossEventLoopGroup; 53 | } 54 | } 55 | 56 | /** 57 | * 获取工作IO线程组 58 | * 59 | * @return ServerWorkEventLoopGroup 60 | */ 61 | public static EventLoopGroup getServerWorkEventLoopGroup() { 62 | if (serverWorkEventLoopGroup != null && !serverWorkEventLoopGroup.isTerminated() 63 | && !serverWorkEventLoopGroup.isShutdown()) { 64 | return serverWorkEventLoopGroup; 65 | } 66 | synchronized (ThreadService.class) { 67 | if (serverWorkEventLoopGroup != null && !serverWorkEventLoopGroup.isTerminated() 68 | && !serverWorkEventLoopGroup.isShutdown()) { 69 | return serverWorkEventLoopGroup; 70 | } 71 | serverWorkEventLoopGroup = new NioEventLoopGroup(serverWorkThreadNum); 72 | return serverWorkEventLoopGroup; 73 | } 74 | } 75 | 76 | /** 77 | * 获取客户端线程组 78 | * 79 | * @return ClientEventLoopGroup 80 | */ 81 | public static EventLoopGroup getClientEventLoopGroup() { 82 | if (clientEventLoopGroup != null && !clientEventLoopGroup.isShutdown() 83 | && !clientEventLoopGroup.isTerminated()) { 84 | return clientEventLoopGroup; 85 | } 86 | synchronized (ThreadService.class) { 87 | if (clientEventLoopGroup != null && !clientEventLoopGroup.isShutdown() 88 | && !clientEventLoopGroup.isTerminated()) { 89 | return clientEventLoopGroup; 90 | } 91 | clientEventLoopGroup = new NioEventLoopGroup(clientThreadNum); 92 | return clientEventLoopGroup; 93 | } 94 | } 95 | 96 | /** 97 | * 获取业务线程池 98 | * 99 | * @return BusinessExecutor 100 | */ 101 | public static ExecutorService getBusinessExecutor () { 102 | if (businessExecutor != null && !businessExecutor.isShutdown() && !businessExecutor.isTerminated()) { 103 | return businessExecutor; 104 | } 105 | synchronized (ThreadService.class) { 106 | if (businessExecutor != null && !businessExecutor.isShutdown() && !businessExecutor.isTerminated()) { 107 | return businessExecutor; 108 | } 109 | businessExecutor = new ThreadPoolExecutor(coreThread, maxThread, 30, TimeUnit.SECONDS, workQueue); 110 | return businessExecutor; 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/client/Channel/QRpcChannel.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.client.Channel; 2 | 3 | import com.google.protobuf.*; 4 | import org.qrpc.server.MethodInfoHolder; 5 | import org.qrpc.client.ConnectionConfig; 6 | import org.qrpc.client.EncryptType; 7 | 8 | /** 9 | * @author Yang Lianhuan 10 | * @version 1.0.0 11 | * @since 2023/7/6 12 | **/ 13 | public abstract class QRpcChannel { 14 | protected String ip; 15 | protected int port; 16 | protected EncryptType encryptType; 17 | 18 | public QRpcChannel(Builder builder) { 19 | this.ip = builder.ip; 20 | this.port = builder.port; 21 | this.encryptType = builder.encryptType; 22 | } 23 | 24 | protected ConnectionConfig getConnectionConfig() { 25 | return new ConnectionConfig(this.ip, this.port, this.encryptType); 26 | } 27 | 28 | protected MethodInfoHolder getMethodInfo(Descriptors.MethodDescriptor method, RpcController controller 29 | , Message request, Message responsePrototype, RpcCallback done) { 30 | return MethodInfoHolder.builder() 31 | .method(method) 32 | .controller(controller) 33 | .request(request) 34 | .responsePrototype(responsePrototype) 35 | .callback(done) 36 | .build(); 37 | } 38 | 39 | public static class Builder { 40 | protected String ip; 41 | protected int port; 42 | protected EncryptType encryptType; 43 | 44 | public Builder ip(String ip) { 45 | this.ip = ip; 46 | return this; 47 | } 48 | 49 | public Builder port(int port) { 50 | this.port = port; 51 | return this; 52 | } 53 | 54 | public Builder(String ip, int port) { 55 | this.ip = ip; 56 | this.port = port; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/client/Channel/SimpleBlockQRpcChannel.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.client.Channel; 2 | 3 | import com.google.protobuf.*; 4 | import org.qrpc.client.ConnectionFactory; 5 | import org.qrpc.client.EncryptType; 6 | import org.qrpc.client.QRpcClientEndpoint; 7 | 8 | /** 9 | * @author Yang Lianhuan 10 | * @version 1.0.0 11 | * @since 2023/7/10 12 | **/ 13 | public class SimpleBlockQRpcChannel extends QRpcChannel implements BlockingRpcChannel { 14 | private final QRpcClientEndpoint endpoint; 15 | 16 | private SimpleBlockQRpcChannel(Builder builder) { 17 | super(builder); 18 | this.endpoint = ConnectionFactory.getInstance().getEndpoint(getConnectionConfig()); 19 | } 20 | 21 | @Override 22 | public Message callBlockingMethod(Descriptors.MethodDescriptor method, RpcController controller, Message request, Message responsePrototype) { 23 | return endpoint.blockSend(getMethodInfo(method, controller, request, responsePrototype, null)); 24 | } 25 | 26 | public static BlockingQRpcChannelBuilder forAddress(String ip, int port) { 27 | return new BlockingQRpcChannelBuilder(ip, port); 28 | } 29 | 30 | public static class BlockingQRpcChannelBuilder extends Builder { 31 | public BlockingQRpcChannelBuilder(String ip, int port) { 32 | super(ip, port); 33 | } 34 | public BlockingQRpcChannelBuilder userPlaintext() { 35 | this.encryptType = EncryptType.PLAIN_TEXT; 36 | return this; 37 | } 38 | public SimpleBlockQRpcChannel build() { 39 | return new SimpleBlockQRpcChannel(this); 40 | } 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/client/Channel/SimpleQRpcChannel.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.client.Channel; 2 | 3 | import com.google.protobuf.*; 4 | import org.qrpc.server.MethodInfoHolder; 5 | import org.qrpc.client.ConnectionFactory; 6 | import org.qrpc.client.EncryptType; 7 | import org.qrpc.client.QRpcClientEndpoint; 8 | 9 | /** 10 | * @author Yang Lianhuan 11 | * @version 1.0.0 12 | * @since 2023/7/8 13 | **/ 14 | public class SimpleQRpcChannel extends QRpcChannel implements RpcChannel { 15 | private final QRpcClientEndpoint endpoint; 16 | 17 | @Override 18 | public void callMethod(Descriptors.MethodDescriptor method, RpcController controller, Message request, Message responsePrototype, RpcCallback done) { 19 | MethodInfoHolder methodInfo = getMethodInfo(method, controller, request, responsePrototype, done); 20 | endpoint.send(methodInfo); 21 | } 22 | 23 | private SimpleQRpcChannel(Builder builder) { 24 | super(builder); 25 | this.endpoint = ConnectionFactory.getInstance().getEndpoint(getConnectionConfig()); 26 | } 27 | 28 | public static SimpleQRpcChannelBuilder forAddress(String ip, int port) { 29 | return new SimpleQRpcChannelBuilder(ip, port); 30 | } 31 | 32 | public static class SimpleQRpcChannelBuilder extends Builder { 33 | 34 | public SimpleQRpcChannelBuilder(String ip, int port) { 35 | super(ip, port); 36 | } 37 | 38 | public SimpleQRpcChannelBuilder userPlaintext() { 39 | this.encryptType = EncryptType.PLAIN_TEXT; 40 | return this; 41 | } 42 | 43 | public SimpleQRpcChannel build() { 44 | return new SimpleQRpcChannel(this); 45 | } 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/client/ConnectionConfig.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.client; 2 | 3 | import lombok.Getter; 4 | 5 | /** 6 | * @author Yang Lianhuan 7 | * @version 1.0.0 8 | * @since 2023/7/10 9 | **/ 10 | @Getter 11 | public class ConnectionConfig { 12 | private final String ip; 13 | private final int port; 14 | private final EncryptType encryptType; 15 | 16 | public ConnectionConfig(String ip, int port, EncryptType encryptType) { 17 | this.ip = ip; 18 | this.port = port; 19 | this.encryptType = encryptType; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/client/ConnectionFactory.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.client; 2 | 3 | import org.qrpc.Factory; 4 | import org.qrpc.net.NettyClientEndpoint; 5 | 6 | import java.util.*; 7 | import java.util.concurrent.ConcurrentHashMap; 8 | 9 | /** 10 | * @author Yang Lianhuan 11 | * @version 1.0.0 12 | * @since 2023/7/10 13 | **/ 14 | public class ConnectionFactory implements Factory { 15 | private static final String COLON = ":"; 16 | private static ConnectionFactory connectionFactory; 17 | private static final Map connectionMap = new ConcurrentHashMap<>(); 18 | private final List endpointHandlers = new ArrayList<>(); 19 | 20 | public static ConnectionFactory getInstance() { 21 | if (connectionFactory != null) { 22 | return connectionFactory; 23 | } 24 | synchronized (ConnectionFactory.class) { 25 | if (connectionFactory != null) { 26 | return connectionFactory; 27 | } 28 | connectionFactory = new ConnectionFactory(); 29 | return connectionFactory; 30 | } 31 | } 32 | 33 | private ConnectionFactory() { 34 | this.endpointHandlers.add(new PlainTextEndpointHandler()); 35 | this.endpointHandlers.sort(Comparator.comparingInt(EndpointHandler::getOrder)); 36 | }; 37 | 38 | public QRpcClientEndpoint getEndpoint(ConnectionConfig connectionConfig) { 39 | String ip = connectionConfig.getIp(); 40 | int port = connectionConfig.getPort(); 41 | String key = ip + COLON + port; 42 | QRpcClientEndpoint endpoint = get(key); 43 | if (endpoint != null && endpoint.available()) { 44 | return endpoint; 45 | } 46 | remove(key); 47 | endpoint = getNewEndpoint(connectionConfig); 48 | register(key, endpoint); 49 | return endpoint; 50 | } 51 | 52 | private QRpcClientEndpoint getNewEndpoint(ConnectionConfig config) { 53 | for (EndpointHandler handler : this.endpointHandlers) { 54 | QRpcClientEndpoint endpoint = handler.process(config); 55 | if (endpoint != null) { 56 | return endpoint; 57 | } 58 | } 59 | return null; 60 | } 61 | 62 | @Override 63 | public void register(String key, QRpcClientEndpoint value) { 64 | connectionMap.put(key, value); 65 | } 66 | 67 | @Override 68 | public boolean contains(String key) { 69 | return connectionMap.containsKey(key); 70 | } 71 | 72 | @Override 73 | public QRpcClientEndpoint get(String key) { 74 | return connectionMap.get(key); 75 | } 76 | 77 | @Override 78 | public void remove(String key) { 79 | connectionMap.remove(key); 80 | } 81 | 82 | interface EndpointHandler { 83 | int getOrder(); 84 | 85 | QRpcClientEndpoint process(ConnectionConfig config); 86 | } 87 | 88 | static class PlainTextEndpointHandler implements EndpointHandler { 89 | 90 | private static final int ORDER = 0; 91 | 92 | @Override 93 | public int getOrder() { 94 | return ORDER; 95 | } 96 | 97 | @Override 98 | public QRpcClientEndpoint process(ConnectionConfig config) { 99 | if (config.getEncryptType() == EncryptType.PLAIN_TEXT 100 | || config.getEncryptType() == null) { 101 | return new NettyClientEndpoint(config.getIp(), config.getPort()); 102 | } 103 | return null; 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/client/EncryptType.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.client; 2 | 3 | /** 4 | * @author Yang Lianhuan 5 | * @version 1.0.0 6 | * @since 2023/7/8 7 | **/ 8 | public enum EncryptType { 9 | SSL, 10 | PLAIN_TEXT 11 | } 12 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/client/Endpoint.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.client; 2 | 3 | import com.google.protobuf.Descriptors; 4 | import com.google.protobuf.Message; 5 | import org.qrpc.ConfigEnvironment; 6 | import org.qrpc.Environment; 7 | import org.qrpc.Meta; 8 | import org.qrpc.PropertyKeys; 9 | import org.qrpc.net.Receiver; 10 | import org.qrpc.net.Sender; 11 | import org.qrpc.server.MethodInfoHolder; 12 | import lombok.Getter; 13 | import lombok.Setter; 14 | 15 | import java.util.UUID; 16 | import java.util.concurrent.CompletableFuture; 17 | import java.util.concurrent.ExecutionException; 18 | import java.util.concurrent.TimeUnit; 19 | import java.util.concurrent.TimeoutException; 20 | 21 | /** 22 | * 子类必须调用setSender方法设置消息发送器,接收消息则回调onMessage()方法 23 | * 24 | * @author Yang Lianhuan 25 | * @version 1.0.0 26 | * @since 2024/2/3 27 | **/ 28 | public abstract class Endpoint implements QRpcClientEndpoint, Receiver, Sender { 29 | protected final RequestFuture requestFuture; 30 | protected final RequestCache requestCache; 31 | 32 | @Getter 33 | protected final ResponseHandler responseHandler; 34 | protected final Environment environment = ConfigEnvironment.getInstance(); 35 | 36 | @Setter 37 | protected boolean ready = false; 38 | 39 | public Endpoint() { 40 | this.requestCache = new RequestCache(); 41 | this.requestFuture = new RequestFuture(); 42 | this.responseHandler = new ResponseHandler(requestCache ,requestFuture); 43 | } 44 | 45 | @Override 46 | public void send(MethodInfoHolder methodInfo) { 47 | Meta.RpcMetaData metaData = getMetaData(methodInfo); 48 | requestCache.register(metaData.getRequestId(), methodInfo); 49 | send(metaData); 50 | } 51 | 52 | @Override 53 | public Message blockSend(MethodInfoHolder methodInfo) { 54 | Meta.RpcMetaData metaData = getMetaData(methodInfo); 55 | CompletableFuture responseFuture = new CompletableFuture<>(); 56 | requestCache.register(metaData.getRequestId(), methodInfo); 57 | requestFuture.register(metaData.getRequestId(), responseFuture); 58 | send(metaData); 59 | try { 60 | return responseFuture.get(Long.parseLong(environment.getProperty(PropertyKeys.CLIENT_TIMEOUT).toString()), TimeUnit.MILLISECONDS); 61 | } catch (InterruptedException | ExecutionException | TimeoutException e) { 62 | e.printStackTrace(); 63 | } 64 | return null; 65 | } 66 | 67 | @Override 68 | public boolean available() { 69 | return ready; 70 | } 71 | 72 | @Override 73 | public void onMessage(Object o) { 74 | getResponseHandler().onMessage(o); 75 | } 76 | 77 | private Meta.RpcMetaData getMetaData(MethodInfoHolder methodInfo) { 78 | Descriptors.MethodDescriptor mDescriptor = methodInfo.getMethod(); 79 | return Meta.RpcMetaData.newBuilder() 80 | .setRequestId(UUID.randomUUID().toString()) 81 | .setServiceName(mDescriptor.getService().getFullName()) 82 | .setMethodId(mDescriptor.getIndex()) 83 | .setType(Meta.Type.REQUEST) 84 | .setContent(methodInfo.getRequest().toByteString()) 85 | .build(); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/client/QRpcClientEndpoint.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.client; 2 | 3 | import com.google.protobuf.Message; 4 | import org.qrpc.server.MethodInfoHolder; 5 | 6 | /** 7 | * send a message to ip:port, responseCallback invoked when respond 8 | * 9 | * @author Yang Lianhuan 10 | * @version 1.0.0 11 | * @since 2023/7/6 12 | **/ 13 | public interface QRpcClientEndpoint { 14 | void send(MethodInfoHolder methodInfo); 15 | 16 | Message blockSend(MethodInfoHolder methodInfo); 17 | 18 | boolean available(); 19 | } 20 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/client/RequestCache.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.client; 2 | 3 | import org.qrpc.Factory; 4 | import org.qrpc.server.MethodInfoHolder; 5 | 6 | import java.util.Map; 7 | import java.util.concurrent.ConcurrentHashMap; 8 | 9 | /** 10 | * @author Yang Lianhuan 11 | * @version 1.0.0 12 | * @since 2023/7/8 13 | **/ 14 | public class RequestCache implements Factory { 15 | 16 | private final Map requestMap = new ConcurrentHashMap<>(); 17 | 18 | @Override 19 | public void register(String name, MethodInfoHolder bean) { 20 | if (requestMap.containsKey(name)) { 21 | return; 22 | } 23 | requestMap.put(name, bean); 24 | } 25 | 26 | @Override 27 | public boolean contains(String name) { 28 | return requestMap.containsKey(name); 29 | } 30 | 31 | @Override 32 | public MethodInfoHolder get(String name) { 33 | return requestMap.get(name); 34 | } 35 | 36 | @Override 37 | public void remove(String key) { 38 | requestMap.remove(key); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/client/RequestFuture.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.client; 2 | 3 | import com.google.protobuf.Message; 4 | import org.qrpc.Factory; 5 | 6 | import java.util.Map; 7 | import java.util.concurrent.CompletableFuture; 8 | import java.util.concurrent.ConcurrentHashMap; 9 | 10 | /** 11 | * @author Yang Lianhuan 12 | * @version 1.0.0 13 | * @since 2023/7/10 14 | **/ 15 | public class RequestFuture implements Factory> { 16 | 17 | private final Map> futureMap = new ConcurrentHashMap<>(); 18 | 19 | @Override 20 | public void register(String key, CompletableFuture value) { 21 | if (!futureMap.containsKey(key)) { 22 | futureMap.put(key, value); 23 | } 24 | } 25 | 26 | @Override 27 | public boolean contains(String key) { 28 | return futureMap.containsKey(key); 29 | } 30 | 31 | @Override 32 | public CompletableFuture get(String key) { 33 | return futureMap.get(key); 34 | } 35 | 36 | @Override 37 | public void remove(String key) { 38 | futureMap.remove(key); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/client/ResponseHandler.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.client; 2 | 3 | import com.google.protobuf.InvalidProtocolBufferException; 4 | import com.google.protobuf.Message; 5 | import org.qrpc.Meta; 6 | import org.qrpc.exception.UnKnownResponseException; 7 | import org.qrpc.net.Receiver; 8 | import org.qrpc.server.MethodInfoHolder; 9 | import lombok.Getter; 10 | 11 | /** 12 | * 返回体处理 13 | * 14 | * @author Yang Lianhuan 15 | * @version 1.0.0 16 | * @since 2024/2/3 17 | **/ 18 | @Getter 19 | public class ResponseHandler implements Receiver { 20 | 21 | private final RequestCache requestCache; 22 | private final RequestFuture requestFuture; 23 | 24 | public ResponseHandler(RequestCache requestCache, RequestFuture requestFuture) { 25 | this.requestCache = requestCache; 26 | this.requestFuture = requestFuture; 27 | } 28 | 29 | @Override 30 | public void onMessage(Object object) { 31 | Meta.RpcMetaData metaData = (Meta.RpcMetaData) object; 32 | String requestId = metaData.getRequestId(); 33 | if (!requestCache.contains(requestId)) { 34 | throw new UnKnownResponseException(); 35 | } 36 | MethodInfoHolder methodInfo = requestCache.get(requestId); 37 | Message response; 38 | try { 39 | response = methodInfo.getResponsePrototype().getParserForType().parseFrom(metaData.getContent()); 40 | } catch (InvalidProtocolBufferException e) { 41 | methodInfo.getController().setFailed("response content can not parse to type"); 42 | e.printStackTrace(); 43 | return; 44 | } 45 | if (requestFuture.contains(requestId)) { 46 | requestFuture.get(requestId).complete(response); 47 | requestFuture.remove(requestId); 48 | return; 49 | } 50 | methodInfo.getCallback().run(response); 51 | requestCache.remove(requestId); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/exception/ClientChannelNotActive.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.exception; 2 | 3 | /** 4 | * @author Yang Lianhuan 5 | * @version 1.0.0 6 | * @since 2024/2/3 7 | **/ 8 | public class ClientChannelNotActive extends RuntimeException{ 9 | public ClientChannelNotActive() { 10 | super("client channel is not active"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/exception/DuplicateServiceException.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.exception; 2 | 3 | /** 4 | * @author Yang Lianhuan 5 | * @version 1.0.0 6 | * @since 2023/7/3 7 | **/ 8 | public class DuplicateServiceException extends RuntimeException { 9 | public DuplicateServiceException(String message) { 10 | super(message); 11 | } 12 | 13 | public DuplicateServiceException() { 14 | super("Service already exist."); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/exception/UnKnownResponseException.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.exception; 2 | 3 | /** 4 | * @author Yang Lianhuan 5 | * @version 1.0.0 6 | * @since 2023/7/9 7 | **/ 8 | public class UnKnownResponseException extends RuntimeException { 9 | public UnKnownResponseException(String message) { 10 | super(message); 11 | } 12 | 13 | public UnKnownResponseException() { 14 | super("response unknown requestId"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/net/NettyClientEndpoint.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.net; 2 | 3 | import org.qrpc.Meta; 4 | import org.qrpc.ThreadService; 5 | import org.qrpc.client.Endpoint; 6 | import io.netty.bootstrap.Bootstrap; 7 | import io.netty.channel.*; 8 | import io.netty.channel.socket.SocketChannel; 9 | import io.netty.channel.socket.nio.NioSocketChannel; 10 | import io.netty.handler.codec.protobuf.ProtobufDecoder; 11 | import io.netty.handler.codec.protobuf.ProtobufEncoder; 12 | import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder; 13 | import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; 14 | import io.netty.util.concurrent.GenericFutureListener; 15 | import lombok.extern.slf4j.Slf4j; 16 | 17 | /** 18 | * @author Yang Lianhuan 19 | * @version 1.0.0 20 | * @since 2023/7/6 21 | **/ 22 | @Slf4j 23 | public class NettyClientEndpoint extends Endpoint { 24 | private final String ip; 25 | private final int port; 26 | private final EventLoopGroup eventLoopGroup; 27 | private ChannelFuture future; 28 | private final ChannelInboundHandler inboundHandler; 29 | 30 | 31 | public NettyClientEndpoint(String ip, int port) { 32 | super(); 33 | this.ip = ip; 34 | this.port = port; 35 | this.eventLoopGroup = ThreadService.getClientEventLoopGroup(); 36 | this.inboundHandler = new NettyClientInboundHandler(this); 37 | init(); 38 | } 39 | 40 | private void init() { 41 | Bootstrap bootstrap = new Bootstrap(); 42 | bootstrap.group(eventLoopGroup) 43 | .channel(NioSocketChannel.class) 44 | .option(ChannelOption.TCP_NODELAY, true) 45 | .option(ChannelOption.SO_KEEPALIVE, true) 46 | .handler(new ChannelInitializer() { 47 | @Override 48 | protected void initChannel(SocketChannel ch) throws Exception { 49 | ch.pipeline() 50 | .addLast(new ProtobufVarint32FrameDecoder()) 51 | .addLast(new ProtobufDecoder(Meta.RpcMetaData.getDefaultInstance())) 52 | .addLast(new ProtobufVarint32LengthFieldPrepender()) 53 | .addLast(new ProtobufEncoder()) 54 | .addLast(inboundHandler); 55 | } 56 | }); 57 | try { 58 | future = bootstrap.connect(ip, port).sync(); 59 | future.addListener((GenericFutureListener) future1 -> { 60 | if (!future1.isSuccess()) { 61 | log.error("connect to {}:{} failed retrying", ip, port); 62 | init(); 63 | return; 64 | } 65 | log.info("connected to {}:{}", ip, port); 66 | setReady(true); 67 | }); 68 | } catch (InterruptedException e) { 69 | Thread.currentThread().interrupt(); 70 | e.printStackTrace(); 71 | } 72 | } 73 | 74 | @Override 75 | public void send(Object o) { 76 | future.channel().writeAndFlush(o); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/net/NettyClientInboundHandler.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.net; 2 | 3 | import org.qrpc.Meta; 4 | import org.qrpc.client.ResponseHandler; 5 | import io.netty.channel.ChannelHandler; 6 | import io.netty.channel.ChannelHandlerContext; 7 | import io.netty.channel.SimpleChannelInboundHandler; 8 | 9 | /** 10 | * Client method receive message from server, all client stub share the same inboundHandler here, 11 | * when channelRead() invoked, we get a @Mata.MetaData, from which we can get serviceName and methodId. 12 | * we find specific callback function RpcController and RpcCallback and invoke them. 13 | * 14 | * @author Yang Lianhuan 15 | * @version 1.0.0 16 | * @since 2023/7/8 17 | **/ 18 | @ChannelHandler.Sharable 19 | public class NettyClientInboundHandler extends SimpleChannelInboundHandler { 20 | 21 | private final Receiver receiver; 22 | 23 | public NettyClientInboundHandler(Receiver receiver) { 24 | this.receiver = receiver; 25 | } 26 | 27 | @Override 28 | protected void channelRead0(ChannelHandlerContext channelHandlerContext, Meta.RpcMetaData metaData) { 29 | receiver.onMessage(metaData); 30 | } 31 | 32 | @Override 33 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 34 | cause.printStackTrace(); 35 | ctx.close(); 36 | } 37 | 38 | @Override 39 | public void channelActive(ChannelHandlerContext ctx) { 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/net/NettyServerInboundHandler.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.net; 2 | 3 | import org.qrpc.Meta; 4 | import io.netty.channel.ChannelHandlerContext; 5 | import io.netty.channel.SimpleChannelInboundHandler; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.qrpc.server.ServiceExecutor; 8 | 9 | /** 10 | * @author Yang Lianhuan 11 | * @version 1.0.0 12 | * @since 2023/7/4 13 | **/ 14 | @Slf4j 15 | public class NettyServerInboundHandler extends SimpleChannelInboundHandler { 16 | 17 | private final Responsible responsible; 18 | 19 | public NettyServerInboundHandler(Responsible responsible) { 20 | this.responsible = responsible; 21 | } 22 | 23 | @Override 24 | protected void channelRead0(ChannelHandlerContext channelHandlerContext, Meta.RpcMetaData metaData) { 25 | Sender nettySeder = new NettyServerSender(channelHandlerContext); 26 | responsible.onMessage(metaData, nettySeder); 27 | } 28 | 29 | @Override 30 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 31 | log.error(cause.getMessage()); 32 | ctx.fireExceptionCaught(cause); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/net/NettyServerSender.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.net; 2 | 3 | import io.netty.channel.ChannelHandlerContext; 4 | 5 | /** 6 | * @author Yang Lianhuan 7 | * @version 1.0.0 8 | * @since 2024/2/3 9 | **/ 10 | public class NettyServerSender implements Sender { 11 | private final ChannelHandlerContext channelHandlerContext; 12 | 13 | public NettyServerSender(ChannelHandlerContext channelHandlerContext) { 14 | this.channelHandlerContext = channelHandlerContext; 15 | } 16 | 17 | @Override 18 | public void send(Object o) { 19 | channelHandlerContext.writeAndFlush(o); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/net/NettyTCPServer.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.net; 2 | 3 | import org.qrpc.Banner; 4 | import org.qrpc.Meta; 5 | import org.qrpc.ThreadService; 6 | import org.qrpc.server.AbstractServer; 7 | import org.qrpc.server.ServerBuilder; 8 | import io.netty.bootstrap.ServerBootstrap; 9 | import io.netty.channel.*; 10 | import io.netty.channel.socket.SocketChannel; 11 | import io.netty.channel.socket.nio.NioServerSocketChannel; 12 | import io.netty.handler.codec.protobuf.ProtobufDecoder; 13 | import io.netty.handler.codec.protobuf.ProtobufEncoder; 14 | import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder; 15 | import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; 16 | import lombok.extern.slf4j.Slf4j; 17 | 18 | import java.net.InetSocketAddress; 19 | 20 | /** 21 | * @author Yang Lianhuan 22 | * @version 1.0.0 23 | * @since 2023/7/1 24 | **/ 25 | @Slf4j 26 | public class NettyTCPServer extends AbstractServer { 27 | 28 | private final int port; 29 | 30 | public NettyTCPServer(int port) { 31 | this.port = port; 32 | } 33 | 34 | public NettyTCPServer(ServerBuilder builder) { 35 | this.port = builder.getPort(); 36 | } 37 | 38 | @Override 39 | public void start() { 40 | EventLoopGroup bossGroup = ThreadService.getServerBossEventLoopGroup(); 41 | EventLoopGroup workerGroup = ThreadService.getServerWorkEventLoopGroup(); 42 | try { 43 | ServerBootstrap bootstrap = new ServerBootstrap(); 44 | bootstrap.group(bossGroup, workerGroup) 45 | .channel(NioServerSocketChannel.class) 46 | .option(ChannelOption.SO_BACKLOG, 128) 47 | .localAddress(new InetSocketAddress(port)) 48 | .childOption(ChannelOption.SO_KEEPALIVE, true) 49 | .childHandler(new ChannelInitializer() { 50 | @Override 51 | protected void initChannel(SocketChannel ch) { 52 | ch.pipeline() 53 | .addLast(new ProtobufVarint32FrameDecoder()) 54 | .addLast(new ProtobufDecoder(Meta.RpcMetaData.getDefaultInstance())) 55 | .addLast(new ProtobufVarint32LengthFieldPrepender()) 56 | .addLast(new ProtobufEncoder()) 57 | .addLast(new NettyServerInboundHandler(NettyTCPServer.this)); 58 | } 59 | }); 60 | Banner.print(); 61 | try { 62 | ChannelFuture future = bootstrap.bind().sync(); 63 | future.addListener((ChannelFutureListener) channelFuture -> Banner.printStarted(port)); 64 | future.channel().closeFuture().sync(); 65 | } catch (InterruptedException e) { 66 | e.printStackTrace(); 67 | } 68 | } finally { 69 | bossGroup.shutdownGracefully(); 70 | workerGroup.shutdownGracefully(); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/net/Receiver.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.net; 2 | 3 | /** 4 | * 消息接收器,底层网络框架实现此接口接收消息 5 | * 6 | * @author Yang Lianhuan 7 | * @version 1.0.0 8 | * @since 2024/2/3 9 | **/ 10 | public interface Receiver { 11 | void onMessage(Object object); 12 | } 13 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/net/Responsible.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.net; 2 | 3 | /** 4 | * @author Yang Lianhuan 5 | * @version 1.0.0 6 | * @since 2024/2/4 7 | **/ 8 | public interface Responsible { 9 | void onMessage(Object o, Sender responder); 10 | } 11 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/net/Sender.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.net; 2 | 3 | /** 4 | * 此为发送消息实现类,底层通信框架实现此接口发送消息 5 | * 6 | * @author Yang Lianhuan 7 | * @version 1.0.0 8 | * @since 2024/2/3 9 | **/ 10 | public interface Sender { 11 | void send(Object o); 12 | } 13 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/net/Server.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.net; 2 | 3 | /** 4 | * 服务器,底层通信框架实现此接口启动服务 5 | * 6 | * @author Yang Lianhuan 7 | * @version 1.0.0 8 | * @since 2023/7/1 9 | **/ 10 | public interface Server extends Responsible { 11 | void start(); 12 | } 13 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/server/AbstractServer.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.server; 2 | 3 | import lombok.Getter; 4 | import org.qrpc.net.Receiver; 5 | import org.qrpc.net.Responsible; 6 | import org.qrpc.net.Sender; 7 | import org.qrpc.net.Server; 8 | 9 | /** 10 | * 需要实现start方法来启动服务,需要回调onMessage来接收消息并传入消息回复器 11 | * 12 | * @author Yang Lianhuan 13 | * @version 1.0.0 14 | * @since 2024/2/4 15 | **/ 16 | @Getter 17 | public abstract class AbstractServer implements Server { 18 | 19 | @Override 20 | public void onMessage(Object o, Sender responder) { 21 | // 因为每次客户端可能不一样,因此每次收到消息都创建一个ServiceExecutor来应答消息,防止消息走串 22 | new ServiceExecutor(responder).onMessage(o); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/server/ListableServiceFactory.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.server; 2 | 3 | import com.google.protobuf.Service; 4 | import org.qrpc.exception.DuplicateServiceException; 5 | 6 | import java.util.Map; 7 | import java.util.concurrent.ConcurrentHashMap; 8 | 9 | /** 10 | * @author Yang Lianhuan 11 | * @version 1.0.0 12 | * @since 2023/4/3 13 | **/ 14 | public class ListableServiceFactory implements ServiceFactory { 15 | private static volatile ServiceFactory serviceFactory; 16 | private final Map serviceMap = new ConcurrentHashMap<>(); 17 | 18 | @Override 19 | public void register(Service service) { 20 | ServiceHolder serviceHolder = new ServiceHolder(service); 21 | register(service.getDescriptorForType().getFullName(), serviceHolder); 22 | } 23 | 24 | @Override 25 | public void register(String name, ServiceHolder serviceHolder) { 26 | if (serviceMap.containsKey(name)) { 27 | throw new DuplicateServiceException(); 28 | } 29 | serviceMap.put(name, serviceHolder); 30 | } 31 | 32 | @Override 33 | public boolean contains(String name) { 34 | return serviceMap.containsKey(name); 35 | } 36 | 37 | @Override 38 | public ServiceHolder get(String name) { 39 | return serviceMap.get(name); 40 | } 41 | 42 | @Override 43 | public void remove(String key) { 44 | serviceMap.remove(key); 45 | } 46 | 47 | public static ServiceFactory getInstance() { 48 | if (serviceFactory != null) { 49 | return serviceFactory; 50 | } 51 | synchronized (ListableServiceFactory.class) { 52 | if (serviceFactory != null) { 53 | return serviceFactory; 54 | } 55 | serviceFactory = new ListableServiceFactory(); 56 | return serviceFactory; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/server/MethodInfoHolder.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.server; 2 | 3 | import com.google.protobuf.Descriptors; 4 | import com.google.protobuf.Message; 5 | import com.google.protobuf.RpcCallback; 6 | import com.google.protobuf.RpcController; 7 | import lombok.Getter; 8 | 9 | /** 10 | * @author Yang Lianhuan 11 | * @version 1.0.0 12 | * @since 2023/4/8 13 | **/ 14 | @Getter 15 | public class MethodInfoHolder { 16 | private Descriptors.MethodDescriptor method; 17 | private RpcController controller; 18 | private com.google.protobuf.Message request; 19 | private Message responsePrototype; 20 | private RpcCallback callback; 21 | 22 | public static MethodInfoHolder builder() { 23 | return new MethodInfoHolder(); 24 | } 25 | 26 | public MethodInfoHolder method(Descriptors.MethodDescriptor methodDescriptor) { 27 | this.method = methodDescriptor; 28 | return this; 29 | } 30 | 31 | public MethodInfoHolder controller(RpcController controller) { 32 | this.controller = controller; 33 | return this; 34 | } 35 | 36 | public MethodInfoHolder request(com.google.protobuf.Message message) { 37 | this.request = message; 38 | return this; 39 | } 40 | 41 | public MethodInfoHolder responsePrototype(Message responsePrototype) { 42 | this.responsePrototype = responsePrototype; 43 | return this; 44 | } 45 | 46 | public MethodInfoHolder callback(RpcCallback callback) { 47 | this.callback = callback; 48 | return this; 49 | } 50 | 51 | public MethodInfoHolder build() { 52 | return this; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/server/QRpcController.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.server; 2 | 3 | import com.google.protobuf.RpcCallback; 4 | import com.google.protobuf.RpcController; 5 | import org.qrpc.Meta; 6 | import org.qrpc.RpcStatus; 7 | import org.qrpc.utils.ResponseManager; 8 | 9 | /** 10 | * @author Yang Lianhuan 11 | * @version 1.0.0 12 | * @since 2023/7/4 13 | **/ 14 | public class QRpcController implements RpcController { 15 | private RpcStatus status; 16 | private String reason; 17 | private final Meta.RpcMetaData metaData; 18 | 19 | public QRpcController(Meta.RpcMetaData metaData) { 20 | this.metaData = metaData; 21 | } 22 | 23 | @Override 24 | public void reset() { 25 | status = RpcStatus.INIT; 26 | } 27 | 28 | @Override 29 | public boolean failed() { 30 | return this.status == RpcStatus.FAILED; 31 | } 32 | 33 | @Override 34 | public String errorText() { 35 | return reason; 36 | } 37 | 38 | @Override 39 | public void startCancel() { 40 | this.status = RpcStatus.CANCELING; 41 | } 42 | 43 | @Override 44 | public void setFailed(String reason) { 45 | this.status = RpcStatus.FAILED; 46 | this.reason = reason; 47 | } 48 | 49 | @Override 50 | public boolean isCanceled() { 51 | return this.status == RpcStatus.CANCELED; 52 | } 53 | 54 | @Override 55 | public void notifyOnCancel(RpcCallback callback) { 56 | Meta.RpcMetaData errData = ResponseManager.getFailedResponse(this.metaData, Meta.Status.SYSTEM_ERROR); 57 | callback.run(errData); 58 | } 59 | 60 | public void notifyOnFailed(RespondCallBack callback) { 61 | Meta.RpcMetaData errData = ResponseManager.getFailedResponse(this.metaData, Meta.Status.SYSTEM_ERROR); 62 | callback.run(errData); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/server/RespondCallBack.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.server; 2 | 3 | import com.google.protobuf.Message; 4 | import com.google.protobuf.RpcCallback; 5 | import org.qrpc.Meta; 6 | import org.qrpc.net.Sender; 7 | import org.qrpc.utils.ResponseManager; 8 | 9 | /** 10 | * @author Yang Lianhuan 11 | * @version 1.0.0 12 | * @since 2023/7/4 13 | **/ 14 | public class RespondCallBack implements RpcCallback { 15 | 16 | private final Sender sender; 17 | private final Meta.RpcMetaData metaData; 18 | 19 | public RespondCallBack(Sender sender, Meta.RpcMetaData metaData) { 20 | this.sender = sender; 21 | this.metaData = metaData; 22 | } 23 | 24 | @Override 25 | public void run(Message response) { 26 | Meta.RpcMetaData rpcMetaData = ResponseManager.getSuccessResponse(this.metaData, response); 27 | sender.send(rpcMetaData); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/server/ServerBuilder.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.server; 2 | 3 | import com.google.protobuf.Service; 4 | import org.qrpc.net.Server; 5 | import org.qrpc.net.NettyTCPServer; 6 | import lombok.Getter; 7 | 8 | /** 9 | * @author Yang Lianhuan 10 | * @version 1.0.0 11 | * @since 2023/7/9 12 | **/ 13 | @Getter 14 | public class ServerBuilder { 15 | private final int port; 16 | private final ServiceFactory serviceFactory = ListableServiceFactory.getInstance(); 17 | private ServerBuilder(int port) { 18 | this.port = port; 19 | } 20 | public static ServerBuilder forPort(int port) { 21 | return new ServerBuilder(port); 22 | } 23 | 24 | public ServerBuilder addService(Service service) { 25 | serviceFactory.register(service); 26 | return this; 27 | } 28 | 29 | public Server build() { 30 | return new NettyTCPServer(this); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/server/ServiceExecutor.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.server; 2 | 3 | import org.qrpc.Meta; 4 | import org.qrpc.ThreadService; 5 | import org.qrpc.net.Receiver; 6 | import org.qrpc.net.Sender; 7 | import org.qrpc.utils.ResponseManager; 8 | 9 | /** 10 | * 运行业务逻辑 11 | * 12 | * @author Yang Lianhuan 13 | * @version 1.0.0 14 | * @since 2024/2/3 15 | **/ 16 | public class ServiceExecutor implements Receiver { 17 | 18 | private final ServiceFactory serviceFactory = ListableServiceFactory.getInstance(); 19 | private final Sender sender; 20 | 21 | public ServiceExecutor(Sender sender) { 22 | this.sender = sender; 23 | } 24 | 25 | @Override 26 | public void onMessage(Object object) { 27 | Meta.RpcMetaData metaData = (Meta.RpcMetaData) object; 28 | String serviceName = metaData.getServiceName(); 29 | if (!serviceFactory.contains(serviceName)) { 30 | Meta.RpcMetaData errData = ResponseManager.getFailedResponse(metaData, Meta.Status.SERVICE_NOT_FOUND); 31 | sender.send(errData); 32 | return; 33 | } 34 | ServiceHolder targetService = serviceFactory.get(serviceName); 35 | ServiceRunner runner = new ServiceRunner(sender, targetService, metaData); 36 | ThreadService.getBusinessExecutor().submit(runner); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/server/ServiceFactory.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.server; 2 | 3 | import com.google.protobuf.Service; 4 | import org.qrpc.Factory; 5 | 6 | /** 7 | * @author Yang Lianhuan 8 | * @version 1.0.0 9 | * @since 2023/4/3 10 | **/ 11 | public interface ServiceFactory extends Factory { 12 | void register(Service service); 13 | 14 | void register(String name, ServiceHolder serviceHolder); 15 | 16 | boolean contains(String name); 17 | 18 | ServiceHolder get(String name); 19 | } 20 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/server/ServiceHolder.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.server; 2 | 3 | import com.google.protobuf.*; 4 | import org.qrpc.Meta; 5 | 6 | /** 7 | * @author Yang Lianhuan 8 | * @version 1.0.0 9 | * @since 2023/4/3 10 | **/ 11 | public class ServiceHolder { 12 | private final Service service; 13 | 14 | public ServiceHolder(Service service) { 15 | this.service = service; 16 | } 17 | 18 | public void callMethod(int methodId, RpcController controller, Meta.RpcMetaData meta, RpcCallback callback) { 19 | Descriptors.MethodDescriptor methodDescriptor = this.service.getDescriptorForType().getMethods().get(methodId); 20 | Message request; 21 | try { 22 | request = this.service.getRequestPrototype(methodDescriptor).getParserForType().parseFrom(meta.getContent()); 23 | } catch (InvalidProtocolBufferException e) { 24 | controller.setFailed("InvalidProtocolBuffer"); 25 | return; 26 | } 27 | this.service.callMethod(methodDescriptor, controller, request, callback); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/server/ServiceRunner.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.server; 2 | 3 | import org.qrpc.Meta; 4 | import org.qrpc.net.Sender; 5 | 6 | import java.util.Objects; 7 | 8 | /** 9 | * 执行实际的业务逻辑,业务逻辑不能在IO线程中处理,否则阻塞IO线程影响处理其他请求 10 | * 11 | * @author Yang Lianhuan 12 | * @version 1.0.0 13 | * @since 2024/2/3 14 | **/ 15 | public class ServiceRunner implements Runnable { 16 | 17 | private final ServiceHolder targetService; 18 | 19 | private final Meta.RpcMetaData metaData; 20 | 21 | private final Sender sender; 22 | 23 | public ServiceRunner(Sender sender, ServiceHolder targetService, Meta.RpcMetaData metaData) { 24 | this.targetService = targetService; 25 | this.metaData = metaData; 26 | this.sender = sender; 27 | } 28 | 29 | @Override 30 | public void run() { 31 | int methodId = metaData.getMethodId(); 32 | QRpcController qRpcController = new QRpcController(metaData); 33 | if (!verifyContext()) { 34 | qRpcController.setFailed("context info invalid, run service failed"); 35 | return; 36 | } 37 | RespondCallBack callback = new RespondCallBack(sender, metaData); 38 | targetService.callMethod(methodId, qRpcController, metaData, callback); 39 | if (qRpcController.failed() || qRpcController.isCanceled()) { 40 | qRpcController.notifyOnFailed(callback); 41 | } 42 | } 43 | 44 | private boolean verifyContext() { 45 | return !Objects.isNull(targetService) && !Objects.isNull(metaData) && !Objects.isNull(sender); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /qrpc-core/src/main/java/org/qrpc/utils/ResponseManager.java: -------------------------------------------------------------------------------- 1 | package org.qrpc.utils; 2 | 3 | import com.google.protobuf.Message; 4 | import org.qrpc.Meta; 5 | 6 | /** 7 | * 响应体管理 8 | * 9 | * @author Yang Lianhuan 10 | * @version 1.0.0 11 | * @since 2024/1/28 12 | **/ 13 | public class ResponseManager { 14 | /** 15 | * 成功响应体 16 | * 17 | * @param metaData metaData 18 | * @param response response 19 | * @return RpcMetaData 20 | */ 21 | public static Meta.RpcMetaData getSuccessResponse(Meta.RpcMetaData metaData, Message response) { 22 | return Meta.RpcMetaData.newBuilder() 23 | .setStatus(Meta.Status.SUCCESS) 24 | .setServiceName(metaData.getServiceName()) 25 | .setMethodId(metaData.getMethodId()) 26 | .setType(Meta.Type.RESPOND) 27 | .setContent(response.toByteString()) 28 | .setRequestId(metaData.getRequestId()) 29 | .build(); 30 | } 31 | 32 | /** 33 | * 返回失败响应体 34 | * 35 | * @param metaData 请求体 36 | * @param status 状态码 37 | * @return RpcMetaData 38 | */ 39 | public static Meta.RpcMetaData getFailedResponse(Meta.RpcMetaData metaData, Meta.Status status) { 40 | return Meta.RpcMetaData.newBuilder() 41 | .setStatus(status) 42 | .setServiceName(metaData.getServiceName()) 43 | .setMethodId(metaData.getMethodId()) 44 | .setType(Meta.Type.RESPOND) 45 | .setRequestId(metaData.getRequestId()) 46 | .build(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /qrpc-core/src/main/resources/meta.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | option java_multiple_files = false; 4 | option java_package = "org.qrpc"; 5 | option java_outer_classname = "Meta"; 6 | 7 | package qrpc.meta; 8 | 9 | message RpcMetaData { 10 | string service_name = 1; 11 | int32 method_id = 2; 12 | bytes content = 3; 13 | Status status = 4; 14 | Type type = 5; 15 | string request_id = 6; 16 | } 17 | 18 | enum Status { 19 | SUCCESS = 0; 20 | TIME_OUT = 2; 21 | SERVICE_NOT_FOUND = 3; 22 | SYSTEM_ERROR = 4; 23 | } 24 | 25 | enum Type { 26 | REQUEST = 0; 27 | RESPOND = 1; 28 | PEER = 2; 29 | } -------------------------------------------------------------------------------- /qrpc-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.barackyoung 8 | qrpc 9 | 1.0.0-SNAPSHOT 10 | 11 | 12 | qrpc-demo 13 | 14 | 15 | 8 16 | 8 17 | UTF-8 18 | 19 | 20 | 21 | 22 | io.github.barackyoung 23 | qrpc-core 24 | 1.0.0-SNAPSHOT 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /qrpc-demo/src/main/java/Client.java: -------------------------------------------------------------------------------- 1 | import com.google.protobuf.*; 2 | import org.qrpc.client.Channel.SimpleBlockQRpcChannel; 3 | import org.qrpc.client.Channel.SimpleQRpcChannel; 4 | 5 | /** 6 | * @author Yang Lianhuan 7 | * @version 1.0.0 8 | * @since 2023/7/9 9 | **/ 10 | public class Client { 11 | public static void main(String[] args) throws ServiceException { 12 | Demo.request request = Demo.request.newBuilder() 13 | .setMessage("hi, server!") 14 | .build(); 15 | 16 | // synchronous usage 17 | Demo.DemoService.BlockingInterface blockingStub = Demo.DemoService.newBlockingStub(SimpleBlockQRpcChannel.forAddress("127.0.0.1", 8888).build()); 18 | Demo.response response = blockingStub.sendMessage(null, request); 19 | System.out.println("synchronous response: " + response.getMessage()); 20 | 21 | // asynchronous usage 22 | Demo.DemoService.Stub stub = Demo.DemoService.newStub(SimpleQRpcChannel.forAddress("127.0.0.1", 8888).build()); 23 | stub.sendMessage(null, request, new RpcCallback() { 24 | @Override 25 | public void run(Demo.response parameter) { 26 | System.out.println("asynchronous response: " + response.getMessage()); 27 | } 28 | }); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /qrpc-demo/src/main/java/Demo.java: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: demo.proto 3 | 4 | public final class Demo { 5 | private Demo() {} 6 | public static void registerAllExtensions( 7 | com.google.protobuf.ExtensionRegistryLite registry) { 8 | } 9 | 10 | public static void registerAllExtensions( 11 | com.google.protobuf.ExtensionRegistry registry) { 12 | registerAllExtensions( 13 | (com.google.protobuf.ExtensionRegistryLite) registry); 14 | } 15 | public interface requestOrBuilder extends 16 | // @@protoc_insertion_point(interface_extends:qrpc.demo.request) 17 | com.google.protobuf.MessageOrBuilder { 18 | 19 | /** 20 | * string message = 1; 21 | * @return The message. 22 | */ 23 | java.lang.String getMessage(); 24 | /** 25 | * string message = 1; 26 | * @return The bytes for message. 27 | */ 28 | com.google.protobuf.ByteString 29 | getMessageBytes(); 30 | } 31 | /** 32 | * Protobuf type {@code qrpc.demo.request} 33 | */ 34 | public static final class request extends 35 | com.google.protobuf.GeneratedMessageV3 implements 36 | // @@protoc_insertion_point(message_implements:qrpc.demo.request) 37 | requestOrBuilder { 38 | private static final long serialVersionUID = 0L; 39 | // Use request.newBuilder() to construct. 40 | private request(com.google.protobuf.GeneratedMessageV3.Builder builder) { 41 | super(builder); 42 | } 43 | private request() { 44 | message_ = ""; 45 | } 46 | 47 | @java.lang.Override 48 | @SuppressWarnings({"unused"}) 49 | protected java.lang.Object newInstance( 50 | UnusedPrivateParameter unused) { 51 | return new request(); 52 | } 53 | 54 | public static final com.google.protobuf.Descriptors.Descriptor 55 | getDescriptor() { 56 | return Demo.internal_static_qrpc_demo_request_descriptor; 57 | } 58 | 59 | @java.lang.Override 60 | protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 61 | internalGetFieldAccessorTable() { 62 | return Demo.internal_static_qrpc_demo_request_fieldAccessorTable 63 | .ensureFieldAccessorsInitialized( 64 | Demo.request.class, Demo.request.Builder.class); 65 | } 66 | 67 | public static final int MESSAGE_FIELD_NUMBER = 1; 68 | @SuppressWarnings("serial") 69 | private volatile java.lang.Object message_ = ""; 70 | /** 71 | * string message = 1; 72 | * @return The message. 73 | */ 74 | @java.lang.Override 75 | public java.lang.String getMessage() { 76 | java.lang.Object ref = message_; 77 | if (ref instanceof java.lang.String) { 78 | return (java.lang.String) ref; 79 | } else { 80 | com.google.protobuf.ByteString bs = 81 | (com.google.protobuf.ByteString) ref; 82 | java.lang.String s = bs.toStringUtf8(); 83 | message_ = s; 84 | return s; 85 | } 86 | } 87 | /** 88 | * string message = 1; 89 | * @return The bytes for message. 90 | */ 91 | @java.lang.Override 92 | public com.google.protobuf.ByteString 93 | getMessageBytes() { 94 | java.lang.Object ref = message_; 95 | if (ref instanceof java.lang.String) { 96 | com.google.protobuf.ByteString b = 97 | com.google.protobuf.ByteString.copyFromUtf8( 98 | (java.lang.String) ref); 99 | message_ = b; 100 | return b; 101 | } else { 102 | return (com.google.protobuf.ByteString) ref; 103 | } 104 | } 105 | 106 | private byte memoizedIsInitialized = -1; 107 | @java.lang.Override 108 | public final boolean isInitialized() { 109 | byte isInitialized = memoizedIsInitialized; 110 | if (isInitialized == 1) return true; 111 | if (isInitialized == 0) return false; 112 | 113 | memoizedIsInitialized = 1; 114 | return true; 115 | } 116 | 117 | @java.lang.Override 118 | public void writeTo(com.google.protobuf.CodedOutputStream output) 119 | throws java.io.IOException { 120 | if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(message_)) { 121 | com.google.protobuf.GeneratedMessageV3.writeString(output, 1, message_); 122 | } 123 | getUnknownFields().writeTo(output); 124 | } 125 | 126 | @java.lang.Override 127 | public int getSerializedSize() { 128 | int size = memoizedSize; 129 | if (size != -1) return size; 130 | 131 | size = 0; 132 | if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(message_)) { 133 | size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, message_); 134 | } 135 | size += getUnknownFields().getSerializedSize(); 136 | memoizedSize = size; 137 | return size; 138 | } 139 | 140 | @java.lang.Override 141 | public boolean equals(final java.lang.Object obj) { 142 | if (obj == this) { 143 | return true; 144 | } 145 | if (!(obj instanceof Demo.request)) { 146 | return super.equals(obj); 147 | } 148 | Demo.request other = (Demo.request) obj; 149 | 150 | if (!getMessage() 151 | .equals(other.getMessage())) return false; 152 | if (!getUnknownFields().equals(other.getUnknownFields())) return false; 153 | return true; 154 | } 155 | 156 | @java.lang.Override 157 | public int hashCode() { 158 | if (memoizedHashCode != 0) { 159 | return memoizedHashCode; 160 | } 161 | int hash = 41; 162 | hash = (19 * hash) + getDescriptor().hashCode(); 163 | hash = (37 * hash) + MESSAGE_FIELD_NUMBER; 164 | hash = (53 * hash) + getMessage().hashCode(); 165 | hash = (29 * hash) + getUnknownFields().hashCode(); 166 | memoizedHashCode = hash; 167 | return hash; 168 | } 169 | 170 | public static Demo.request parseFrom( 171 | java.nio.ByteBuffer data) 172 | throws com.google.protobuf.InvalidProtocolBufferException { 173 | return PARSER.parseFrom(data); 174 | } 175 | public static Demo.request parseFrom( 176 | java.nio.ByteBuffer data, 177 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 178 | throws com.google.protobuf.InvalidProtocolBufferException { 179 | return PARSER.parseFrom(data, extensionRegistry); 180 | } 181 | public static Demo.request parseFrom( 182 | com.google.protobuf.ByteString data) 183 | throws com.google.protobuf.InvalidProtocolBufferException { 184 | return PARSER.parseFrom(data); 185 | } 186 | public static Demo.request parseFrom( 187 | com.google.protobuf.ByteString data, 188 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 189 | throws com.google.protobuf.InvalidProtocolBufferException { 190 | return PARSER.parseFrom(data, extensionRegistry); 191 | } 192 | public static Demo.request parseFrom(byte[] data) 193 | throws com.google.protobuf.InvalidProtocolBufferException { 194 | return PARSER.parseFrom(data); 195 | } 196 | public static Demo.request parseFrom( 197 | byte[] data, 198 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 199 | throws com.google.protobuf.InvalidProtocolBufferException { 200 | return PARSER.parseFrom(data, extensionRegistry); 201 | } 202 | public static Demo.request parseFrom(java.io.InputStream input) 203 | throws java.io.IOException { 204 | return com.google.protobuf.GeneratedMessageV3 205 | .parseWithIOException(PARSER, input); 206 | } 207 | public static Demo.request parseFrom( 208 | java.io.InputStream input, 209 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 210 | throws java.io.IOException { 211 | return com.google.protobuf.GeneratedMessageV3 212 | .parseWithIOException(PARSER, input, extensionRegistry); 213 | } 214 | 215 | public static Demo.request parseDelimitedFrom(java.io.InputStream input) 216 | throws java.io.IOException { 217 | return com.google.protobuf.GeneratedMessageV3 218 | .parseDelimitedWithIOException(PARSER, input); 219 | } 220 | 221 | public static Demo.request parseDelimitedFrom( 222 | java.io.InputStream input, 223 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 224 | throws java.io.IOException { 225 | return com.google.protobuf.GeneratedMessageV3 226 | .parseDelimitedWithIOException(PARSER, input, extensionRegistry); 227 | } 228 | public static Demo.request parseFrom( 229 | com.google.protobuf.CodedInputStream input) 230 | throws java.io.IOException { 231 | return com.google.protobuf.GeneratedMessageV3 232 | .parseWithIOException(PARSER, input); 233 | } 234 | public static Demo.request parseFrom( 235 | com.google.protobuf.CodedInputStream input, 236 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 237 | throws java.io.IOException { 238 | return com.google.protobuf.GeneratedMessageV3 239 | .parseWithIOException(PARSER, input, extensionRegistry); 240 | } 241 | 242 | @java.lang.Override 243 | public Builder newBuilderForType() { return newBuilder(); } 244 | public static Builder newBuilder() { 245 | return DEFAULT_INSTANCE.toBuilder(); 246 | } 247 | public static Builder newBuilder(Demo.request prototype) { 248 | return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); 249 | } 250 | @java.lang.Override 251 | public Builder toBuilder() { 252 | return this == DEFAULT_INSTANCE 253 | ? new Builder() : new Builder().mergeFrom(this); 254 | } 255 | 256 | @java.lang.Override 257 | protected Builder newBuilderForType( 258 | com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { 259 | Builder builder = new Builder(parent); 260 | return builder; 261 | } 262 | /** 263 | * Protobuf type {@code qrpc.demo.request} 264 | */ 265 | public static final class Builder extends 266 | com.google.protobuf.GeneratedMessageV3.Builder implements 267 | // @@protoc_insertion_point(builder_implements:qrpc.demo.request) 268 | Demo.requestOrBuilder { 269 | public static final com.google.protobuf.Descriptors.Descriptor 270 | getDescriptor() { 271 | return Demo.internal_static_qrpc_demo_request_descriptor; 272 | } 273 | 274 | @java.lang.Override 275 | protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 276 | internalGetFieldAccessorTable() { 277 | return Demo.internal_static_qrpc_demo_request_fieldAccessorTable 278 | .ensureFieldAccessorsInitialized( 279 | Demo.request.class, Demo.request.Builder.class); 280 | } 281 | 282 | // Construct using Demo.request.newBuilder() 283 | private Builder() { 284 | 285 | } 286 | 287 | private Builder( 288 | com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { 289 | super(parent); 290 | 291 | } 292 | @java.lang.Override 293 | public Builder clear() { 294 | super.clear(); 295 | bitField0_ = 0; 296 | message_ = ""; 297 | return this; 298 | } 299 | 300 | @java.lang.Override 301 | public com.google.protobuf.Descriptors.Descriptor 302 | getDescriptorForType() { 303 | return Demo.internal_static_qrpc_demo_request_descriptor; 304 | } 305 | 306 | @java.lang.Override 307 | public Demo.request getDefaultInstanceForType() { 308 | return Demo.request.getDefaultInstance(); 309 | } 310 | 311 | @java.lang.Override 312 | public Demo.request build() { 313 | Demo.request result = buildPartial(); 314 | if (!result.isInitialized()) { 315 | throw newUninitializedMessageException(result); 316 | } 317 | return result; 318 | } 319 | 320 | @java.lang.Override 321 | public Demo.request buildPartial() { 322 | Demo.request result = new Demo.request(this); 323 | if (bitField0_ != 0) { buildPartial0(result); } 324 | onBuilt(); 325 | return result; 326 | } 327 | 328 | private void buildPartial0(Demo.request result) { 329 | int from_bitField0_ = bitField0_; 330 | if (((from_bitField0_ & 0x00000001) != 0)) { 331 | result.message_ = message_; 332 | } 333 | } 334 | 335 | @java.lang.Override 336 | public Builder clone() { 337 | return super.clone(); 338 | } 339 | @java.lang.Override 340 | public Builder setField( 341 | com.google.protobuf.Descriptors.FieldDescriptor field, 342 | java.lang.Object value) { 343 | return super.setField(field, value); 344 | } 345 | @java.lang.Override 346 | public Builder clearField( 347 | com.google.protobuf.Descriptors.FieldDescriptor field) { 348 | return super.clearField(field); 349 | } 350 | @java.lang.Override 351 | public Builder clearOneof( 352 | com.google.protobuf.Descriptors.OneofDescriptor oneof) { 353 | return super.clearOneof(oneof); 354 | } 355 | @java.lang.Override 356 | public Builder setRepeatedField( 357 | com.google.protobuf.Descriptors.FieldDescriptor field, 358 | int index, java.lang.Object value) { 359 | return super.setRepeatedField(field, index, value); 360 | } 361 | @java.lang.Override 362 | public Builder addRepeatedField( 363 | com.google.protobuf.Descriptors.FieldDescriptor field, 364 | java.lang.Object value) { 365 | return super.addRepeatedField(field, value); 366 | } 367 | @java.lang.Override 368 | public Builder mergeFrom(com.google.protobuf.Message other) { 369 | if (other instanceof Demo.request) { 370 | return mergeFrom((Demo.request)other); 371 | } else { 372 | super.mergeFrom(other); 373 | return this; 374 | } 375 | } 376 | 377 | public Builder mergeFrom(Demo.request other) { 378 | if (other == Demo.request.getDefaultInstance()) return this; 379 | if (!other.getMessage().isEmpty()) { 380 | message_ = other.message_; 381 | bitField0_ |= 0x00000001; 382 | onChanged(); 383 | } 384 | this.mergeUnknownFields(other.getUnknownFields()); 385 | onChanged(); 386 | return this; 387 | } 388 | 389 | @java.lang.Override 390 | public final boolean isInitialized() { 391 | return true; 392 | } 393 | 394 | @java.lang.Override 395 | public Builder mergeFrom( 396 | com.google.protobuf.CodedInputStream input, 397 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 398 | throws java.io.IOException { 399 | if (extensionRegistry == null) { 400 | throw new java.lang.NullPointerException(); 401 | } 402 | try { 403 | boolean done = false; 404 | while (!done) { 405 | int tag = input.readTag(); 406 | switch (tag) { 407 | case 0: 408 | done = true; 409 | break; 410 | case 10: { 411 | message_ = input.readStringRequireUtf8(); 412 | bitField0_ |= 0x00000001; 413 | break; 414 | } // case 10 415 | default: { 416 | if (!super.parseUnknownField(input, extensionRegistry, tag)) { 417 | done = true; // was an endgroup tag 418 | } 419 | break; 420 | } // default: 421 | } // switch (tag) 422 | } // while (!done) 423 | } catch (com.google.protobuf.InvalidProtocolBufferException e) { 424 | throw e.unwrapIOException(); 425 | } finally { 426 | onChanged(); 427 | } // finally 428 | return this; 429 | } 430 | private int bitField0_; 431 | 432 | private java.lang.Object message_ = ""; 433 | /** 434 | * string message = 1; 435 | * @return The message. 436 | */ 437 | public java.lang.String getMessage() { 438 | java.lang.Object ref = message_; 439 | if (!(ref instanceof java.lang.String)) { 440 | com.google.protobuf.ByteString bs = 441 | (com.google.protobuf.ByteString) ref; 442 | java.lang.String s = bs.toStringUtf8(); 443 | message_ = s; 444 | return s; 445 | } else { 446 | return (java.lang.String) ref; 447 | } 448 | } 449 | /** 450 | * string message = 1; 451 | * @return The bytes for message. 452 | */ 453 | public com.google.protobuf.ByteString 454 | getMessageBytes() { 455 | java.lang.Object ref = message_; 456 | if (ref instanceof String) { 457 | com.google.protobuf.ByteString b = 458 | com.google.protobuf.ByteString.copyFromUtf8( 459 | (java.lang.String) ref); 460 | message_ = b; 461 | return b; 462 | } else { 463 | return (com.google.protobuf.ByteString) ref; 464 | } 465 | } 466 | /** 467 | * string message = 1; 468 | * @param value The message to set. 469 | * @return This builder for chaining. 470 | */ 471 | public Builder setMessage( 472 | java.lang.String value) { 473 | if (value == null) { throw new NullPointerException(); } 474 | message_ = value; 475 | bitField0_ |= 0x00000001; 476 | onChanged(); 477 | return this; 478 | } 479 | /** 480 | * string message = 1; 481 | * @return This builder for chaining. 482 | */ 483 | public Builder clearMessage() { 484 | message_ = getDefaultInstance().getMessage(); 485 | bitField0_ = (bitField0_ & ~0x00000001); 486 | onChanged(); 487 | return this; 488 | } 489 | /** 490 | * string message = 1; 491 | * @param value The bytes for message to set. 492 | * @return This builder for chaining. 493 | */ 494 | public Builder setMessageBytes( 495 | com.google.protobuf.ByteString value) { 496 | if (value == null) { throw new NullPointerException(); } 497 | checkByteStringIsUtf8(value); 498 | message_ = value; 499 | bitField0_ |= 0x00000001; 500 | onChanged(); 501 | return this; 502 | } 503 | @java.lang.Override 504 | public final Builder setUnknownFields( 505 | final com.google.protobuf.UnknownFieldSet unknownFields) { 506 | return super.setUnknownFields(unknownFields); 507 | } 508 | 509 | @java.lang.Override 510 | public final Builder mergeUnknownFields( 511 | final com.google.protobuf.UnknownFieldSet unknownFields) { 512 | return super.mergeUnknownFields(unknownFields); 513 | } 514 | 515 | 516 | // @@protoc_insertion_point(builder_scope:qrpc.demo.request) 517 | } 518 | 519 | // @@protoc_insertion_point(class_scope:qrpc.demo.request) 520 | private static final Demo.request DEFAULT_INSTANCE; 521 | static { 522 | DEFAULT_INSTANCE = new Demo.request(); 523 | } 524 | 525 | public static Demo.request getDefaultInstance() { 526 | return DEFAULT_INSTANCE; 527 | } 528 | 529 | private static final com.google.protobuf.Parser 530 | PARSER = new com.google.protobuf.AbstractParser() { 531 | @java.lang.Override 532 | public request parsePartialFrom( 533 | com.google.protobuf.CodedInputStream input, 534 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 535 | throws com.google.protobuf.InvalidProtocolBufferException { 536 | Builder builder = newBuilder(); 537 | try { 538 | builder.mergeFrom(input, extensionRegistry); 539 | } catch (com.google.protobuf.InvalidProtocolBufferException e) { 540 | throw e.setUnfinishedMessage(builder.buildPartial()); 541 | } catch (com.google.protobuf.UninitializedMessageException e) { 542 | throw e.asInvalidProtocolBufferException().setUnfinishedMessage(builder.buildPartial()); 543 | } catch (java.io.IOException e) { 544 | throw new com.google.protobuf.InvalidProtocolBufferException(e) 545 | .setUnfinishedMessage(builder.buildPartial()); 546 | } 547 | return builder.buildPartial(); 548 | } 549 | }; 550 | 551 | public static com.google.protobuf.Parser parser() { 552 | return PARSER; 553 | } 554 | 555 | @java.lang.Override 556 | public com.google.protobuf.Parser getParserForType() { 557 | return PARSER; 558 | } 559 | 560 | @java.lang.Override 561 | public Demo.request getDefaultInstanceForType() { 562 | return DEFAULT_INSTANCE; 563 | } 564 | 565 | } 566 | 567 | public interface responseOrBuilder extends 568 | // @@protoc_insertion_point(interface_extends:qrpc.demo.response) 569 | com.google.protobuf.MessageOrBuilder { 570 | 571 | /** 572 | * string message = 1; 573 | * @return The message. 574 | */ 575 | java.lang.String getMessage(); 576 | /** 577 | * string message = 1; 578 | * @return The bytes for message. 579 | */ 580 | com.google.protobuf.ByteString 581 | getMessageBytes(); 582 | } 583 | /** 584 | * Protobuf type {@code qrpc.demo.response} 585 | */ 586 | public static final class response extends 587 | com.google.protobuf.GeneratedMessageV3 implements 588 | // @@protoc_insertion_point(message_implements:qrpc.demo.response) 589 | responseOrBuilder { 590 | private static final long serialVersionUID = 0L; 591 | // Use response.newBuilder() to construct. 592 | private response(com.google.protobuf.GeneratedMessageV3.Builder builder) { 593 | super(builder); 594 | } 595 | private response() { 596 | message_ = ""; 597 | } 598 | 599 | @java.lang.Override 600 | @SuppressWarnings({"unused"}) 601 | protected java.lang.Object newInstance( 602 | UnusedPrivateParameter unused) { 603 | return new response(); 604 | } 605 | 606 | public static final com.google.protobuf.Descriptors.Descriptor 607 | getDescriptor() { 608 | return Demo.internal_static_qrpc_demo_response_descriptor; 609 | } 610 | 611 | @java.lang.Override 612 | protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 613 | internalGetFieldAccessorTable() { 614 | return Demo.internal_static_qrpc_demo_response_fieldAccessorTable 615 | .ensureFieldAccessorsInitialized( 616 | Demo.response.class, Demo.response.Builder.class); 617 | } 618 | 619 | public static final int MESSAGE_FIELD_NUMBER = 1; 620 | @SuppressWarnings("serial") 621 | private volatile java.lang.Object message_ = ""; 622 | /** 623 | * string message = 1; 624 | * @return The message. 625 | */ 626 | @java.lang.Override 627 | public java.lang.String getMessage() { 628 | java.lang.Object ref = message_; 629 | if (ref instanceof java.lang.String) { 630 | return (java.lang.String) ref; 631 | } else { 632 | com.google.protobuf.ByteString bs = 633 | (com.google.protobuf.ByteString) ref; 634 | java.lang.String s = bs.toStringUtf8(); 635 | message_ = s; 636 | return s; 637 | } 638 | } 639 | /** 640 | * string message = 1; 641 | * @return The bytes for message. 642 | */ 643 | @java.lang.Override 644 | public com.google.protobuf.ByteString 645 | getMessageBytes() { 646 | java.lang.Object ref = message_; 647 | if (ref instanceof java.lang.String) { 648 | com.google.protobuf.ByteString b = 649 | com.google.protobuf.ByteString.copyFromUtf8( 650 | (java.lang.String) ref); 651 | message_ = b; 652 | return b; 653 | } else { 654 | return (com.google.protobuf.ByteString) ref; 655 | } 656 | } 657 | 658 | private byte memoizedIsInitialized = -1; 659 | @java.lang.Override 660 | public final boolean isInitialized() { 661 | byte isInitialized = memoizedIsInitialized; 662 | if (isInitialized == 1) return true; 663 | if (isInitialized == 0) return false; 664 | 665 | memoizedIsInitialized = 1; 666 | return true; 667 | } 668 | 669 | @java.lang.Override 670 | public void writeTo(com.google.protobuf.CodedOutputStream output) 671 | throws java.io.IOException { 672 | if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(message_)) { 673 | com.google.protobuf.GeneratedMessageV3.writeString(output, 1, message_); 674 | } 675 | getUnknownFields().writeTo(output); 676 | } 677 | 678 | @java.lang.Override 679 | public int getSerializedSize() { 680 | int size = memoizedSize; 681 | if (size != -1) return size; 682 | 683 | size = 0; 684 | if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(message_)) { 685 | size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, message_); 686 | } 687 | size += getUnknownFields().getSerializedSize(); 688 | memoizedSize = size; 689 | return size; 690 | } 691 | 692 | @java.lang.Override 693 | public boolean equals(final java.lang.Object obj) { 694 | if (obj == this) { 695 | return true; 696 | } 697 | if (!(obj instanceof Demo.response)) { 698 | return super.equals(obj); 699 | } 700 | Demo.response other = (Demo.response) obj; 701 | 702 | if (!getMessage() 703 | .equals(other.getMessage())) return false; 704 | if (!getUnknownFields().equals(other.getUnknownFields())) return false; 705 | return true; 706 | } 707 | 708 | @java.lang.Override 709 | public int hashCode() { 710 | if (memoizedHashCode != 0) { 711 | return memoizedHashCode; 712 | } 713 | int hash = 41; 714 | hash = (19 * hash) + getDescriptor().hashCode(); 715 | hash = (37 * hash) + MESSAGE_FIELD_NUMBER; 716 | hash = (53 * hash) + getMessage().hashCode(); 717 | hash = (29 * hash) + getUnknownFields().hashCode(); 718 | memoizedHashCode = hash; 719 | return hash; 720 | } 721 | 722 | public static Demo.response parseFrom( 723 | java.nio.ByteBuffer data) 724 | throws com.google.protobuf.InvalidProtocolBufferException { 725 | return PARSER.parseFrom(data); 726 | } 727 | public static Demo.response parseFrom( 728 | java.nio.ByteBuffer data, 729 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 730 | throws com.google.protobuf.InvalidProtocolBufferException { 731 | return PARSER.parseFrom(data, extensionRegistry); 732 | } 733 | public static Demo.response parseFrom( 734 | com.google.protobuf.ByteString data) 735 | throws com.google.protobuf.InvalidProtocolBufferException { 736 | return PARSER.parseFrom(data); 737 | } 738 | public static Demo.response parseFrom( 739 | com.google.protobuf.ByteString data, 740 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 741 | throws com.google.protobuf.InvalidProtocolBufferException { 742 | return PARSER.parseFrom(data, extensionRegistry); 743 | } 744 | public static Demo.response parseFrom(byte[] data) 745 | throws com.google.protobuf.InvalidProtocolBufferException { 746 | return PARSER.parseFrom(data); 747 | } 748 | public static Demo.response parseFrom( 749 | byte[] data, 750 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 751 | throws com.google.protobuf.InvalidProtocolBufferException { 752 | return PARSER.parseFrom(data, extensionRegistry); 753 | } 754 | public static Demo.response parseFrom(java.io.InputStream input) 755 | throws java.io.IOException { 756 | return com.google.protobuf.GeneratedMessageV3 757 | .parseWithIOException(PARSER, input); 758 | } 759 | public static Demo.response parseFrom( 760 | java.io.InputStream input, 761 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 762 | throws java.io.IOException { 763 | return com.google.protobuf.GeneratedMessageV3 764 | .parseWithIOException(PARSER, input, extensionRegistry); 765 | } 766 | 767 | public static Demo.response parseDelimitedFrom(java.io.InputStream input) 768 | throws java.io.IOException { 769 | return com.google.protobuf.GeneratedMessageV3 770 | .parseDelimitedWithIOException(PARSER, input); 771 | } 772 | 773 | public static Demo.response parseDelimitedFrom( 774 | java.io.InputStream input, 775 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 776 | throws java.io.IOException { 777 | return com.google.protobuf.GeneratedMessageV3 778 | .parseDelimitedWithIOException(PARSER, input, extensionRegistry); 779 | } 780 | public static Demo.response parseFrom( 781 | com.google.protobuf.CodedInputStream input) 782 | throws java.io.IOException { 783 | return com.google.protobuf.GeneratedMessageV3 784 | .parseWithIOException(PARSER, input); 785 | } 786 | public static Demo.response parseFrom( 787 | com.google.protobuf.CodedInputStream input, 788 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 789 | throws java.io.IOException { 790 | return com.google.protobuf.GeneratedMessageV3 791 | .parseWithIOException(PARSER, input, extensionRegistry); 792 | } 793 | 794 | @java.lang.Override 795 | public Builder newBuilderForType() { return newBuilder(); } 796 | public static Builder newBuilder() { 797 | return DEFAULT_INSTANCE.toBuilder(); 798 | } 799 | public static Builder newBuilder(Demo.response prototype) { 800 | return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); 801 | } 802 | @java.lang.Override 803 | public Builder toBuilder() { 804 | return this == DEFAULT_INSTANCE 805 | ? new Builder() : new Builder().mergeFrom(this); 806 | } 807 | 808 | @java.lang.Override 809 | protected Builder newBuilderForType( 810 | com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { 811 | Builder builder = new Builder(parent); 812 | return builder; 813 | } 814 | /** 815 | * Protobuf type {@code qrpc.demo.response} 816 | */ 817 | public static final class Builder extends 818 | com.google.protobuf.GeneratedMessageV3.Builder implements 819 | // @@protoc_insertion_point(builder_implements:qrpc.demo.response) 820 | Demo.responseOrBuilder { 821 | public static final com.google.protobuf.Descriptors.Descriptor 822 | getDescriptor() { 823 | return Demo.internal_static_qrpc_demo_response_descriptor; 824 | } 825 | 826 | @java.lang.Override 827 | protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 828 | internalGetFieldAccessorTable() { 829 | return Demo.internal_static_qrpc_demo_response_fieldAccessorTable 830 | .ensureFieldAccessorsInitialized( 831 | Demo.response.class, Demo.response.Builder.class); 832 | } 833 | 834 | // Construct using Demo.response.newBuilder() 835 | private Builder() { 836 | 837 | } 838 | 839 | private Builder( 840 | com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { 841 | super(parent); 842 | 843 | } 844 | @java.lang.Override 845 | public Builder clear() { 846 | super.clear(); 847 | bitField0_ = 0; 848 | message_ = ""; 849 | return this; 850 | } 851 | 852 | @java.lang.Override 853 | public com.google.protobuf.Descriptors.Descriptor 854 | getDescriptorForType() { 855 | return Demo.internal_static_qrpc_demo_response_descriptor; 856 | } 857 | 858 | @java.lang.Override 859 | public Demo.response getDefaultInstanceForType() { 860 | return Demo.response.getDefaultInstance(); 861 | } 862 | 863 | @java.lang.Override 864 | public Demo.response build() { 865 | Demo.response result = buildPartial(); 866 | if (!result.isInitialized()) { 867 | throw newUninitializedMessageException(result); 868 | } 869 | return result; 870 | } 871 | 872 | @java.lang.Override 873 | public Demo.response buildPartial() { 874 | Demo.response result = new Demo.response(this); 875 | if (bitField0_ != 0) { buildPartial0(result); } 876 | onBuilt(); 877 | return result; 878 | } 879 | 880 | private void buildPartial0(Demo.response result) { 881 | int from_bitField0_ = bitField0_; 882 | if (((from_bitField0_ & 0x00000001) != 0)) { 883 | result.message_ = message_; 884 | } 885 | } 886 | 887 | @java.lang.Override 888 | public Builder clone() { 889 | return super.clone(); 890 | } 891 | @java.lang.Override 892 | public Builder setField( 893 | com.google.protobuf.Descriptors.FieldDescriptor field, 894 | java.lang.Object value) { 895 | return super.setField(field, value); 896 | } 897 | @java.lang.Override 898 | public Builder clearField( 899 | com.google.protobuf.Descriptors.FieldDescriptor field) { 900 | return super.clearField(field); 901 | } 902 | @java.lang.Override 903 | public Builder clearOneof( 904 | com.google.protobuf.Descriptors.OneofDescriptor oneof) { 905 | return super.clearOneof(oneof); 906 | } 907 | @java.lang.Override 908 | public Builder setRepeatedField( 909 | com.google.protobuf.Descriptors.FieldDescriptor field, 910 | int index, java.lang.Object value) { 911 | return super.setRepeatedField(field, index, value); 912 | } 913 | @java.lang.Override 914 | public Builder addRepeatedField( 915 | com.google.protobuf.Descriptors.FieldDescriptor field, 916 | java.lang.Object value) { 917 | return super.addRepeatedField(field, value); 918 | } 919 | @java.lang.Override 920 | public Builder mergeFrom(com.google.protobuf.Message other) { 921 | if (other instanceof Demo.response) { 922 | return mergeFrom((Demo.response)other); 923 | } else { 924 | super.mergeFrom(other); 925 | return this; 926 | } 927 | } 928 | 929 | public Builder mergeFrom(Demo.response other) { 930 | if (other == Demo.response.getDefaultInstance()) return this; 931 | if (!other.getMessage().isEmpty()) { 932 | message_ = other.message_; 933 | bitField0_ |= 0x00000001; 934 | onChanged(); 935 | } 936 | this.mergeUnknownFields(other.getUnknownFields()); 937 | onChanged(); 938 | return this; 939 | } 940 | 941 | @java.lang.Override 942 | public final boolean isInitialized() { 943 | return true; 944 | } 945 | 946 | @java.lang.Override 947 | public Builder mergeFrom( 948 | com.google.protobuf.CodedInputStream input, 949 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 950 | throws java.io.IOException { 951 | if (extensionRegistry == null) { 952 | throw new java.lang.NullPointerException(); 953 | } 954 | try { 955 | boolean done = false; 956 | while (!done) { 957 | int tag = input.readTag(); 958 | switch (tag) { 959 | case 0: 960 | done = true; 961 | break; 962 | case 10: { 963 | message_ = input.readStringRequireUtf8(); 964 | bitField0_ |= 0x00000001; 965 | break; 966 | } // case 10 967 | default: { 968 | if (!super.parseUnknownField(input, extensionRegistry, tag)) { 969 | done = true; // was an endgroup tag 970 | } 971 | break; 972 | } // default: 973 | } // switch (tag) 974 | } // while (!done) 975 | } catch (com.google.protobuf.InvalidProtocolBufferException e) { 976 | throw e.unwrapIOException(); 977 | } finally { 978 | onChanged(); 979 | } // finally 980 | return this; 981 | } 982 | private int bitField0_; 983 | 984 | private java.lang.Object message_ = ""; 985 | /** 986 | * string message = 1; 987 | * @return The message. 988 | */ 989 | public java.lang.String getMessage() { 990 | java.lang.Object ref = message_; 991 | if (!(ref instanceof java.lang.String)) { 992 | com.google.protobuf.ByteString bs = 993 | (com.google.protobuf.ByteString) ref; 994 | java.lang.String s = bs.toStringUtf8(); 995 | message_ = s; 996 | return s; 997 | } else { 998 | return (java.lang.String) ref; 999 | } 1000 | } 1001 | /** 1002 | * string message = 1; 1003 | * @return The bytes for message. 1004 | */ 1005 | public com.google.protobuf.ByteString 1006 | getMessageBytes() { 1007 | java.lang.Object ref = message_; 1008 | if (ref instanceof String) { 1009 | com.google.protobuf.ByteString b = 1010 | com.google.protobuf.ByteString.copyFromUtf8( 1011 | (java.lang.String) ref); 1012 | message_ = b; 1013 | return b; 1014 | } else { 1015 | return (com.google.protobuf.ByteString) ref; 1016 | } 1017 | } 1018 | /** 1019 | * string message = 1; 1020 | * @param value The message to set. 1021 | * @return This builder for chaining. 1022 | */ 1023 | public Builder setMessage( 1024 | java.lang.String value) { 1025 | if (value == null) { throw new NullPointerException(); } 1026 | message_ = value; 1027 | bitField0_ |= 0x00000001; 1028 | onChanged(); 1029 | return this; 1030 | } 1031 | /** 1032 | * string message = 1; 1033 | * @return This builder for chaining. 1034 | */ 1035 | public Builder clearMessage() { 1036 | message_ = getDefaultInstance().getMessage(); 1037 | bitField0_ = (bitField0_ & ~0x00000001); 1038 | onChanged(); 1039 | return this; 1040 | } 1041 | /** 1042 | * string message = 1; 1043 | * @param value The bytes for message to set. 1044 | * @return This builder for chaining. 1045 | */ 1046 | public Builder setMessageBytes( 1047 | com.google.protobuf.ByteString value) { 1048 | if (value == null) { throw new NullPointerException(); } 1049 | checkByteStringIsUtf8(value); 1050 | message_ = value; 1051 | bitField0_ |= 0x00000001; 1052 | onChanged(); 1053 | return this; 1054 | } 1055 | @java.lang.Override 1056 | public final Builder setUnknownFields( 1057 | final com.google.protobuf.UnknownFieldSet unknownFields) { 1058 | return super.setUnknownFields(unknownFields); 1059 | } 1060 | 1061 | @java.lang.Override 1062 | public final Builder mergeUnknownFields( 1063 | final com.google.protobuf.UnknownFieldSet unknownFields) { 1064 | return super.mergeUnknownFields(unknownFields); 1065 | } 1066 | 1067 | 1068 | // @@protoc_insertion_point(builder_scope:qrpc.demo.response) 1069 | } 1070 | 1071 | // @@protoc_insertion_point(class_scope:qrpc.demo.response) 1072 | private static final Demo.response DEFAULT_INSTANCE; 1073 | static { 1074 | DEFAULT_INSTANCE = new Demo.response(); 1075 | } 1076 | 1077 | public static Demo.response getDefaultInstance() { 1078 | return DEFAULT_INSTANCE; 1079 | } 1080 | 1081 | private static final com.google.protobuf.Parser 1082 | PARSER = new com.google.protobuf.AbstractParser() { 1083 | @java.lang.Override 1084 | public response parsePartialFrom( 1085 | com.google.protobuf.CodedInputStream input, 1086 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 1087 | throws com.google.protobuf.InvalidProtocolBufferException { 1088 | Builder builder = newBuilder(); 1089 | try { 1090 | builder.mergeFrom(input, extensionRegistry); 1091 | } catch (com.google.protobuf.InvalidProtocolBufferException e) { 1092 | throw e.setUnfinishedMessage(builder.buildPartial()); 1093 | } catch (com.google.protobuf.UninitializedMessageException e) { 1094 | throw e.asInvalidProtocolBufferException().setUnfinishedMessage(builder.buildPartial()); 1095 | } catch (java.io.IOException e) { 1096 | throw new com.google.protobuf.InvalidProtocolBufferException(e) 1097 | .setUnfinishedMessage(builder.buildPartial()); 1098 | } 1099 | return builder.buildPartial(); 1100 | } 1101 | }; 1102 | 1103 | public static com.google.protobuf.Parser parser() { 1104 | return PARSER; 1105 | } 1106 | 1107 | @java.lang.Override 1108 | public com.google.protobuf.Parser getParserForType() { 1109 | return PARSER; 1110 | } 1111 | 1112 | @java.lang.Override 1113 | public Demo.response getDefaultInstanceForType() { 1114 | return DEFAULT_INSTANCE; 1115 | } 1116 | 1117 | } 1118 | 1119 | /** 1120 | * Protobuf service {@code qrpc.demo.DemoService} 1121 | */ 1122 | public static abstract class DemoService 1123 | implements com.google.protobuf.Service { 1124 | protected DemoService() {} 1125 | 1126 | public interface Interface { 1127 | /** 1128 | * rpc sendMessage(.qrpc.demo.request) returns (.qrpc.demo.response); 1129 | */ 1130 | public abstract void sendMessage( 1131 | com.google.protobuf.RpcController controller, 1132 | Demo.request request, 1133 | com.google.protobuf.RpcCallback done); 1134 | 1135 | } 1136 | 1137 | public static com.google.protobuf.Service newReflectiveService( 1138 | final Interface impl) { 1139 | return new DemoService() { 1140 | @java.lang.Override 1141 | public void sendMessage( 1142 | com.google.protobuf.RpcController controller, 1143 | Demo.request request, 1144 | com.google.protobuf.RpcCallback done) { 1145 | impl.sendMessage(controller, request, done); 1146 | } 1147 | 1148 | }; 1149 | } 1150 | 1151 | public static com.google.protobuf.BlockingService 1152 | newReflectiveBlockingService(final BlockingInterface impl) { 1153 | return new com.google.protobuf.BlockingService() { 1154 | public final com.google.protobuf.Descriptors.ServiceDescriptor 1155 | getDescriptorForType() { 1156 | return getDescriptor(); 1157 | } 1158 | 1159 | public final com.google.protobuf.Message callBlockingMethod( 1160 | com.google.protobuf.Descriptors.MethodDescriptor method, 1161 | com.google.protobuf.RpcController controller, 1162 | com.google.protobuf.Message request) 1163 | throws com.google.protobuf.ServiceException { 1164 | if (method.getService() != getDescriptor()) { 1165 | throw new java.lang.IllegalArgumentException( 1166 | "Service.callBlockingMethod() given method descriptor for " + 1167 | "wrong service type."); 1168 | } 1169 | switch(method.getIndex()) { 1170 | case 0: 1171 | return impl.sendMessage(controller, (Demo.request)request); 1172 | default: 1173 | throw new java.lang.AssertionError("Can't get here."); 1174 | } 1175 | } 1176 | 1177 | public final com.google.protobuf.Message 1178 | getRequestPrototype( 1179 | com.google.protobuf.Descriptors.MethodDescriptor method) { 1180 | if (method.getService() != getDescriptor()) { 1181 | throw new java.lang.IllegalArgumentException( 1182 | "Service.getRequestPrototype() given method " + 1183 | "descriptor for wrong service type."); 1184 | } 1185 | switch(method.getIndex()) { 1186 | case 0: 1187 | return Demo.request.getDefaultInstance(); 1188 | default: 1189 | throw new java.lang.AssertionError("Can't get here."); 1190 | } 1191 | } 1192 | 1193 | public final com.google.protobuf.Message 1194 | getResponsePrototype( 1195 | com.google.protobuf.Descriptors.MethodDescriptor method) { 1196 | if (method.getService() != getDescriptor()) { 1197 | throw new java.lang.IllegalArgumentException( 1198 | "Service.getResponsePrototype() given method " + 1199 | "descriptor for wrong service type."); 1200 | } 1201 | switch(method.getIndex()) { 1202 | case 0: 1203 | return Demo.response.getDefaultInstance(); 1204 | default: 1205 | throw new java.lang.AssertionError("Can't get here."); 1206 | } 1207 | } 1208 | 1209 | }; 1210 | } 1211 | 1212 | /** 1213 | * rpc sendMessage(.qrpc.demo.request) returns (.qrpc.demo.response); 1214 | */ 1215 | public abstract void sendMessage( 1216 | com.google.protobuf.RpcController controller, 1217 | Demo.request request, 1218 | com.google.protobuf.RpcCallback done); 1219 | 1220 | public static final 1221 | com.google.protobuf.Descriptors.ServiceDescriptor 1222 | getDescriptor() { 1223 | return Demo.getDescriptor().getServices().get(0); 1224 | } 1225 | public final com.google.protobuf.Descriptors.ServiceDescriptor 1226 | getDescriptorForType() { 1227 | return getDescriptor(); 1228 | } 1229 | 1230 | public final void callMethod( 1231 | com.google.protobuf.Descriptors.MethodDescriptor method, 1232 | com.google.protobuf.RpcController controller, 1233 | com.google.protobuf.Message request, 1234 | com.google.protobuf.RpcCallback< 1235 | com.google.protobuf.Message> done) { 1236 | if (method.getService() != getDescriptor()) { 1237 | throw new java.lang.IllegalArgumentException( 1238 | "Service.callMethod() given method descriptor for wrong " + 1239 | "service type."); 1240 | } 1241 | switch(method.getIndex()) { 1242 | case 0: 1243 | this.sendMessage(controller, (Demo.request)request, 1244 | com.google.protobuf.RpcUtil.specializeCallback( 1245 | done)); 1246 | return; 1247 | default: 1248 | throw new java.lang.AssertionError("Can't get here."); 1249 | } 1250 | } 1251 | 1252 | public final com.google.protobuf.Message 1253 | getRequestPrototype( 1254 | com.google.protobuf.Descriptors.MethodDescriptor method) { 1255 | if (method.getService() != getDescriptor()) { 1256 | throw new java.lang.IllegalArgumentException( 1257 | "Service.getRequestPrototype() given method " + 1258 | "descriptor for wrong service type."); 1259 | } 1260 | switch(method.getIndex()) { 1261 | case 0: 1262 | return Demo.request.getDefaultInstance(); 1263 | default: 1264 | throw new java.lang.AssertionError("Can't get here."); 1265 | } 1266 | } 1267 | 1268 | public final com.google.protobuf.Message 1269 | getResponsePrototype( 1270 | com.google.protobuf.Descriptors.MethodDescriptor method) { 1271 | if (method.getService() != getDescriptor()) { 1272 | throw new java.lang.IllegalArgumentException( 1273 | "Service.getResponsePrototype() given method " + 1274 | "descriptor for wrong service type."); 1275 | } 1276 | switch(method.getIndex()) { 1277 | case 0: 1278 | return Demo.response.getDefaultInstance(); 1279 | default: 1280 | throw new java.lang.AssertionError("Can't get here."); 1281 | } 1282 | } 1283 | 1284 | public static Stub newStub( 1285 | com.google.protobuf.RpcChannel channel) { 1286 | return new Stub(channel); 1287 | } 1288 | 1289 | public static final class Stub extends Demo.DemoService implements Interface { 1290 | private Stub(com.google.protobuf.RpcChannel channel) { 1291 | this.channel = channel; 1292 | } 1293 | 1294 | private final com.google.protobuf.RpcChannel channel; 1295 | 1296 | public com.google.protobuf.RpcChannel getChannel() { 1297 | return channel; 1298 | } 1299 | 1300 | public void sendMessage( 1301 | com.google.protobuf.RpcController controller, 1302 | Demo.request request, 1303 | com.google.protobuf.RpcCallback done) { 1304 | channel.callMethod( 1305 | getDescriptor().getMethods().get(0), 1306 | controller, 1307 | request, 1308 | Demo.response.getDefaultInstance(), 1309 | com.google.protobuf.RpcUtil.generalizeCallback( 1310 | done, 1311 | Demo.response.class, 1312 | Demo.response.getDefaultInstance())); 1313 | } 1314 | } 1315 | 1316 | public static BlockingInterface newBlockingStub( 1317 | com.google.protobuf.BlockingRpcChannel channel) { 1318 | return new BlockingStub(channel); 1319 | } 1320 | 1321 | public interface BlockingInterface { 1322 | public Demo.response sendMessage( 1323 | com.google.protobuf.RpcController controller, 1324 | Demo.request request) 1325 | throws com.google.protobuf.ServiceException; 1326 | } 1327 | 1328 | private static final class BlockingStub implements BlockingInterface { 1329 | private BlockingStub(com.google.protobuf.BlockingRpcChannel channel) { 1330 | this.channel = channel; 1331 | } 1332 | 1333 | private final com.google.protobuf.BlockingRpcChannel channel; 1334 | 1335 | public Demo.response sendMessage( 1336 | com.google.protobuf.RpcController controller, 1337 | Demo.request request) 1338 | throws com.google.protobuf.ServiceException { 1339 | return (Demo.response) channel.callBlockingMethod( 1340 | getDescriptor().getMethods().get(0), 1341 | controller, 1342 | request, 1343 | Demo.response.getDefaultInstance()); 1344 | } 1345 | 1346 | } 1347 | 1348 | // @@protoc_insertion_point(class_scope:qrpc.demo.DemoService) 1349 | } 1350 | 1351 | private static final com.google.protobuf.Descriptors.Descriptor 1352 | internal_static_qrpc_demo_request_descriptor; 1353 | private static final 1354 | com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 1355 | internal_static_qrpc_demo_request_fieldAccessorTable; 1356 | private static final com.google.protobuf.Descriptors.Descriptor 1357 | internal_static_qrpc_demo_response_descriptor; 1358 | private static final 1359 | com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 1360 | internal_static_qrpc_demo_response_fieldAccessorTable; 1361 | 1362 | public static com.google.protobuf.Descriptors.FileDescriptor 1363 | getDescriptor() { 1364 | return descriptor; 1365 | } 1366 | private static com.google.protobuf.Descriptors.FileDescriptor 1367 | descriptor; 1368 | static { 1369 | java.lang.String[] descriptorData = { 1370 | "\n\ndemo.proto\022\tqrpc.demo\"\032\n\007request\022\017\n\007me" + 1371 | "ssage\030\001 \001(\t\"\033\n\010response\022\017\n\007message\030\001 \001(\t" + 1372 | "2G\n\013DemoService\0228\n\013sendMessage\022\022.qrpc.de" + 1373 | "mo.request\032\023.qrpc.demo.response\"\000B\032\n\rcom" + 1374 | ".qrpc.demoB\004DemoP\000\210\001\001b\006proto3" 1375 | }; 1376 | descriptor = com.google.protobuf.Descriptors.FileDescriptor 1377 | .internalBuildGeneratedFileFrom(descriptorData, 1378 | new com.google.protobuf.Descriptors.FileDescriptor[] { 1379 | }); 1380 | internal_static_qrpc_demo_request_descriptor = 1381 | getDescriptor().getMessageTypes().get(0); 1382 | internal_static_qrpc_demo_request_fieldAccessorTable = new 1383 | com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( 1384 | internal_static_qrpc_demo_request_descriptor, 1385 | new java.lang.String[] { "Message", }); 1386 | internal_static_qrpc_demo_response_descriptor = 1387 | getDescriptor().getMessageTypes().get(1); 1388 | internal_static_qrpc_demo_response_fieldAccessorTable = new 1389 | com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( 1390 | internal_static_qrpc_demo_response_descriptor, 1391 | new java.lang.String[] { "Message", }); 1392 | } 1393 | 1394 | // @@protoc_insertion_point(outer_class_scope) 1395 | } 1396 | -------------------------------------------------------------------------------- /qrpc-demo/src/main/java/DemoServiceImpl.java: -------------------------------------------------------------------------------- 1 | import com.google.protobuf.RpcCallback; 2 | import com.google.protobuf.RpcController; 3 | 4 | /** 5 | * @author Yang Lianhuan 6 | * @version 1.0.0 7 | * @since 2023/7/9 8 | **/ 9 | public class DemoServiceImpl extends Demo.DemoService{ 10 | @Override 11 | public void sendMessage(RpcController controller, Demo.request request, RpcCallback done) { 12 | System.out.println("received from client, message: " + request.getMessage()); 13 | Demo.response response = Demo.response.newBuilder().setMessage("hi client").build(); 14 | done.run(response); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /qrpc-demo/src/main/java/Server.java: -------------------------------------------------------------------------------- 1 | import org.qrpc.server.ServerBuilder; 2 | 3 | /** 4 | * @author Yang Lianhuan 5 | * @version 1.0.0 6 | * @since 2023/7/9 7 | **/ 8 | public class Server { 9 | public static void main(String[] args) { 10 | org.qrpc.net.Server server = ServerBuilder.forPort(8888) 11 | .addService(new DemoServiceImpl()) 12 | .build(); 13 | server.start(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /qrpc-demo/src/main/resources/demo.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | option java_multiple_files = false; 4 | option java_package = "com.qrpc.demo"; 5 | option java_outer_classname = "Demo"; 6 | option java_generic_services = true; 7 | 8 | package qrpc.demo; 9 | 10 | message request { 11 | string message = 1; 12 | } 13 | 14 | message response { 15 | string message = 1; 16 | } 17 | 18 | service DemoService { 19 | rpc sendMessage(request) returns (response) {}; 20 | } -------------------------------------------------------------------------------- /qrpc-project.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | --------------------------------------------------------------------------------