28 #if defined(_USE_WIN_API) 31 #pragma comment(lib, "wsock32.lib") 32 #elif defined(_USE_LINUX_API) 33 #include <arpa/inet.h> 36 #include <netinet/tcp.h> 37 #include <sys/socket.h> 49 #if defined(_USE_WIN_API) 54 struct tcp_keepalive stParameter =
58 stParameter.onoff = (enable != 0) ? 1: 0;
59 stParameter.keepalivetime = (idle == 0) ? 1 : (idle * 1000);
60 stParameter.keepaliveinterval = interval * 1000;
61 ret = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &stParameter,
sizeof(stParameter), &stReturned,
sizeof(stReturned), &dwBytes, NULL, NULL);
65 #elif defined(_USE_LINUX_API) 70 enable = (enable != 0) ? 1: 0;
77 idle = (idle == 0) ? 1 : idle;
113 #if defined(_USE_WIN_API) 114 WSAEVENT connevt = NULL;
115 WSANETWORKEVENTS events;
122 if (param == NULL || sock == NULL) {
132 memset(&server, 0,
sizeof(server));
138 #if defined(_USE_WIN_API) 139 connevt = WSACreateEvent();
140 if(connevt == WSA_INVALID_EVENT) {
144 ret = WSAEventSelect(*sock, connevt, FD_CONNECT);
145 if(ret == SOCKET_ERROR) {
150 funcval = fcntl(*sock,
F_GETFL, 0);
165 #if defined(_USE_WIN_API) 166 ret = WSAGetLastError();
167 if(ret != WSAEWOULDBLOCK) {
172 ret = WSAWaitForMultipleEvents(1, &connevt,
174 if (ret != WSA_WAIT_EVENT_0) {
175 if (ret == WSA_WAIT_TIMEOUT) {
183 ret = WSAEnumNetworkEvents(*sock, connevt, &events);
184 if (ret == SOCKET_ERROR) {
188 if (!(events.lNetworkEvents & FD_CONNECT)) {
192 if (events.iErrorCode[FD_CONNECT_BIT] != 0) {
198 if (ret != EINPROGRESS) {
209 ret =
select(*sock + 1, NULL, &fds, NULL, &tv);
223 if((sock != NULL) && (*sock != 0)) {
224 #if defined(_USE_WIN_API) 226 WSAEventSelect(*sock, NULL, 0);
227 ioctlsocket(*sock, FIONBIO, &dwTemp);
229 if(connevt != NULL) {
230 WSACloseEvent(connevt);
260 if (param == NULL || sock == NULL)
326 if (buf == NULL || strlen(buf) == 0)
330 flag |= MSG_NOSIGNAL;
334 flag |= *(
int *) arg;
337 len_send = (len_buf != 0) ? len_buf : strlen(buf);
339 ret =
send(sock, buf, len_send, flag);
347 if (len_send > len_sended) {
372 if (buf == NULL || len_recved == NULL)
376 flag |= *(
int *) arg;
383 ret =
recv(sock, buf, len_buf, flag);
391 if (*len_recved == 0) {
431 hr =
tcp_recv(sock, buf_tmp, len_recv, &len_recved, 0, NULL);
463 *client =
accept(sock, NULL, NULL);
507 enable = (enable != 0) ? 1 : 0;
HRESULT tcp_send(int sock, const char *buf, uint32_t len_buf, void *arg)
Sends TCP packet.
static SOCKET accept(SOCKET s, struct sockaddr *addr, int *addrlen)
HRESULT tcp_set_timeout(int sock, uint32_t timeout)
Sets timeout value to the TCP socket.
HRESULT tcp_accept(int sock, int *client)
TCP server accepts a TCP client.
HRESULT tcp_open_server(void *param, int *sock)
Opens TCP server.
HRESULT tcp_close(int *sock)
Closes the socket.
#define FAILED(hr)
A macro that returns TRUE/FALSE. If hr is less than zero, then returns TRUE.
#define E_HANDLE
Failed because the handle is invalid.
#define DEV_BUF_MAX
The maximum buffer size of a packet.
#define E_ACCESSDENIED
Failed because the resource is not ready.
#define E_INVALIDARG
Failed because some arguments are invalid.
#define E_FAIL
Failed because unspecified error occurs.
static int send(SOCKET s, const char *buf, int len, int flags)
#define DNGetLastError()
A macro that gets last OS error.
HRESULT tcp_open_client(void *param, int *sock)
Opens TCP client.
static int32_t _tcp_set_keepalive(int sock, int enable, uint32_t idle, uint32_t interval, uint32_t count)
_DN_EXP_SOCKET HRESULT socket_close(int *sock)
Closes the socket.
#define SUCCEEDED(hr)
A macro that returns TRUE/FALSE. If hr is zero or more, then returns TRUE.
_DN_EXP_SOCKET HRESULT socket_bind(const struct CONN_PARAM_ETH *param, int *sock)
Binds the socket with param's source address and source port.
A type definition for Ethernet connection parameters.
_DN_EXP_SOCKET HRESULT socket_set_timeout(int sock, uint32_t timeout)
Sets timeout value to the socket.
_DN_EXP_SOCKET HRESULT socket_open(int type, int *sock)
Creates a socket.
#define FD_SET(fd, fdsetp)
HRESULT tcp_clear(int sock, uint32_t timeout)
Clears the received buffer.
static int setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen)
_DN_EXP_DEVICE HRESULT check_timeout(int sock, uint32_t timeout)
Checks the communication timeout.
HRESULT tcp_set_nodelay(int sock, int enable)
Sets no delay option.
static int select(int __nfds, fd_set *__restrict __readfds, fd_set *__restrict __writefds, fd_set *__restrict __exceptfds, struct timeval *__restrict __timeout)
uint32_t tcp_conn_timeout
static int recv(SOCKET s, char *buf, int len, int flags)
static int listen(SOCKET s, int backlog)
HRESULT tcp_recv(int sock, char *buf, uint32_t len_buf, uint32_t *len_recved, uint32_t timeout, void *arg)
Receives TCP packet.
#define E_TIMEOUT
Failed because the communication timed out.
static int connect(SOCKET s, const struct sockaddr *name, int namelen)
HRESULT tcp_set_keepalive(int sock, int enable, uint32_t idle, uint32_t interval, uint32_t count)
Sets keep alive option.
#define OSERR2HRESULT(err)
A macro that returns HREUSLT(0x8091) which means OS error.