*** empty log message ***
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include "errhdl.h"
|
||||
#include "client.h"
|
||||
#include "util.h"
|
||||
#include "str_util.h"
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
@@ -35,7 +39,7 @@ int CocOpen(CocConn *conn)
|
||||
tmo=100; /* wait total ca. 10 sec. for 15 tries */
|
||||
while (try>0) {
|
||||
try--;
|
||||
util_delay(tmo); tmo=tmo*5/4;
|
||||
CocDelay(tmo); tmo=tmo*5/4;
|
||||
ERR_I(i=CocConnect(conn, try>0));
|
||||
if (i==0) return(0);
|
||||
}
|
||||
@@ -48,12 +52,12 @@ int CocOpen(CocConn *conn)
|
||||
int CocInitClient(CocConn *conn, char *host, int port, char *magic, int bufsize, char *startcmd) {
|
||||
assert(conn!=NULL);
|
||||
if (bufsize==0) bufsize=1024;
|
||||
conn->cmdbuf=buf_create(bufsize);
|
||||
conn->resbuf=buf_create(bufsize);
|
||||
ERR_P(conn->cmdbuf=str_create_buf(bufsize, '\0'));
|
||||
ERR_P(conn->resbuf=str_create_buf(bufsize, '\0'));
|
||||
conn->port=port;
|
||||
str_copy(conn->host, host);
|
||||
str_copy(conn->magic, magic);
|
||||
str_copy(conn->startcmd, startcmd);
|
||||
ERR_I(str_copy(conn->host, host));
|
||||
ERR_I(str_copy(conn->magic, magic));
|
||||
ERR_I(str_copy(conn->startcmd, startcmd));
|
||||
conn->fd=-1;
|
||||
conn->varList=NULL;
|
||||
CocVarList(&conn->varList);
|
||||
@@ -69,15 +73,13 @@ int CocSendMagic(CocConn *conn, char *magic) {
|
||||
char *err;
|
||||
|
||||
if (magic[0]!='#') ERR_MSG("magic must start with '#'");
|
||||
buf_put_start(conn->resbuf); /* use return buffer for command in order to preserve command buffer */
|
||||
buf_put_str(conn->resbuf, magic);
|
||||
buf_put_end(conn->resbuf);
|
||||
if (conn->resbuf->buf==NULL) ERR_MSG("buffer full");
|
||||
ERR_SI(send(conn->fd, conn->resbuf->buf, conn->resbuf->size, 0));
|
||||
str_put_start(conn->resbuf); /* use return buffer for command in order to preserve command buffer */
|
||||
ERR_I(str_put_str(conn->resbuf, magic));
|
||||
ERR_SI(send(conn->fd, conn->resbuf->buf, conn->resbuf->wrpos, 0));
|
||||
ERR_I(CocRecv(conn->fd, conn->resbuf));
|
||||
err=buf_get_str(conn->resbuf);
|
||||
ERR_P(err=str_get_str(conn->resbuf, NULL));
|
||||
if (*err!='\0') { ErrMsg(err); ErrTxt(": (response from server)",0); goto OnError; }
|
||||
if (buf_size(conn->resbuf)!=0) ERR_MSG("syntax error");
|
||||
ERR_I(str_get_end(conn->resbuf));
|
||||
return(0);
|
||||
OnError: return(-1);
|
||||
}
|
||||
@@ -89,7 +91,7 @@ int CocTryCmd(CocConn *conn)
|
||||
ERR_I(CocOpen(conn));
|
||||
ERR_I(CocSendMagic(conn, conn->magic));
|
||||
}
|
||||
ERR_SI(send(conn->fd, conn->cmdbuf->buf, conn->cmdbuf->size, 0));
|
||||
ERR_SI(send(conn->fd, conn->cmdbuf->buf, conn->cmdbuf->wrpos, 0));
|
||||
ERR_I(CocRecv(conn->fd, conn->resbuf));
|
||||
return(0);
|
||||
OnError: return(-1);
|
||||
@@ -103,16 +105,17 @@ int CocCmd(CocConn *conn, const char *rwList)
|
||||
const char *t, *s;
|
||||
char nam[32];
|
||||
CocVar *var;
|
||||
void *adr;
|
||||
|
||||
assert(conn!=NULL);
|
||||
buf_put_start(conn->cmdbuf);
|
||||
str_put_start(conn->cmdbuf);
|
||||
s=rwList;
|
||||
setmode=0;
|
||||
do {
|
||||
t=strchr(s, ',');
|
||||
if (t==NULL) t=s+strlen(s);
|
||||
if (*s=='[') {
|
||||
if (!setmode) buf_put_str(conn->cmdbuf, "[");
|
||||
if (!setmode) ERR_I(str_put_str(conn->cmdbuf, "["));
|
||||
s++;
|
||||
setmode=1;
|
||||
}
|
||||
@@ -121,19 +124,17 @@ int CocCmd(CocConn *conn, const char *rwList)
|
||||
str_ncpy(nam, s, i+1);
|
||||
if (setmode) {
|
||||
if (nam[i-1]==']') { i--; nam[i]='\0'; setmode=0; }
|
||||
buf_put_str(conn->cmdbuf, nam);
|
||||
ERR_I(CocPutVar(conn->varList, conn->cmdbuf, nam));
|
||||
if (!setmode) buf_put_str(conn->cmdbuf, "]");
|
||||
ERR_I(str_put_str(conn->cmdbuf, nam));
|
||||
ERR_I(CocPutVar(conn->varList, conn->cmdbuf, nam, 0));
|
||||
if (!setmode) ERR_I(str_put_str(conn->cmdbuf, "]"));
|
||||
} else {
|
||||
var=CocFindVar(conn->varList, nam);
|
||||
var=CocFindVar(conn->varList, nam, NULL);
|
||||
if (var==NULL) ERR_MSG("variable not found");
|
||||
buf_put_str(conn->cmdbuf, nam);
|
||||
ERR_I(str_put_str(conn->cmdbuf, nam));
|
||||
}
|
||||
s=t+1;
|
||||
} while (*t!='\0');
|
||||
|
||||
buf_put_end(conn->cmdbuf);
|
||||
if (conn->cmdbuf->buf==NULL) ERR_MSG("out buffer full");
|
||||
cnt=3;
|
||||
while (1) {
|
||||
cnt--;
|
||||
@@ -148,7 +149,7 @@ int CocCmd(CocConn *conn, const char *rwList)
|
||||
if (ErrCode!=ECONNRESET && ErrCode!=EPIPE) goto OnError;
|
||||
ErrShow("try again, error was");
|
||||
}
|
||||
err=buf_get_str(conn->resbuf);
|
||||
ERR_P(err=str_get_str(conn->resbuf, NULL));
|
||||
if (*err!='\0') { ErrMsg(err); ErrTxt(": (response from server)",0 ); goto OnError; }
|
||||
|
||||
/* read values */
|
||||
@@ -166,11 +167,11 @@ int CocCmd(CocConn *conn, const char *rwList)
|
||||
if (*(t-1)==']') setmode=0;
|
||||
} else {
|
||||
str_ncpy(nam, s, i+1);
|
||||
ERR_I(CocGetVar(conn->varList, conn->resbuf, nam));
|
||||
ERR_I(CocGetVar(conn->varList, conn->resbuf, nam, 0));
|
||||
}
|
||||
s=t+1;
|
||||
} while (*t!='\0');
|
||||
if (buf_size(conn->resbuf)!=0) ERR_MSG("syntax error");
|
||||
ERR_I(str_get_end(conn->resbuf));
|
||||
return(0);
|
||||
|
||||
OnError: return(-1);
|
||||
@@ -179,7 +180,7 @@ int CocCmd(CocConn *conn, const char *rwList)
|
||||
void CocCloseClient(CocConn *conn) {
|
||||
assert(conn!=NULL);
|
||||
close(conn->fd);
|
||||
buf_free(conn->cmdbuf);
|
||||
buf_free(conn->resbuf);
|
||||
str_free_buf(conn->cmdbuf);
|
||||
str_free_buf(conn->resbuf);
|
||||
CocFreeVarList(&conn->varList);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user