diff options
author | FreeArtMan <dos21h@gmail.com> | 2017-05-13 22:37:06 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2017-05-13 22:37:06 +0100 |
commit | b385fc9a6832f4186f98cde4bd92b7de6ac553b9 (patch) | |
tree | 322004b33f00e35591bdc7cafa1c32df7a9375f7 | |
parent | 1e8e7cf72de6ca97b8e8cd3ba4266788ba9fec53 (diff) | |
download | agni-b385fc9a6832f4186f98cde4bd92b7de6ac553b9.tar.gz agni-b385fc9a6832f4186f98cde4bd92b7de6ac553b9.zip |
Now after suspend HW, bot can reconnect back to server address
-rw-r--r-- | agni.c | 88 | ||||
-rw-r--r-- | cmd_ping.c | 2 | ||||
-rw-r--r-- | sock_conn.c | 9 |
3 files changed, 87 insertions, 12 deletions
@@ -10,6 +10,7 @@ #include <unistd.h> #include <netdb.h> #include <sys/syscall.h> +#include <sys/socket.h> #include <sched.h> #include <stdatomic.h> @@ -164,6 +165,7 @@ int th_start_client(void *data) int run; int mq_event; int byte_num; + int fret=-1; server_cfg *cfg = data; mq_ntf_mdt *mq = cfg->mq; @@ -175,7 +177,8 @@ int th_start_client(void *data) //network creation var int cret = -1; - int conn=-1; + //int conn=-1; + irc_conn conn; char conn_buf[TH_CONN_BUF_SZ]; char cmd_buf[TH_CONN_BUF_SZ]; @@ -202,6 +205,7 @@ int th_start_client(void *data) //PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); + /* //PNL(); //PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); conn = irc_connect(cfg->server, cfg->port); @@ -217,13 +221,24 @@ int th_start_client(void *data) //some logic how to deal with it atomic_fetch_sub(&cfg->running,1); //return 0; + }*/ + + memset(&conn, 0, sizeof(irc_conn)); + fret = irc_open(cfg->server, cfg->port, &conn); + if (fret != 0) + { + ERROR("Cant connect\n"); + return -1; } + //lets timeout after 150 sec, ping from server is 120 sec + irc_read_timeout(&conn,150); + //PNL(); //send some commands to irc to register nick snprintf(cmd_buf, TH_CONN_BUF_SZ, "USER %s 0 0 :%s\r\n", cfg->user, cfg->user); - write(conn, cmd_buf, strlen(cmd_buf)); + write(conn.conn_fd, cmd_buf, strlen(cmd_buf)); snprintf(cmd_buf, TH_CONN_BUF_SZ,"NICK %s \r\n", cfg->user); - write(conn, cmd_buf, strlen(cmd_buf)); + write(conn.conn_fd, cmd_buf, strlen(cmd_buf)); //prepare message queue mq = cfg->mq; @@ -345,7 +360,7 @@ int th_start_client(void *data) //prepare response int fret = snprintf(cmd_buf, TH_CONN_BUF_SZ,":%s PRIVMSG %s :%s \r\n", cfg->user, dest_user, match_resp->resp); PRINT("%s\n",cmd_buf); - write(conn, cmd_buf, fret); + write(conn.conn_fd, cmd_buf, fret); } else { @@ -354,7 +369,7 @@ int th_start_client(void *data) //prepare response int fret = snprintf(cmd_buf, TH_CONN_BUF_SZ,":%s PRIVMSG %s :%s \r\n", dest_user, resp_user, match_resp->resp); PRINT("%s\n",cmd_buf); - write(conn, cmd_buf, fret); + write(conn.conn_fd, cmd_buf, fret); } free(dest_user); dest_user = NULL; @@ -389,9 +404,13 @@ int th_start_client(void *data) //recieve irc commands and pass to MQ, save command to table //fix this by allowing drain just chunks, not whole buffer - memset(conn_buf, 0, TH_CONN_BUF_SZ); - if((cret = read(conn,conn_buf, 256))>0) + memset(conn_buf, 0, TH_CONN_BUF_SZ); + PNL(); + cret = 0; + //if((cret = read(conn,conn_buf, 256))>0) + if ((cret = irc_read(&conn, conn_buf, 256))>0) { + PRINT(">>READ %d<<\n", cret); irc_buf_puts(ib, conn_buf, cret); while (irc_buf_ready(ib) == 1) { @@ -409,7 +428,7 @@ int th_start_client(void *data) char pong_buf[pong_buf_sz]; snprintf(pong_buf,pong_buf_sz,"PONG %s\n",cfg->user); PRINT("%s",pong_buf) - fret = write(conn,pong_buf,strlen(pong_buf)); + fret = write(conn.conn_fd,pong_buf,strlen(pong_buf)); printf("OUT>>PONG %d\n",fret); } @@ -464,7 +483,7 @@ int th_start_client(void *data) //lets join channel memset(cmd_buf, 0, TH_CONN_BUF_SZ); int fret = snprintf(cmd_buf, TH_CONN_BUF_SZ, ":%s JOIN %s\r\n", dest_name, sep_chan); - write(conn, cmd_buf, fret); + write(conn.conn_fd, cmd_buf, fret); break; } iter++; @@ -534,6 +553,57 @@ int th_start_client(void *data) } } + } else if (cret == 0) + { + //int err_read_conn = errno; + //if ((err_read_conn == EBADF)||(err_read_conn == 11)) + { + //TODO add lofic to retry number, and thex exit from thread + PRINT(">>!READ %d<<\n", cret); + //hope server where disconnected + //close(conn); + + if (irc_reconnect(&conn)==-1) + { + while (-1 == irc_reconnect(&conn)) + { + printf("Reconnect in 10 sec\n"); + sleep(10); + } + sleep(1); + } + //send some commands to irc to register nick + snprintf(cmd_buf, TH_CONN_BUF_SZ, "USER %s 0 0 :%s\r\n", cfg->user, cfg->user); + write(conn.conn_fd, cmd_buf, strlen(cmd_buf)); + snprintf(cmd_buf, TH_CONN_BUF_SZ,"NICK %s \r\n", cfg->user); + write(conn.conn_fd, cmd_buf, strlen(cmd_buf)); + + } + } else + { + perror("No conn"); + PRINT(">>READ %d <<\n",cret); + PRINT("CRET %d CONN %d LIBC %d\n", cret, conn.err, conn.liberr); + sleep(1); + if (conn.err == ERR_SC_TIMEOUT) + { + printf("TIMEOUT\n"); + + if (irc_reconnect(&conn)==-1) + { + while (-1 == irc_reconnect(&conn)) + { + printf("Reconnect in 10 sec\n"); + sleep(10); + } + sleep(1); + } + //send some commands to irc to register nick + snprintf(cmd_buf, TH_CONN_BUF_SZ, "USER %s 0 0 :%s\r\n", cfg->user, cfg->user); + write(conn.conn_fd, cmd_buf, strlen(cmd_buf)); + snprintf(cmd_buf, TH_CONN_BUF_SZ,"NICK %s \r\n", cfg->user); + write(conn.conn_fd, cmd_buf, strlen(cmd_buf)); + } } //send command over queue @@ -7,7 +7,7 @@ void *cmd_ping(void *data) printf("PONG\n"); - ret = alloc_new_str("pong "); + ret = alloc_new_str("pong"); return ret; }
\ No newline at end of file diff --git a/sock_conn.c b/sock_conn.c index 57a3acd..119dd10 100644 --- a/sock_conn.c +++ b/sock_conn.c @@ -499,6 +499,7 @@ int irc_reconnect(irc_conn *conn) irc_conn reconn; + PNL(); if (conn == NULL) { PERM(); @@ -516,18 +517,22 @@ int irc_reconnect(irc_conn *conn) fret = irc_open(hostname, port, &reconn); if (fret==-1) { - PERM(); + ENL(); return -1; } if (-1 == irc_read_timeout(&reconn, timeout)) { - PERM(); + ENL(); } //BUG lets think about this case deeper //free(hostname); //free(port); + PNL(); + + memcpy(conn, &reconn, sizeof(irc_conn)); + return 0; } |