rust_rsm/rsm/socket/
tcpsocket.rs1#![allow(non_camel_case_types)]
2#![allow(non_snake_case)]
3#![allow(non_upper_case_globals)]
4#![allow(dead_code)]
5
6use crate::common::errcode;
7use std::net::{SocketAddr};
8use crate::net_ext::{RawFdType,self};
9use super::*;
10use super::{socketpool};
11
12impl TcpSocket {
15 pub fn new(local_addr:&SocketAddr)->Result<Self,errcode::RESULT> {
16 let af=if local_addr.is_ipv4() {SOCKET_ADDRESS_FAMILY::SOCKET_INET}
17 else { SOCKET_ADDRESS_FAMILY::SOCKET_INET6 };
18 let sock_id = match socketpool::new_socket(af, SOCKET_TYPE::PROTO_STREAM, net_ext::IpProtos::Ip_Proto_TCP) {
19 Ok(s)=>s,
20 Err(e)=>return Err(e),
21 };
22 let sock = match socketpool::get_socket_by_idx(sock_id) {
23 None=>return Err(errcode::ERROR_INIT_FAILED),
24 Some(s)=>s,
25 };
26 sock.set_reuse_addr(true);
27 let ret = sock.bind(local_addr);
28 if ret!=errcode::RESULT_SUCCESS {
29 return Err(ret)
30 }
31
32 let udp = Self {
33 sck_idx:sock_id,
34 };
35
36 return Ok(udp)
37 }
38
39 pub fn get_socket_by_id(id:i32)->Self {
40 return Self { sck_idx: id }
41 }
42
43 pub fn set_send_buf_size(&mut self,buf_size:usize)->errcode::RESULT {
44 let sock = match socketpool::get_socket_by_idx(self.sck_idx) {
45 None=>return errcode::ERROR_INVALID_INDEX,
46 Some(s)=>s,
47 };
48 return sock.set_send_buffer(buf_size)
49 }
50
51 pub fn set_recv_buf_size(&mut self,buf_size:usize)->errcode::RESULT {
52 let sock = match socketpool::get_socket_by_idx(self.sck_idx) {
53 None=>return errcode::ERROR_INVALID_INDEX,
54 Some(s)=>s,
55 };
56 return sock.set_recv_buffer(buf_size)
57 }
58
59 pub fn connect(&mut self,dst:&SocketAddr)->errcode::RESULT {
60 let sock = match socketpool::get_socket_by_idx(self.sck_idx) {
61 None=>return errcode::ERROR_INVALID_INDEX,
62 Some(s)=>s,
63 };
64
65 return sock.connect(dst)
66 }
67
68 pub fn send(&mut self,buf:&[u8])->Result<usize,errcode::RESULT> {
69 let sock = match socketpool::get_socket_by_idx(self.sck_idx) {
70 None=>return Err(errcode::ERROR_INVALID_INDEX),
71 Some(s)=>s,
72 };
73
74 return sock.send(buf)
75 }
76
77 pub fn recv(&mut self,buf:&mut [u8])->Result<usize,errcode::RESULT> {
78 let sock = match socketpool::get_socket_by_idx(self.sck_idx) {
79 None=>return Err(errcode::ERROR_INVALID_INDEX),
80 Some(s)=>s,
81 };
82
83 return sock.recv(buf)
84 }
85
86 pub fn get_socket_id(&self)->i32 {
87 return self.sck_idx
88 }
89
90 pub fn get_os_socket(&self)->RawFdType {
92 let sock = match socketpool::get_socket_by_idx(self.sck_idx) {
93 None=>return 0,
94 Some(s)=>s,
95 };
96
97 return sock.get_raw_fd()
98 }
99
100 pub fn get_local_addr(&self)->Option<SocketAddr> {
101 let sock = match socketpool::get_socket_by_idx(self.sck_idx) {
102 None=>return None,
103 Some(s)=>s,
104 };
105
106 return Some(sock.get_local_addr())
107 }
108
109 pub fn get_peer_addr(&self)->Option<SocketAddr> {
110 let sock = match socketpool::get_socket_by_idx(self.sck_idx) {
111 None=>return None,
112 Some(s)=>s,
113 };
114
115 return Some(sock.get_peer_addr())
116 }
117
118 pub fn close(&mut self)->errcode::RESULT {
119 socketpool::close_socket(self.sck_idx)
120 }
121
122}