Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
FORMUS3IC_LAS3
/
embb
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
458054e7
authored
Nov 10, 2014
by
Marcus Winter
Committed by
unknown
Dec 11, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mtapi_network_c: switched from nonblocking sockets to select
parent
010e2be0
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
14 deletions
+55
-14
mtapi_network_c/src/embb_mtapi_network_socket.c
+39
-14
mtapi_network_c/src/embb_mtapi_network_socket.h
+5
-0
mtapi_network_c/test/embb_mtapi_network_test_socket.cc
+11
-0
No files found.
mtapi_network_c/src/embb_mtapi_network_socket.c
View file @
458054e7
...
@@ -22,16 +22,6 @@ void embb_mtapi_network_socket_finalize(
...
@@ -22,16 +22,6 @@ void embb_mtapi_network_socket_finalize(
}
}
}
}
int
embb_mtapi_network_socket_setnonblock
(
embb_mtapi_network_socket_t
*
that
)
{
u_long
mode
=
1
;
if
(
SOCKET_ERROR
==
ioctlsocket
(
that
->
handle
,
FIONBIO
,
&
mode
))
{
return
0
;
}
else
{
return
1
;
}
}
int
embb_mtapi_network_socket_bind_and_listen
(
int
embb_mtapi_network_socket_bind_and_listen
(
embb_mtapi_network_socket_t
*
that
,
embb_mtapi_network_socket_t
*
that
,
char
const
*
host
,
char
const
*
host
,
...
@@ -61,8 +51,7 @@ int embb_mtapi_network_socket_bind_and_listen(
...
@@ -61,8 +51,7 @@ int embb_mtapi_network_socket_bind_and_listen(
return
0
;
return
0
;
}
}
// set non-block
return
1
;
return
embb_mtapi_network_socket_setnonblock
(
that
);
}
}
int
embb_mtapi_network_socket_accept
(
int
embb_mtapi_network_socket_accept
(
...
@@ -96,6 +85,41 @@ int embb_mtapi_network_socket_connect(
...
@@ -96,6 +85,41 @@ int embb_mtapi_network_socket_connect(
return
1
;
return
1
;
}
}
int
embb_mtapi_network_socket_select
(
embb_mtapi_network_socket_t
*
that
,
int
timeout
)
{
fd_set
read_set
;
int
max_fd
;
int
err
;
struct
timeval
tv
;
tv
.
tv_sec
=
timeout
/
1000
;
tv
.
tv_usec
=
timeout
%
1000
;
FD_ZERO
(
&
read_set
);
FD_SET
(
that
->
handle
,
&
read_set
);
max_fd
=
that
->
handle
;
if
(
timeout
>=
0
)
{
err
=
select
(
max_fd
,
&
read_set
,
NULL
,
NULL
,
&
tv
);
}
else
{
err
=
select
(
max_fd
,
&
read_set
,
NULL
,
NULL
,
NULL
);
}
if
(
0
==
err
)
{
// timeout
return
0
;
}
if
(
SOCKET_ERROR
==
err
)
{
return
0
;
}
if
(
FD_ISSET
(
that
->
handle
,
&
read_set
))
{
return
1
;
}
return
0
;
}
int
embb_mtapi_network_socket_sendbuffer
(
int
embb_mtapi_network_socket_sendbuffer
(
embb_mtapi_network_socket_t
*
that
,
embb_mtapi_network_socket_t
*
that
,
embb_mtapi_network_buffer_t
*
buffer
)
{
embb_mtapi_network_buffer_t
*
buffer
)
{
...
@@ -111,12 +135,13 @@ int embb_mtapi_network_socket_recvbuffer(
...
@@ -111,12 +135,13 @@ int embb_mtapi_network_socket_recvbuffer(
embb_mtapi_network_socket_t
*
that
,
embb_mtapi_network_socket_t
*
that
,
embb_mtapi_network_buffer_t
*
buffer
)
{
embb_mtapi_network_buffer_t
*
buffer
)
{
u_long
bytes_available
=
0
;
u_long
bytes_available
=
0
;
int
err
;
if
(
0
!=
ioctlsocket
(
that
->
handle
,
FIONREAD
,
&
bytes_available
))
if
(
0
!=
ioctlsocket
(
that
->
handle
,
FIONREAD
,
&
bytes_available
))
return
0
;
return
0
;
if
(
buffer
->
capacity
>
(
int
)
bytes_available
)
if
(
buffer
->
capacity
>
(
int
)
bytes_available
)
return
0
;
return
0
;
int
result
=
recv
(
that
->
handle
,
buffer
->
data
,
buffer
->
capacity
,
0
);
err
=
recv
(
that
->
handle
,
buffer
->
data
,
buffer
->
capacity
,
0
);
if
(
result
!=
buffer
->
capacity
)
if
(
err
!=
buffer
->
capacity
)
return
0
;
return
0
;
buffer
->
size
=
buffer
->
capacity
;
buffer
->
size
=
buffer
->
capacity
;
return
buffer
->
size
;
return
buffer
->
size
;
...
...
mtapi_network_c/src/embb_mtapi_network_socket.h
View file @
458054e7
...
@@ -66,6 +66,11 @@ int embb_mtapi_network_socket_connect(
...
@@ -66,6 +66,11 @@ int embb_mtapi_network_socket_connect(
uint16_t
port
uint16_t
port
);
);
int
embb_mtapi_network_socket_select
(
embb_mtapi_network_socket_t
*
that
,
int
timeout
);
int
embb_mtapi_network_socket_sendbuffer
(
int
embb_mtapi_network_socket_sendbuffer
(
embb_mtapi_network_socket_t
*
that
,
embb_mtapi_network_socket_t
*
that
,
embb_mtapi_network_buffer_t
*
buffer
embb_mtapi_network_buffer_t
*
buffer
...
...
mtapi_network_c/test/embb_mtapi_network_test_socket.cc
View file @
458054e7
...
@@ -55,19 +55,30 @@ void NetworkSocketTest::TestBasic() {
...
@@ -55,19 +55,30 @@ void NetworkSocketTest::TestBasic() {
err
=
embb_mtapi_network_socket_bind_and_listen
(
&
server_sock
,
"127.0.0.1"
,
4711
);
err
=
embb_mtapi_network_socket_bind_and_listen
(
&
server_sock
,
"127.0.0.1"
,
4711
);
PT_EXPECT
(
err
!=
0
);
PT_EXPECT
(
err
!=
0
);
err
=
embb_mtapi_network_socket_select
(
&
server_sock
,
1
);
PT_EXPECT
(
err
==
0
);
err
=
embb_mtapi_network_socket_initialize
(
&
client_sock
);
err
=
embb_mtapi_network_socket_initialize
(
&
client_sock
);
PT_EXPECT
(
err
!=
0
);
PT_EXPECT
(
err
!=
0
);
err
=
embb_mtapi_network_socket_connect
(
&
client_sock
,
"127.0.0.1"
,
4711
);
err
=
embb_mtapi_network_socket_connect
(
&
client_sock
,
"127.0.0.1"
,
4711
);
PT_EXPECT
(
err
!=
0
);
PT_EXPECT
(
err
!=
0
);
err
=
embb_mtapi_network_socket_select
(
&
server_sock
,
-
1
);
PT_EXPECT
(
err
!=
0
);
err
=
embb_mtapi_network_socket_accept
(
&
server_sock
,
&
accept_sock
);
err
=
embb_mtapi_network_socket_accept
(
&
server_sock
,
&
accept_sock
);
PT_EXPECT
(
err
!=
0
);
PT_EXPECT
(
err
!=
0
);
err
=
embb_mtapi_network_socket_select
(
&
accept_sock
,
1
);
PT_EXPECT
(
err
==
0
);
err
=
embb_mtapi_network_buffer_push_back_int32
(
&
send_buffer
,
0x12345678
);
err
=
embb_mtapi_network_buffer_push_back_int32
(
&
send_buffer
,
0x12345678
);
PT_EXPECT
(
err
==
4
);
PT_EXPECT
(
err
==
4
);
err
=
embb_mtapi_network_socket_sendbuffer
(
&
client_sock
,
&
send_buffer
);
err
=
embb_mtapi_network_socket_sendbuffer
(
&
client_sock
,
&
send_buffer
);
PT_EXPECT
(
err
==
4
);
PT_EXPECT
(
err
==
4
);
err
=
embb_mtapi_network_socket_select
(
&
accept_sock
,
-
1
);
PT_EXPECT
(
err
!=
0
);
err
=
embb_mtapi_network_socket_recvbuffer
(
&
accept_sock
,
&
recv_buffer
);
err
=
embb_mtapi_network_socket_recvbuffer
(
&
accept_sock
,
&
recv_buffer
);
PT_EXPECT
(
err
==
4
);
PT_EXPECT
(
err
==
4
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment