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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::{BrpcError, BrpcResult};
use brpc_sys::ffi::{self, BrpcServer, BrpcServerOptions};
use libc::{c_int, c_void};
#[repr(C)]
pub enum ServiceOwnership {
ServerOwnsService = 0,
ServerDoesntOwnService = 1,
}
#[doc(hidden)]
pub trait Service {
fn get_service_ptr(&self) -> *mut c_void;
}
pub struct Server {
inner: *mut BrpcServer,
}
impl Server {
pub fn new() -> Self {
Server {
inner: unsafe { ffi::brpc_server_new() },
}
}
pub fn add_service<T: Service + Sized>(
&mut self,
service: &T,
ownership: ServiceOwnership,
) -> BrpcResult<()> {
let ret = unsafe {
ffi::brpc_server_add_service(self.inner, service.get_service_ptr(), ownership as c_int)
};
if ret == 0 {
Ok(())
} else {
Err(BrpcError::EINTERNAL)
}
}
pub fn start(&mut self, port: u16, opt: &ServerOptions) -> BrpcResult<()> {
let ret = unsafe { ffi::brpc_server_start(self.inner, i32::from(port), opt.inner) };
if ret == 0 {
Ok(())
} else {
Err(BrpcError::EINTERNAL)
}
}
pub fn run(&mut self) {
unsafe { ffi::brpc_server_run_until_asked_to_quit(self.inner) };
}
}
impl Drop for Server {
fn drop(&mut self) {
unsafe {
ffi::brpc_server_destroy(self.inner);
}
}
}
impl Default for Server {
fn default() -> Self {
Self::new()
}
}
pub struct ServerOptions {
#[doc(hidden)]
pub(crate) inner: *mut BrpcServerOptions,
}
impl ServerOptions {
pub fn new() -> Self {
ServerOptions {
inner: unsafe { ffi::brpc_server_options_new() },
}
}
pub fn set_idle_timeout_ms(&mut self, timeout: i32) {
unsafe { ffi::brpc_server_options_set_idle_timeout_ms(self.inner, timeout as c_int) }
}
}
impl Drop for ServerOptions {
fn drop(&mut self) {
unsafe {
ffi::brpc_server_options_destroy(self.inner);
}
}
}
impl Default for ServerOptions {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::ptr;
struct NullService {}
impl Service for NullService {
fn get_service_ptr(&self) -> *mut c_void {
ptr::null_mut()
}
}
#[test]
fn server_options_new() {
let opt = ServerOptions::new();
assert_ne!(opt.inner, ptr::null_mut());
}
#[test]
fn server_options_set_idle_timeout_ms() {
let mut opt = ServerOptions::new();
opt.set_idle_timeout_ms(0);
}
#[test]
fn server_new() {
let server = Server::new();
assert_ne!(server.inner, ptr::null_mut());
}
#[test]
fn server_add_null_service() {
let service = NullService {};
let mut server = Server::new();
let ret = server.add_service(&service, ServiceOwnership::ServerDoesntOwnService);
assert_eq!(false, ret.is_ok());
}
#[test]
fn server_start_null_service() {
let service = NullService {};
let mut server = Server::new();
let _ = server.add_service(&service, ServiceOwnership::ServerDoesntOwnService);
let opt = ServerOptions::new();
let ret = server.start(50000, &opt);
assert_eq!(true, ret.is_ok());
}
}