static uint32_t timeout = 0;
- if( p_iot->tcp_client.connected == 1 ){
+ if( p_iot->tcp_client.connected == TCP_CONNECTED ){
switch( p_iot->connected ){
case 0:
if( g_dev.idbm ){
break;
}
}else{
- if( timeout++ > 10 ){
- timeout= 0;
- printf( "connect timeout , restart tcp client!\n" );
-
- //uv_close((uv_handle_t*) , NULL);
- tcp_cli_start( &p_iot->tcp_client, 1*1000); //tcp close, reconnect ....
- uv_timer_start( &s_iot.iot_timer, connect_timer_cb, 2000, 0 );
-
- }else{
//wait tcp connected
printf( "wait tcp connect ...\n" );
- uv_timer_start( &s_iot.iot_timer, connect_timer_cb, 2000, 0 );
- }
+ uv_timer_start( &s_iot.iot_timer, connect_timer_cb, 5000, 0 );
}
}
p_iot = (iot_t *)handle->data;
- if( p_iot->tcp_client.connected ){
+ if( p_iot->tcp_client.connected == TCP_CONNECTED ){
if( p_iot->connected ){
rpt_str = iot_wrap_heartbeat_pkt( );
p_iot = (iot_t *)handle->data;
- if( p_iot->tcp_client.connected ){
+ if( p_iot->tcp_client.connected == TCP_CONNECTED ){
if( p_iot->connected ){
iot_tx_msg_t *msg;
char perr;
+ if (status < 0) {
+ // 写入失败
+ fprintf(stderr, "Write error: %s\n", uv_strerror(status));
+ if( !uv_is_closing( (uv_handle_t*)&s_iot.tcp_client.sock ) ){
+ printf( "iot_tx_cb err: .. close sock!\n" );
+ uv_close( (uv_handle_t*)&s_iot.tcp_client.sock, NULL );
+ }else{
+ printf( "iot_tx_cb err, to close socket, ------ uv_is_closing\n" );
+ }
+ printf( "clear msg queue\n" );
+ while((msg = cQRcv( s_iot.tx_q, &perr ))){
+ free(msg);
+ }
+ s_iot.tx_busy = 0;
+ s_iot.tcp_client.connected = TCP_DISCONNECTED;
+ iot_tcp_conn_cb( &s_iot.tcp_client, 0 );
+ tcp_cli_start( &s_iot.tcp_client, 10*1000 );//tcp close, reconnect ....
+ return;
+ }
+
+ printf( "uv_tcp connect ... \n" );
msg = cQRcv( s_iot.tx_q, &perr );
if( msg ){
printf( "tcp write data -- recv msg, write msg...\n" );
uv_buf_t uvBuf = uv_buf_init( msg->base, msg->sz );
- uv_write( &s_iot.tcp_client.write_req, (uv_stream_t*)&s_iot.tcp_client.sock, &uvBuf, 1, iot_tx_cb );
+ if( s_iot.connected ){
+ uv_write( &s_iot.tcp_client.write_req, (uv_stream_t*)&s_iot.tcp_client.sock, &uvBuf, 1, iot_tx_cb );
+ }else{
+ printf( "-- iot disconnect ---\n" );
+ }
free( msg );
}else{
printf( "tcp write data -- done, tx_bus->0\n" );
#include "alarmmonitor.h"
#include "am_tcp.h"
-/*
-typedef struct{
- const char * name;
- uv_timer_t timer;
- uv_loop_t *loop;
- char * host;
- char port[8];
-// int state; //0:init, 1:resolved, 2:connected,
- int connected;
- uv_getaddrinfo_t resolver;
- uv_tcp_t sock;
- uv_connect_t conn_req;
- uv_write_t write_req;
-
- void * data;
-}tcp_cli_t;
-*/
void tcp_cli_start( tcp_cli_t *cli, int delay_ms );
void on_tcp_write(uv_write_t* req, int status);
+void tcp_cli_start_timer_cb(uv_timer_t * handle );
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
{
p_cli->conn_cb( p_cli, 0 );
//if (nread != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
- uv_close((uv_handle_t*) client, NULL);
+ if( !uv_is_closing( (uv_handle_t*)client ) ){
+ uv_close((uv_handle_t*) client, NULL);
+ }else{
+ printf( "on_read err, to close socket, ------ uv_is_closing\n" );
+ }
if( buf && buf->base )
free(buf->base);
//free(client);
printf( "%s-------tcp on_connect st:%d-------\n", p_cli->name, status );
if (status < 0)
{
+ p_cli->connected = TCP_DISCONNECTED;
+ AM_LOGI( "[%s] TCP Connect ---------------------- FAIL", p_cli->name );
fprintf(stderr, "Connection error %s\n", uv_strerror(status));
uv_close( (uv_handle_t*)&p_cli->sock, NULL );
tcp_cli_start( p_cli, 10*1000 );
return;
}
+ uv_timer_stop( &p_cli->start_timer );
+
//uv_tcp_keepalive( &p_cli->sock, 1, 10 );
p_cli->write_req.data = p_cli;
uv_read_start((uv_stream_t*)&p_cli->sock, alloc_buffer, on_read );
- p_cli->connected = 1;
+ p_cli->connected = TCP_CONNECTED;
if( p_cli->conn_cb )
p_cli->conn_cb( p_cli, 1 );
-#if 0
- char* str = "hello server, this is client!";
- uv_buf_t uvBuf = uv_buf_init(str, strlen(str));
- fprintf(stdout, "----Write req:%p\n", &p_cli->write_req );
- uv_write( &p_cli->write_req, (uv_stream_t*)&p_cli->sock, &uvBuf, 1, on_tcp_write);
-#endif
- fprintf(stdout, "Connect ok\n");
+ AM_LOGI( "[%s] TCP Connect ---------------------- OK", p_cli->name );
}
void on_resolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res)
if (status < 0) {
fprintf(stderr, "getaddrinfo callback error %s\n", uv_strerror(status) );
- tcp_cli_start( p_cli, 5*1000 );
+ tcp_cli_start( p_cli, 10*1000 );
return;
}
uv_ip4_name((struct sockaddr_in*) res->ai_addr, addr, 16);
- fprintf(stderr, "on_resolved: [struct addrinfo *res:%p], - str %s\n", res, addr);
+ printf( "\n\non_resolved: [struct addrinfo *res:%p], - str %s\n", res, addr);
+ printf( "uv_tcp init ... start\n" );
uv_tcp_init( p_cli->loop, &p_cli->sock);
p_cli->conn_req.data = p_cli;
+ AM_LOGI( "[%s] TCP Connect ...............................", p_cli->name );
+ p_cli->connected = TCP_CONNECTING;
+ uv_timer_stop( &p_cli->start_timer );
+printf( "start conn timer ......................................\n" );
uv_tcp_connect(&p_cli->conn_req, &p_cli->sock, (const struct sockaddr *) res->ai_addr, on_connect);
-
+ printf( "uv_tcp connect ... wait...\n" );
uv_freeaddrinfo(res);
}
if (r) {
fprintf(stderr, "getaddrinfo call error %s\n", uv_strerror( r ) );
- tcp_cli_start( handle->data, 5*1000 );
+ tcp_cli_start( handle->data, 10*1000 );
}
}
cli->loop = uv_default_loop();
cli->host = strdup( host );
sprintf( cli->port, "%d", port );
- uv_timer_init( cli->loop, &cli->timer);
- cli->timer.data = cli;
-
- //cli->state = 0;
+ uv_timer_init( cli->loop, &cli->start_timer);
+ cli->start_timer.data = cli;
+ cli->connected = TCP_DISCONNECTED;
cli->sock.data = cli;
cli->name = strdup( name );
cli->conn_cb = conn_cb;
}
-void tcp_cli_start( tcp_cli_t *cli, int delay_ms )
+void tcp_cli_start_timer_cb(uv_timer_t * handle )
+{
+ int r;
+ struct addrinfo hints;
+
+ tcp_cli_t *p_cli = (tcp_cli_t*)handle->data;
+
+ hints.ai_family = PF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+ hints.ai_flags = 0;
+
+ p_cli->resolver.data = p_cli;
+ r = uv_getaddrinfo( handle->loop, &p_cli->resolver, on_resolved, p_cli->host, p_cli->port, &hints );
+
+ if (r) {
+ fprintf(stderr, "getaddrinfo call error %s\n", uv_strerror( r ) );
+ //restart ...
+ tcp_cli_start( handle->data, 10*1000 );
+ }
+}
+
+void tcp_cli_target( tcp_cli_t *cli, char *host, uint16_t port )
{
-// if( cli->state == 0 ){
- uv_timer_start( &cli->timer, tcp_cli_resolve, delay_ms, 0 );
-// }else if( cli->state == 1 ){
- //uv_timer_start( cli->timer, tcp_cli_connect, 0, 0 );
-// uv_timer_start( &cli->timer, tcp_cli_resolve, delay_ms, 0 );
-// }
+ if( host && port>0 ){
+ if( cli->host )
+ free( cli->host );
+ cli->host = strdup( host );
+ sprintf( cli->port, "%d", port );
+ }
}
-void _tcp_write_data( tcp_cli_t *cli, char *tx_data, int tx_size )
+void tcp_cli_start( tcp_cli_t *cli, int delay_ms )
{
- uv_buf_t uvBuf = uv_buf_init( tx_data, tx_size );
- uv_write( &cli->write_req, (uv_stream_t*)&cli->sock, &uvBuf, 1, on_tcp_write);
- printf( "tcp write data -- %s\n", tx_data );
+ cli->connected = TCP_DISCONNECTED;
+ uv_timer_start( &cli->start_timer, tcp_cli_start_timer_cb, delay_ms, 0 );
}
void tcp_write_buf( tcp_cli_t *cli, uv_buf_t *uvBuf )
{
+ printf( "[%s]tcp write buf -- %p -- %ld\n", cli->name, uvBuf->base, uvBuf->len );
uv_write( &cli->write_req, (uv_stream_t*)&cli->sock, uvBuf, 1, on_tcp_write);
- ///printf( "tcp write buf -- %p -- %ld\n", uvBuf->base, uvBuf->len );
}