aerosocket_client/
client.rs1use aerosocket_core::{Message, Result};
6use std::net::SocketAddr;
7
8#[derive(Debug)]
10pub struct Client {
11 addr: SocketAddr,
13 config: ClientConfig,
15}
16
17impl Client {
18 pub fn new(addr: SocketAddr) -> Self {
20 Self {
21 addr,
22 config: ClientConfig::default(),
23 }
24 }
25
26 pub fn with_config(mut self, config: ClientConfig) -> Self {
28 self.config = config;
29 self
30 }
31
32 pub async fn connect(self) -> Result<ClientConnection> {
34 Ok(ClientConnection::new(self.addr))
36 }
37}
38
39#[derive(Debug)]
41pub struct ClientConnection {
42 #[allow(dead_code)]
44 remote_addr: SocketAddr,
45 connected: bool,
47}
48
49impl ClientConnection {
50 pub fn new(addr: SocketAddr) -> Self {
52 Self {
53 remote_addr: addr,
54 connected: false,
55 }
56 }
57
58 pub async fn send(&mut self, _message: Message) -> Result<()> {
60 Ok(())
62 }
63
64 pub async fn next(&mut self) -> Result<Option<Message>> {
66 Ok(None)
68 }
69
70 pub async fn close(&mut self, _code: Option<u16>, _reason: Option<&str>) -> Result<()> {
72 self.connected = false;
74 Ok(())
75 }
76
77 pub fn is_connected(&self) -> bool {
79 self.connected
80 }
81}
82
83#[derive(Debug, Clone)]
85pub struct ClientConfig {
86 pub max_frame_size: usize,
88 pub max_message_size: usize,
90 pub handshake_timeout: std::time::Duration,
92 pub compression_enabled: bool,
94}
95
96impl Default for ClientConfig {
97 fn default() -> Self {
98 Self {
99 max_frame_size: 1024 * 1024, max_message_size: 16 * 1024 * 1024, handshake_timeout: std::time::Duration::from_secs(30),
102 compression_enabled: false,
103 }
104 }
105}
106
107#[derive(Debug)]
109pub struct ClientBuilder {
110 addr: SocketAddr,
111 config: ClientConfig,
112}
113
114impl ClientBuilder {
115 pub fn new(addr: SocketAddr) -> Self {
117 Self {
118 addr,
119 config: ClientConfig::default(),
120 }
121 }
122
123 pub fn max_frame_size(mut self, size: usize) -> Self {
125 self.config.max_frame_size = size;
126 self
127 }
128
129 pub fn max_message_size(mut self, size: usize) -> Self {
131 self.config.max_message_size = size;
132 self
133 }
134
135 pub fn handshake_timeout(mut self, timeout: std::time::Duration) -> Self {
137 self.config.handshake_timeout = timeout;
138 self
139 }
140
141 pub fn compression(mut self, enabled: bool) -> Self {
143 self.config.compression_enabled = enabled;
144 self
145 }
146
147 pub fn build(self) -> Client {
149 Client::new(self.addr).with_config(self.config)
150 }
151}
152
153#[cfg(test)]
154mod tests {
155 use super::*;
156
157 #[test]
158 fn test_client_creation() {
159 let addr = "127.0.0.1:8080".parse().unwrap();
160 let client = Client::new(addr);
161 assert_eq!(client.addr, addr);
162 }
163
164 #[test]
165 fn test_client_config() {
166 let config = ClientConfig::default();
167 assert_eq!(config.max_frame_size, 1024 * 1024);
168 assert_eq!(config.max_message_size, 16 * 1024 * 1024);
169 }
170
171 #[test]
172 fn test_client_builder() {
173 let addr = "127.0.0.1:8080".parse().unwrap();
174 let client = ClientBuilder::new(addr)
175 .max_frame_size(2048)
176 .compression(true)
177 .build();
178
179 assert_eq!(client.addr, addr);
180 assert_eq!(client.config.max_frame_size, 2048);
181 assert!(client.config.compression_enabled);
182 }
183}