From a073dc3c983b4c56c5da92642c9ad11995bdb844 Mon Sep 17 00:00:00 2001
From: yy1717 <fctom1215@outlook.com>
Date: 星期一, 11 五月 2020 18:05:31 +0800
Subject: [PATCH] 新的科目三
---
lib/src/main/cpp/common/net.cpp | 68 +++++++++++++++++----------------
1 files changed, 35 insertions(+), 33 deletions(-)
diff --git a/lib/src/main/cpp/common/net.cpp b/lib/src/main/cpp/common/net.cpp
index 1ccea8c..3ea1083 100644
--- a/lib/src/main/cpp/common/net.cpp
+++ b/lib/src/main/cpp/common/net.cpp
@@ -17,6 +17,8 @@
#include "net.h"
#include "../jni_log.h"
+#define DEBUG(fmt, args...) LOGD("<net> <%s>: " fmt, __func__, ##args)
+
using namespace std;
/*************************************************************
@@ -31,14 +33,14 @@
bool found_first = false;
if ((hptr = gethostbyname(host_name)) == NULL) {
- LOGE("gethostbyname error\n");
+ DEBUG("gethostbyname error\n");
return -1;
}
- LOGD("official hostname: %s\n", hptr->h_name);
+ DEBUG("official hostname: %s\n", hptr->h_name);
for (pptr = hptr->h_aliases; *pptr != NULL; pptr++) {
- LOGD("alias:%s\n", *pptr);
+ DEBUG("alias:%s\n", *pptr);
}
switch(hptr->h_addrtype)
@@ -46,7 +48,7 @@
case AF_INET:
{
for (pptr = hptr->h_addr_list; *pptr != NULL; pptr++) {
- LOGD("addrsss:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
+ DEBUG("addrsss:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
if (!found_first) {
strcpy(net_addr, str);
found_first = true;
@@ -56,7 +58,7 @@
}
case AF_INET6:
default:
- LOGD("unknown address type\n");
+ DEBUG("unknown address type\n");
break;
}
@@ -80,53 +82,53 @@
// int value, value_len;
// getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &value_len);
-// LOGD("keepalive 0 %d", value);
+// DEBUG("keepalive 0 %d", value);
// value = 128;
// setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value));
-// LOGD("keepalive 1 %d", value);
+// DEBUG("keepalive 1 %d", value);
// getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &value_len);
-// LOGD("keepalive 1 %d", value);
+// DEBUG("keepalive 1 %d", value);
/* Set: use keepalive on fd, default 0 */
alive = 1;
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &alive, sizeof(alive)) != 0)
{
- LOGE("TCP Set keepalive error");
+ DEBUG("TCP Set keepalive error");
return -1;
}
/* 20 Seconds not data, send keeplive packet, default 7200 */
idle = 20;
if (setsockopt (fd, SOL_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)) != 0)
{
- LOGE("TCP Set keepalive idle error");
+ DEBUG("TCP Set keepalive idle error");
return -1;
}
/* If not recv respond, After 5 seconds retry, default 75 */
intv = 5;
if (setsockopt (fd, SOL_TCP, TCP_KEEPINTVL, &intv, sizeof(intv)) != 0)
{
- LOGE("TCP Set keepalive intv error");
+ DEBUG("TCP Set keepalive intv error");
return -1;
}
/* If try 9 times and fail, we consider the tcp is disconnected, default 9 */
cnt = 9;
if (setsockopt (fd, SOL_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt)) != 0)
{
- LOGE("TCP Set keepalive cnt error");
+ DEBUG("TCP Set keepalive cnt error");
return -1;
}
/* int timeout = 10000; // 10绉�
if (setsockopt (fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &timeout, sizeof(timeout)) != 0)
{
- LOGE("TCP Set keepalive timeout error");
+ DEBUG("TCP Set keepalive timeout error");
return -1;
}*/
- LOGD("TCP Set keepalive OK");
+ DEBUG("TCP Set keepalive OK");
return 0;
}
@@ -142,13 +144,13 @@
int error_value;
socklen_t error_value_len;
- LOGI("%s", __func__);
+ DEBUG("tcp_connect... %s : %d", ip, port);
error_value_len = sizeof( error_value );
if((soc = socket(PF_INET, SOCK_STREAM, 0)) == -1)
{
- LOGE("%s: socket", __func__);
+ DEBUG("%s: socket", __func__);
return -1;
}
@@ -160,13 +162,13 @@
// Set non-blocking
if( (arg = fcntl(soc, F_GETFL, NULL)) < 0)
{
- LOGE("%s: fcntl( F_GETFL ) error", __func__);
+ DEBUG("%s: fcntl( F_GETFL ) error", __func__);
goto TCP_CONNECT_1;
}
if( fcntl(soc, F_SETFL, arg | O_NONBLOCK) < 0)
{
- LOGE( "%s: fcntl( F_SETFL ) error", __func__);
+ DEBUG( "%s: fcntl( F_SETFL ) error", __func__);
goto TCP_CONNECT_1;
}
@@ -198,12 +200,12 @@
if(fds_ret < 0)
{
- LOGE( "%s: TCP select error", __func__);
+ DEBUG( "%s: TCP select error", __func__);
goto TCP_CONNECT_1;
}
else if(fds_ret == 0)
{
- LOGE("%s: TCP Connect Timeout %ld", __func__, tv.tv_sec);
+ DEBUG("%s: TCP Connect Timeout %ld", __func__, tv.tv_sec);
goto TCP_CONNECT_1;
}
else if(FD_ISSET(soc, &rdfds))
@@ -215,7 +217,7 @@
}
else
{
- LOGE("%s: some error occur in tcp_connect()", __func__);
+ DEBUG("%s: some error occur in tcp_connect()", __func__);
goto TCP_CONNECT_1;
}
@@ -244,11 +246,11 @@
arg &= ~O_NONBLOCK;
if( fcntl(soc, F_SETFL, arg) < 0)
{
- LOGE( "%s: fcntl( F_SETFL ) error", __func__);
+ DEBUG( "%s: fcntl( F_SETFL ) error", __func__);
goto TCP_CONNECT_1;
}
- LOGI("%s: tcp connected %s: %d", __func__, ip, port);
+ DEBUG("%s: tcp connected %s: %d", __func__, ip, port);
return( soc );
@@ -317,19 +319,19 @@
fds_ret = select(fd + 1, NULL, &rdfds, NULL, &tv);
if (fds_ret < 0) {
- LOGE("tcp error send select error");
+ DEBUG("tcp error send select error");
return -1;
} else if(fds_ret == 0) {
- LOGE("tcp error Occur failure(such as line disconnect)");
+ DEBUG("tcp error Occur failure(such as line disconnect)");
//Occur failure(such as line disconnect)
ret = -1;
} else if(FD_ISSET(fd, &rdfds)) {
ret = send(fd, buf, len, 0);
if(ret == -1) {
- LOGE("tcp error TCP Send Error");
+ DEBUG("tcp error TCP Send Error");
}
} else {
- LOGE("tcp error tcp send has error\n");
+ DEBUG("tcp error tcp send has error\n");
}
return ret;
@@ -375,7 +377,7 @@
void DisconnectTCP(int fd)
{
- LOGI("DisconnectTCP fd = %d", fd);
+ DEBUG("DisconnectTCP fd = %d", fd);
if (fd >= 0) {
shutdown(fd, SHUT_RDWR);
close(fd);
@@ -426,12 +428,12 @@
if (fds_ret < 0)
{
- LOGE("UDP send select error");
+ DEBUG("UDP send select error");
return -1;
}
else if(fds_ret == 0)
{
- LOGE("Occur failure(such as line disconnect)");
+ DEBUG("Occur failure(such as line disconnect)");
ret = -1;
}
else if(FD_ISSET(fd, &rdfds))
@@ -447,12 +449,12 @@
if(ret == -1)
{
- LOGE("UDP Send Error");
+ DEBUG("UDP Send Error");
}
}
else
{
- LOGE("UDP send has error\n");
+ DEBUG("UDP send has error\n");
}
return ret;
@@ -473,7 +475,7 @@
if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) {
return 0;
} else {
- LOGE("UDP ERROR!! = %d", errno);
+ DEBUG("UDP ERROR!! = %d", errno);
return -1;
}
}
--
Gitblit v1.8.0