*** svdrpc.cpp Fri Mar 29 19:04:51 2002 --- /home/stuermml/Tech/kvdr-0.51.1/kvdr/svdrpc.cpp Sun Mar 23 21:18:17 2003 *************** *** 95,100 **** --- 95,109 ---- return -1; } + bool cSocket::Alive(void) + { + char buf[301]; + errno=0; + int result = read(sock, &buf, 0); + fprintf(stderr, "Alive check: result = %d, errno = %d\n", result, errno); + return ((result != -1) || (errno!=ENOTCONN)); + } + // --- cSVDRPC ---------------------------------------------------------------- bool cSVDRPC::Connected() *************** *** 118,124 **** else filedes = -1; outstandingReply=1; ! ReadReply();//lese Greeting... } void cSVDRPC::Close(void) --- 127,146 ---- else filedes = -1; outstandingReply=1; ! int count = 1000; ! while (count--) { ! if (ReadReply()) break; ! } ! } ! ! void cSVDRPC::Reconnect(void) { ! Close();//reopen connection in the case something went wrong: ! filedes = socket.Connect(); ! outstandingReply=1; ! int count = 1000; ! while (count--) { ! if (ReadReply()) break;//lese Greeting... ! } } void cSVDRPC::Close(void) *************** *** 135,156 **** if (length < 0) length = strlen(s); outstandingReply++; { ! int wbytes=write(filedes, s, length); ! if (wbytes == length) ! return true; ! if (wbytes < 0) ! { ! //perror("svdrpc-write:"); ! Close();//reopen connection in the case something went wrong: ! //if (socket.Open()) ! // filedes = socket.Connect(); ! //else ! filedes = -1; ! outstandingReply=1; ! ReadReply();//lese Greeting... ! } ! else ! fprintf(stderr, "Wrote %d bytes instead of %d\n", wbytes, length); } } return false; --- 157,179 ---- if (length < 0) length = strlen(s); outstandingReply++; { ! int wbytes; ! int cerr; ! int count = 5; ! while (count--) { ! fprintf(stderr, "Sending %d bytes (trial %d)\n", length, 5 - count); ! wbytes = write(filedes, s, length); ! if (wbytes == length) { ! fprintf(stderr, " ok\n"); ! return true; ! } ! if (wbytes < 0) { ! fprintf(stderr, " result %d, reconnecting\n", wbytes); ! Reconnect(); ! } else { ! fprintf(stderr, " Wrote %d bytes instead of %d\n", wbytes, length); ! } ! } } } return false; *************** *** 167,173 **** FD_SET(filedes, &set); struct timeval timeout; timeout.tv_sec = 0; ! timeout.tv_usec = 200; do { select(1, &set, NULL, NULL, &timeout); --- 190,196 ---- FD_SET(filedes, &set); struct timeval timeout; timeout.tv_sec = 0; ! timeout.tv_usec = 200000; do { select(1, &set, NULL, NULL, &timeout); *************** *** 181,192 **** --- 204,219 ---- { buf[rbytes]=0; if((outstandingReply>0)&&(buf[rbytes-1]=='\n'))outstandingReply--; + fprintf(stderr, "read returns %d %s\n", n, buf); return true; } else { + fprintf(stderr, "read returns eof\n"); buf[0]=0; } + } else { + fprintf(stderr, "filedes < 0\n"); } return false; } *************** *** 195,202 **** { char *Option=NULL; asprintf(&Option,"Quit\r\n"); ! bool result=Send(Option); ! ReadReply(); delete Option; return result; } --- 222,239 ---- { char *Option=NULL; asprintf(&Option,"Quit\r\n"); ! int count = 5; ! bool result; ! while (count--) { ! result = Send(Option); ! if (ReadReply()) { ! fprintf(stderr, "read: acknowledged\n"); ! count = 0; ! } else { ! fprintf(stderr, "read: socked died, reconnecting\n"); ! Reconnect(); ! } ! } delete Option; return result; } *************** *** 206,230 **** { char *Option=NULL; asprintf(&Option,"HITK %s\r\n",key); ! bool result=Send(Option); ! ReadReply(); delete Option; return result; } const char* cSVDRPC::CmdChan() { ! int n=0; ! while(outstandingReply>0 && Connected() && n++<100) ! { ! ReadReply(); ! } char *Option=NULL; asprintf(&Option,"CHAN\r\n"); Send(Option); delete Option; int id,chan; ! n=0; do { ReadReply(); --- 243,274 ---- { char *Option=NULL; asprintf(&Option,"HITK %s\r\n",key); ! int count = 5; ! bool result; ! while (count--) { ! ClrBuf(); ! result = Send(Option); ! if (250 == ReadReplyC()) { ! fprintf(stderr, "read: acknowledged\n"); ! count = 0; ! } else { ! fprintf(stderr, "read: socked died, reconnecting\n"); ! Reconnect(); ! } ! } delete Option; return result; } const char* cSVDRPC::CmdChan() { ! ClrBuf(); char *Option=NULL; asprintf(&Option,"CHAN\r\n"); Send(Option); delete Option; int id,chan; ! int n=0; do { ReadReply(); *************** *** 239,241 **** --- 283,305 ---- } return name; } + + void cSVDRPC::ClrBuf() { + int n=0; + while(outstandingReply>0 && Connected() && n++<100) + { + ReadReply(); + } + } + + int cSVDRPC::ReadReplyC() { + int n=0; + int id; + do { ReadReply(); } while (outstandingReply>0 && n++<100); + if (1==sscanf(buf,"%d%d%20s",&id)) { + fprintf(stderr, "answer code is %d\n", id); + return id; + } + return -1; + } + *** svdrpc.h Fri Mar 29 19:04:51 2002 --- /home/stuermml/Tech/kvdr-0.51.1/kvdr/svdrpc.h Sun Mar 23 21:01:44 2003 *************** *** 30,35 **** --- 30,36 ---- bool Open(int Port); int Connect(void); void Close(void); + bool Alive(void); }; class cSVDRPC { *************** *** 47,52 **** --- 48,56 ---- int filedes; bool Send(const char *s, int length = -1); bool ReadReply(); + int ReadReplyC(); + void ClrBuf(); + void Reconnect(); char buf[MAXCMDBUFFER]; int outstandingReply; };