Commit 88f4925a authored by 黄振令's avatar 黄振令

【修改内容】1. 保存发现网关的列表,主机重启后能继续连接;2. platform tcp连接失败尝试一直连接

parent cc8d5de8
......@@ -31,6 +31,54 @@ static ipc_cb* g_cb = NULL;
static struct ev_io w_accept;
// Save/Load the gateway list
void kk_gw_list_save(void)
{
FILE *fp;
uint8_t i;
uint8_t j;
int len = MAX_LISTEN_NUM * sizeof(kk_tcp_ctrl_t);
// Save device table
fp = fopen("gwlist.txt", "w");
fwrite(g_tcp_ctrl,len,1,fp);
fclose(fp);
}
void kk_gw_list_load(void)
{
uint16_t i;
FILE *fp;
int readLen = MAX_LISTEN_NUM * sizeof(kk_tcp_ctrl_t);
int retLen = 0;
fp = fopen("gwlist.txt", "r");
if (!fp) {
printf("open gatewaylist.txt failed! \n");
goto error1;
}
char buf[1024] = {0};
retLen = fread(g_tcp_ctrl, readLen, 1, fp);
printf("read gatewaylist.txt retLen= %d, readLen=%d ! \n", retLen,readLen);
if (retLen != readLen ){
printf("read gatewaylist.txt failed! \n");
}
fclose(fp);
// Set the rest of the device table to null.
error1:
for (i=0; i < MAX_LISTEN_NUM; i++) {
printf("deviceCode ip sock [%s] [%s] [%d] \n",g_tcp_ctrl[i].deviceCode,
g_tcp_ctrl[i].ip==NULL?"":g_tcp_ctrl[i].ip, g_tcp_ctrl[i].sock);
g_tcp_ctrl[i].sock = -1;
}
}
static int get_idx_by_ip(char ip[MAX_IP_LEN]){
int i = 0;
......@@ -162,6 +210,7 @@ int kk_set_tcp_channel(char devCode[DEVICE_CODE_LEN], char ip[MAX_IP_LEN]){
if(strcmp(devCode, g_tcp_ctrl[i].deviceCode) == 0){
strncpy(g_tcp_ctrl[i].ip, ip, strlen(ip));
printf("find and replace it [%d][%s][%s] \n",i, g_tcp_ctrl[i].ip, devCode);
kk_gw_list_save();
break;
}
......@@ -179,6 +228,7 @@ int kk_set_tcp_channel(char devCode[DEVICE_CODE_LEN], char ip[MAX_IP_LEN]){
strncpy(g_tcp_ctrl[isEmptyIdx].ip, ip, strlen(ip));
strncpy(g_tcp_ctrl[isEmptyIdx].deviceCode, devCode, strlen(devCode));
printf("idx deviceCode ip[%d][%s][%s]",isEmptyIdx, g_tcp_ctrl[isEmptyIdx].deviceCode, g_tcp_ctrl[isEmptyIdx].ip);
kk_gw_list_save();
return 0;
}
......@@ -384,9 +434,8 @@ int kk_TCP_channel_init(ipc_cb cb)
}
g_init = 1;
memset(g_tcp_ctrl, 0, sizeof(kk_tcp_ctrl_t)*MAX_LISTEN_NUM);
for(i = 0; i < MAX_LISTEN_NUM; i++){
g_tcp_ctrl[i].sock = -1;
}
kk_gw_list_load();
if (g_pTh ==NULL && 0 != pthread_create(&g_pTh, NULL, loop_tcp_thread, NULL)) {
printf("create pthread failed\r\n");
......@@ -479,6 +528,9 @@ void _MutexUnlock(void *mutex)
typedef struct {
void *mutex;
int sd;
int isConnect;
char ip[MAX_IP_LEN];
int port;
ipc_cb* cb;
} kk_tcp_client_t;
......@@ -495,29 +547,7 @@ static int _init_client(){
g_client_ctrl.sd = -1;
}
static void loop_tcp_client_thread(void *arg){
printf("loop_tcp_client_thread start!\r\n");
char buf[1024]= {0};
int ret = 0;
while(1){
_MutexLock(g_client_ctrl.mutex);
ret = read(g_client_ctrl.sd, buf, sizeof(buf));
_MutexUnlock(g_client_ctrl.mutex);
if(-1== ret){
//printf("=================read error \n");
//break ;
}else if(ret > 0){
printf("buf = %s\n",buf);
if (g_client_ctrl.cb != NULL){
g_client_ctrl.cb(buf,ret,"");
}
}
usleep(100000);
}
printf("loop_tcp_client_thread================== end \n");
}
static int client_socket_init(int *sd, char *ipaddr, uint16_t port)
{
......@@ -570,14 +600,57 @@ err1:
return -1;
}
static void loop_tcp_client_thread(void *arg){
printf("loop_tcp_client_thread start!\r\n");
char buf[1024]= {0};
int ret = 0;
while(1){
if(-1 == client_socket_init(&g_client_ctrl.sd,g_client_ctrl.ip, g_client_ctrl.port)){
printf("connect failed \n");
sleep(1);
continue;
}
g_client_ctrl.isConnect = 1;
while(g_client_ctrl.isConnect){
_MutexLock(g_client_ctrl.mutex);
ret = read(g_client_ctrl.sd, buf, sizeof(buf));
_MutexUnlock(g_client_ctrl.mutex);
if(-1== ret){
//printf("=================read error \n");
//break ;
}else if(ret > 0){
printf("buf = %s\n",buf);
if (g_client_ctrl.cb != NULL){
g_client_ctrl.cb(buf,ret,"");
}
}
usleep(100000);
}
printf("network error, try connect again! \n");
close(g_client_ctrl.sd);
}
printf("loop_tcp_client_thread================== end \n");
}
int kk_tcp_client_send(char* data, int len){
int ret = 0;
if (g_client_ctrl.sd > -1 && data != NULL){
int cnt = 0;
if ( data != NULL){
while(g_client_ctrl.sd == -1 && cnt < 5){
printf("[%s] tcp don't connect, sleep 1s !!!! \n",__FUNCTION__);
sleep(1);
cnt++;
}
_MutexLock(g_client_ctrl.mutex);
ret = write(g_client_ctrl.sd, data, len);
_MutexUnlock(g_client_ctrl.mutex);
if (ret < 0){
printf("[%s] write failed!!!! \n",__FUNCTION__);
printf("[%s] write failed ret=%d, reconnect !!!! \n",__FUNCTION__, ret);
g_client_ctrl.isConnect = 0;
}
}
}
......@@ -585,11 +658,8 @@ int kk_tcp_client_init(char ip[MAX_IP_LEN], int port, ipc_cb cb)
{
kk_tcp_client_deinit();
_init_client();
if(-1 == client_socket_init(&g_client_ctrl.sd,ip, port)){
printf("connect failed \n");
return -1;
}
g_client_ctrl.port = port;
memcpy(g_client_ctrl.ip, ip, strlen(ip));
if (g_pTh ==NULL && 0 != pthread_create(&g_pTh, NULL, loop_tcp_client_thread, NULL)) {
printf("create pthread failed\r\n");
return -1;
......
......@@ -381,6 +381,8 @@ void *udp_dispatch_yield(void *args){
char device_code[DEVICE_CODE_LEN] = {0};
int devId = 0;
kk_TCP_channel_init(gw2mid_cb);
while(1)
{
......@@ -437,7 +439,7 @@ void *udp_dispatch_yield(void *args){
DEBUG_PRINT("gwDevCode =%s proto=%s \n",gwDevCode,proto);
if(strcmp(proto,"tcp") == 0){
//
kk_TCP_channel_init(gw2mid_cb);
kk_set_tcp_channel(gwDevCode,inet_ntoa(from.sin_addr));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment