Skip to content

Commit d85ace6

Browse files
committed
Added support of snd/rcv buf options for socket
1 parent 5e72115 commit d85ace6

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

booster/booster/aio/basic_socket.h

+38
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ namespace aio {
8282
keep_alive, //< SO_KEEPALIVE socket option
8383
reuse_address //< SO_REUSEADDR socket option
8484
} boolean_option_type;
85+
8586

8687
///
8788
/// Get a value for a boolean_option_type
@@ -106,6 +107,43 @@ namespace aio {
106107
/// Throws system::system_error if error occurs.
107108
///
108109
void set_option(boolean_option_type opt,bool v);
110+
111+
///
112+
/// Integer socket options list
113+
///
114+
/// \ver{v1_2}
115+
typedef enum {
116+
receive_buffer_size, //< SO_RCVBUF options
117+
send_buffer_size, //< SO_SNDBUF option
118+
} integer_option_type;
119+
///
120+
/// Get a value for a integer_option_type
121+
///
122+
/// If a error occurs it is assigned to \a e.
123+
///
124+
/// \ver{v1_2}
125+
int get_option(integer_option_type opt,system::error_code &e);
126+
///
127+
/// Get a value for a integer_option_type
128+
/// Throws system::system_error if error occurs.
129+
///
130+
/// \ver{v1_2}
131+
int get_option(integer_option_type opt);
132+
///
133+
/// Set a value for a integer_option_type
134+
///
135+
/// If a error occurs it is assigned to \a e.
136+
///
137+
/// \ver{v1_2}
138+
void set_option(integer_option_type opt,int v,system::error_code &e);
139+
///
140+
/// Set a value for a integer_option_type
141+
///
142+
/// Throws system::system_error if error occurs.
143+
///
144+
/// \ver{v1_2}
145+
void set_option(integer_option_type opt,int v);
146+
109147
///
110148
/// Bind the opended socket the \ref endpoint \a ep
111149
///

booster/lib/aio/src/basic_socket.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,71 @@ endpoint basic_socket::remote_endpoint()
105105
}
106106

107107

108+
109+
void basic_socket::set_option(integer_option_type opt,int v,system::error_code &e)
110+
{
111+
int value = v;
112+
char const *p=reinterpret_cast<char const *>(&value);
113+
int res = 0;
114+
switch(opt) {
115+
case receive_buffer_size:
116+
res=::setsockopt(native(),SOL_SOCKET,SO_RCVBUF,p,sizeof(value));
117+
break;
118+
case send_buffer_size:
119+
res=::setsockopt(native(),SOL_SOCKET,SO_SNDBUF,p,sizeof(value));
120+
break;
121+
default:
122+
;
123+
}
124+
if(res < 0)
125+
e=geterror();
126+
}
127+
128+
void basic_socket::set_option(integer_option_type opt,int v)
129+
{
130+
system::error_code e;
131+
set_option(opt,v,e);
132+
if(e) throw system::system_error(e);
133+
}
134+
135+
int basic_socket::get_option(integer_option_type opt,system::error_code &e)
136+
{
137+
int value = 0;
138+
socklen_t len = sizeof(value);
139+
#ifdef BOOSTER_WIN32
140+
char *ptr = reinterpret_cast<char *>(&value);
141+
#else
142+
int *ptr = &value;
143+
#endif
144+
int res = 0;
145+
switch(opt) {
146+
case receive_buffer_size:
147+
res=::getsockopt(native(),SOL_SOCKET,SO_RCVBUF,ptr,&len);
148+
break;
149+
case send_buffer_size:
150+
res=::getsockopt(native(),SOL_SOCKET,SO_SNDBUF,ptr,&len);
151+
break;
152+
default:
153+
;
154+
}
155+
if(res < 0) {
156+
e=geterror();
157+
return false;
158+
}
159+
return value;
160+
}
161+
162+
int basic_socket::get_option(integer_option_type opt)
163+
{
164+
system::error_code e;
165+
int res = get_option(opt,e);
166+
if(e) throw system::system_error(e);
167+
return res;
168+
}
169+
170+
171+
172+
108173
void basic_socket::set_option(boolean_option_type opt,bool v,system::error_code &e)
109174
{
110175
int value = v ? 1 : 0;

0 commit comments

Comments
 (0)