Commit 1984c81c authored by 黄振令's avatar 黄振令

【修改内容】网关连接主机不上,开始重新发现机制,防止主机ip地址变化,而无法连接

【提交人】huang.zhenling
parent aab93033
......@@ -6,6 +6,10 @@
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include "com_api.h"
#define CCU_TCP_PORT 16565
......@@ -554,14 +558,14 @@ typedef struct {
char ip[MAX_IP_LEN];
int port;
ipc_cb* cb;
int retry;
} kk_tcp_client_t;
static kk_tcp_client_t g_client_ctrl = {NULL, -1, 0,{0},0, NULL};
static kk_tcp_client_t g_client_ctrl = {NULL, -1, 0,{0},0, NULL,0};
static int _init_client(){
memset(&g_client_ctrl, 0 ,sizeof(kk_tcp_client_t));
/* Create Mutex */
g_client_ctrl.mutex = _MutexCreate();
if (g_client_ctrl.mutex == NULL) {
return -1;
......@@ -601,14 +605,14 @@ static int client_socket_init(int *sd, char *ipaddr, uint16_t port)
}
//连接
int retry = 0;
for(;retry < 10; retry++){
for(;retry < 3; retry++){
if(-1 != connect(sock, (struct sockaddr *)&addr, sizeof(addr))){
break;
}
printf("==================connect retry=%d \n", retry);
//printf("==================connect retry=%d \n", retry);
sleep(1);
}
if (retry >= 10){
if (retry >= 3){
printf("==================connect failed \n");
goto err2;
}
......@@ -626,28 +630,56 @@ static void loop_tcp_client_thread(void *arg){
printf("loop_tcp_client_thread start!\r\n");
char buf[1024]= {0};
int ret = 0;
fd_set fds;
struct timeval timeout={0,200}; //select等待3秒,3秒轮询,要非阻塞就置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);
g_client_ctrl.retry++;
continue;
}
g_client_ctrl.isConnect = 1;
g_client_ctrl.retry = 0;
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,"");
FD_ZERO(&fds); //每次循环都要清空集合,否则不能检测描述符变化
FD_SET(g_client_ctrl.sd,&fds); //添加描述符
switch(select(g_client_ctrl.sd + 1,&fds,NULL,NULL,&timeout)) //select使用
{
case -1:
g_client_ctrl.isConnect = 0;
printf(" [%s] select error ret=%d \n", __FUNCTION__, ret);
break; //select错误 退出循环
case 0:
break; //再次轮询
default:
if(FD_ISSET(g_client_ctrl.sd,&fds)) //测试sock是否可读,即是否网络上有数据
{
//接受网络数据
_MutexLock(g_client_ctrl.mutex);
ret = read(g_client_ctrl.sd, buf, sizeof(buf));
_MutexUnlock(g_client_ctrl.mutex);
if( ret <= 0){
printf("=================read error ret=%d \n",ret);
if (errno != EINTR){
g_client_ctrl.isConnect = 0;
printf("read error reconnect!! \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);
break;
}// end switch
//usleep(100000);
}
printf("network error, try connect again! \n");
......@@ -658,6 +690,14 @@ static void loop_tcp_client_thread(void *arg){
}
int kk_get_retry_num(){
return g_client_ctrl.retry;
}
int kk_reset_retry_num(){
return g_client_ctrl.retry = 0;
}
int kk_tcp_client_send(char* data, int len){
int ret = 0;
int cnt = 0;
......@@ -698,6 +738,7 @@ int kk_tcp_client_init(char ip[MAX_IP_LEN], int port, ipc_cb cb)
kk_tcp_client_deinit(){
if (g_client_ctrl.sd > -1){
close(g_client_ctrl.sd);
g_client_ctrl.sd = -1;
}
_MutexDestroy(g_client_ctrl.mutex);
}
......
......@@ -369,6 +369,7 @@ int search_ccu(char devcode[33], char ip[16], int* port){
return -1;
}
printf("[%s] start search ccu!! \n", __FUNCTION__);
while (1)
{
if ((iSendbytes = sendto(sock, sendMessage, strlen(sendMessage)+1, 0, (struct sockaddr*)&Addrto, sizeof(struct sockaddr))) == -1)
......@@ -487,7 +488,6 @@ void* _msg_event_property_post(char ip[16], int port){
}
void ipcHandle(void)
{
char deviceCode[33] = {0};
......@@ -513,6 +513,8 @@ void ipcHandle(void)
jrpc_register_procedure(&my_server, rpc_table[i].func, rpc_table[i].name, NULL );
}
//send add gw to ccu
char* outbuf = _msg_topo_add();
if (outbuf == NULL){
......@@ -545,11 +547,32 @@ void ipcHandle(void)
kk_sendData2CCU(postmsg, strlen(postmsg));
free(postmsg);
}
if (kk_get_retry_num() > 20){
//discover ccu
search_ccu(deviceCode, ip, &port);
if(strcmp(GW2CCU_PROTOCOL, "tcp") == 0){
kk_tcp_client_init(ip, port, _cb);
}else{
//kk_ipc_init(IPC_PLAT2MID, _cb, macString/*GW_DEVICE_CODE*/, ip);
}
//send add gw to ccu
outbuf = _msg_topo_add();
if (outbuf == NULL){
printf("[%s] topo add msg failed, exit\n",__FUNCTION__);
return;
}
kk_sendData2CCU(outbuf, strlen(outbuf));
free(outbuf);
cnt = 0;
kk_reset_retry_num();
}
}
//jrpc_server_run(&my_server);
//jrpc_server_destroy(&my_server);
}
......
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