rust_rsm/rsm/socket/
socket.rs

1use crate::common::errcode;
2use crate::net_ext;
3use std::net::{IpAddr,SocketAddr};
4use super::*;
5#[cfg(windows)]
6use crate::net_ext::windows::rawsocket;
7
8#[cfg(unix)]
9use crate::net_ext::unix::rawsocket;
10
11///Inner Socket ecapsulation implementation, other socket must derive from this implementation
12/// Each socket is associated with a socket index, which is maintain outside, e.g. a socket pool instance
13///
14impl Socket {
15    pub fn new_socket(sock_idx:i32,sock_af:SOCKET_ADDRESS_FAMILY,sock_type:SOCKET_TYPE,proto:u8)->Result<Self,errcode::RESULT> {
16         let sock = match socket(sock_af.clone(), sock_type.clone(), proto) {
17            Ok(s)=>s,
18            Err(e)=>return Err(e),
19        };
20
21        let mut sock = Socket {
22            sock_id:sock_idx,
23            os_fd:sock,
24            sock_af:sock_af,
25            sock_type:sock_type,
26            proto:proto,
27            state:SOCKET_STATE::SOCK_INIT,
28            tcp_server:false,
29            lb_policy:SOCKET_LB_POLICY::SOCK_LB_ALL_INSTANCE,
30            local_addr:SocketAddr::new(IpAddr::from([0,0,0,0]), 0),
31            peer_addr:SocketAddr::new(IpAddr::from([0,0,0,0]), 0),
32        };
33        sock.set_non_blocking();
34
35        return Ok(sock)
36    }
37    pub fn set_reuse_addr(&mut self,is_reuse:bool)->errcode::RESULT {
38        let reuse= if is_reuse { 1 } else {0};
39        rawsocket::set_socket_reuse_addr(self.get_raw_fd(), reuse)
40    }
41
42    pub fn set_reuse_port(&mut self,is_reuse:bool)->errcode::RESULT {
43        let reuse= if is_reuse { 1 } else {0};
44        rawsocket::set_socket_reuse_port(self.get_raw_fd(), reuse)
45    }
46    
47    pub fn bind(&mut self,addr:&SocketAddr)->errcode::RESULT {
48        if self.state!=SOCKET_STATE::SOCK_INIT {
49            return errcode::ERROR_INVALID_STATE
50        }
51        let ret = rawsocket::bind(self.get_raw_fd(), addr);
52        if ret == errcode::RESULT_SUCCESS {
53            self.state=SOCKET_STATE::SOCK_BIND
54        }
55        return ret
56    }
57
58    pub fn listen(&mut self,back_log:i32)->errcode::RESULT{
59        if self.state!=SOCKET_STATE::SOCK_BIND {
60            return errcode::ERROR_INVALID_STATE
61        }
62        let ret = rawsocket::listen(self.get_raw_fd(), back_log);
63        if ret == errcode::RESULT_SUCCESS {
64            self.tcp_server = true;
65            self.state=SOCKET_STATE::SOCK_LISTENING
66        } else {
67            println!("listen socket failed,local={},ret={},os_err={}",self.local_addr,ret,std::io::Error::last_os_error());
68        }         
69
70        return ret
71    }
72
73    pub fn accept(&mut self,new_idx:i32)->Result<Self,errcode::RESULT> {
74        if self.state!=SOCKET_STATE::SOCK_LISTENING {
75            return Err(errcode::ERROR_INVALID_STATE)
76        }
77        let (fd,peer) = match rawsocket::accept(self.get_raw_fd()) {
78            Ok((f,p))=>(f,p),
79            Err(e)=>return Err(e),
80        };
81        let mut new_sock = self.clone();
82        new_sock.os_fd=fd;
83        new_sock.sock_id=new_idx;
84        new_sock.tcp_server=false;
85        new_sock.peer_addr = peer;
86        new_sock.state=SOCKET_STATE::SOCK_CONNECTED;
87        new_sock.set_non_blocking();
88       Ok(new_sock)
89    }
90
91    pub fn connect(&mut self,dst:&SocketAddr)->errcode::RESULT {
92        let state = self.state;
93        self.state=SOCKET_STATE::SOCK_CONNECTING;
94        let ret=rawsocket::connect(self.os_fd, dst);
95        if ret==errcode::RESULT_SUCCESS {
96            self.state=SOCKET_STATE::SOCK_CONNECTED;
97        } else {
98            self.state = state;
99        }
100        return ret;
101    }
102    pub fn send(&mut self,buf:&[u8])->Result<usize,errcode::RESULT> {
103        return rawsocket::write_fd(self.os_fd, buf, 0);
104    }
105
106    pub fn send_to(&mut self,dst:&SocketAddr,buf:&[u8])->Result<usize,errcode::RESULT> {
107        return rawsocket::send_to(self.os_fd, buf, 0, dst)
108    }
109
110    pub fn recv(&mut self,buf:&mut [u8])->Result<usize,errcode::RESULT> {
111
112       return rawsocket::read_fd(self.os_fd, buf, 0)
113    }
114
115    pub fn recv_from(&mut self,buf:&mut [u8])->Result<(usize,SocketAddr),errcode::RESULT> {
116        let (len,peer) = match rawsocket::recv_from(self.os_fd, buf) {
117            Ok((l,a))=>(l,a),
118            Err(_)=>return Err(errcode::ERROR_RECV_MSG),
119        };
120
121        return Ok((len,peer))
122    }
123
124    pub fn get_raw_fd(&self)->net_ext::RawFdType {
125        self.os_fd
126    }
127
128    pub fn set_send_buffer(&mut self,size:usize)->errcode::RESULT {
129        return rawsocket::set_socket_sendbuf(self.os_fd, size as i32)        
130    }
131
132    pub fn set_recv_buffer(&mut self,size:usize)->errcode::RESULT {
133        return rawsocket::set_socket_recvbuf(self.os_fd, size as i32)
134    }
135
136    pub fn set_non_blocking(&mut self)->errcode::RESULT {
137        if rawsocket::set_non_blocking(self.os_fd).is_ok() {
138            return  errcode::RESULT_SUCCESS
139        }
140
141        return errcode::ERROR_OS_CALL_FAILED
142       
143    }
144
145    pub fn get_socket_id(&self)->i32 {
146        self.sock_id
147    }
148
149    pub(crate) fn get_sock_state(&self)->SOCKET_STATE {
150        self.state
151    }
152
153    pub fn is_tcp_server(&self)->bool {
154        return self.tcp_server
155    }
156
157    pub fn set_lb_policy(&mut self,policy:SOCKET_LB_POLICY)->errcode::RESULT {
158        if !self.is_tcp_server() {
159            return errcode::ERROR_INVALID_STATE
160        }
161        self.lb_policy = policy;
162        errcode::RESULT_SUCCESS
163    }
164
165    pub fn get_lb_policy(&self)->Result<SOCKET_LB_POLICY,errcode::RESULT> {
166        if self.is_tcp_server() {
167            return Ok(self.lb_policy)
168        }
169        Err(errcode::ERROR_INVALID_STATE)
170    }
171
172    pub fn get_local_addr(&self)->SocketAddr {
173        self.local_addr
174    }
175
176    pub fn get_peer_addr(&self)->SocketAddr {
177        self.peer_addr
178    }
179
180    pub fn get_sock_type(&self)->SOCKET_TYPE {
181        self.sock_type
182    }
183
184}
185
186//close the Socket on drop the memory
187impl Drop for Socket {
188    fn drop(&mut self) {
189        rawsocket::close_fd(self.os_fd)
190    }
191}