*** empty log message ***

This commit is contained in:
cvs
2000-03-31 07:51:28 +00:00
parent 72589129c3
commit 24eb75d7d3
24 changed files with 1184 additions and 670 deletions

View File

@@ -5,17 +5,24 @@
#include <sys/time.h>
#include <sys/timeb.h>
#include <sys/socket.h>
#include <strings.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include "errhdl.h"
#include "logfile.h"
#include "server.h"
#include "util.h"
#include "str_util.h"
static buf_type *buf, *bufo;
static Str_Buf *buf, *bufo;
static fd_set mask, rmask;
static int maxfd;
struct CocClient *cList, *cLastCmd=NULL;
static int mainFd;
static int modified;
int CocInitServer(int bufsize, int port) {
int i;
@@ -23,8 +30,8 @@ int CocInitServer(int bufsize, int port) {
char *err;
if (bufsize==0) bufsize=1024;
buf=buf_create(bufsize);
bufo=buf_create(bufsize);
ERR_P(buf=str_create_buf(bufsize, '\0'));
ERR_P(bufo=str_create_buf(bufsize,'\0'));
cList=malloc(sizeof(*cList)); /* empty header */
/* first try to connect to an existing server */
@@ -48,18 +55,18 @@ int CocHandle1Request(int tmo_msec, int fd) {
struct hostent *h;
struct timeval tmo={0,1};
struct CocClient *cl, *cl0;
int i, newfd, setmode, status;
int i, newfd, setmode;
size_t cadrlen;
char *err, *varname;
status=3;
rmask=mask;
if (fd>0) FD_SET(fd, &rmask);
tmo.tv_sec=tmo_msec / 1000;
tmo.tv_usec=(tmo_msec % 1000)*1000+1;
if (fd>=maxfd) maxfd=fd+1;
ERR_SI(i=select(maxfd,&rmask,NULL,NULL,&tmo));
if (fd>0 && FD_ISSET(fd, &rmask)) return(1);
if (fd>0 && FD_ISSET(fd, &rmask)) return(1); /* event on fd */
if (i==0) return(0); /* timeout */
if (FD_ISSET(mainFd, &rmask)) {
ERR_SI(newfd=accept(mainFd, (struct sockaddr *)&cadr, &cadrlen));
@@ -82,8 +89,8 @@ int CocHandle1Request(int tmo_msec, int fd) {
cl0=cList; cl=cl0->next;
while (cl!=NULL) {
if (FD_ISSET(cl->fd, &rmask)) {
buf->usize=recv(cl->fd, buf->start, buf->isize, 0);
if (buf->usize<=0) {
buf->wrpos=recv(cl->fd, buf->buf, buf->dsize, 0);
if (buf->wrpos<=0) {
logfileOut(LOG_MAIN, "(%d) disconnected\n",cl->fd);
close(cl->fd);
FD_CLR(cl->fd, &mask);
@@ -91,29 +98,26 @@ int CocHandle1Request(int tmo_msec, int fd) {
free(cl);
cl=cl0;
} else {
if (buf->usize<0) ERR_MSG("usize negative");
buf_put_start(bufo);
buf_put_str(bufo, ""); /* empty error message */
str_put_start(bufo);
str_get_start(buf);
ERR_I(str_put_str(bufo, "")); /* empty error message */
setmode=0;
buf_reset(buf);
err=NULL;
varname=buf_get_str(buf);
ERR_P(varname=str_get_str(buf, NULL));
logfileOut(LOG_NET, "(%d) ", cl->fd);
if (varname[0]=='#') { /* access code */
if (0==strcmp(varname,"#rdacc")) {
logfileOut(LOG_MAIN, " set read mode\n");
logfileOut(LOG_MAIN, "set read mode\n");
cl->mode=1;
} else if (0==strcmp(varname,"#rwacs")) {
logfileOut(LOG_MAIN, " set write mode\n");
logfileOut(LOG_MAIN, "set write mode\n");
cl->mode=2;
} else {
err="bad access code";
}
buf_put_end(bufo);
} else if (cl->mode==0) {
err="no access";
buf_put_end(bufo);
} else {
while (1) {
if (varname[0]=='[') {
@@ -123,52 +127,50 @@ int CocHandle1Request(int tmo_msec, int fd) {
setmode=0;
} else if (setmode) {
if (0==strcmp("$", varname)) { /* special case: command */
str_copy(cl->cmd, buf_get_str(buf));
ERR_P(str_get_str(buf, cl->cmd));
cl->res[0]='\0';
} else {
i=CocGetVar(serverVarList, buf, varname);
i=CocGetVar(serverVarList, buf, varname, 1);
if (i<0) { err=ErrMessage; break; }
}
status=2;
modified=1;
} else {
if (0==strcmp("$", varname)) { /* special case: response */
buf_put_str(bufo, cl->res);
ERR_I(str_put_str(bufo, cl->res));
cl->res[0]='\0';
} else {
i=CocPutVar(serverVarList, bufo, varname);
i=CocPutVar(serverVarList, bufo, varname, 0);
if (i<0) { err=ErrMessage; break; }
}
}
i=buf_size(buf);
if (i<=0) {
if (i<0) err="type mismatch";
if (buf->rdpos>=buf->wrpos) {
ERR_I(str_get_end(buf));
break;
}
varname=buf_get_str(buf);
if (buf_size(buf)<0) { err="syntax error"; break; }
ERR_P(varname=str_get_str(buf, NULL));
}
buf_reset(buf);
str_get_start(buf);
logfileOutBuf(LOG_NET, buf);
buf_put_end(bufo);
str_get_start(bufo);
logfileOut(LOG_NET, " |");
logfileOutBuf(LOG_NET, bufo);
}
if (err==NULL) {
logfileOut(LOG_NET, "\n");
} else {
buf_put_start(bufo); /* reset output */
buf_put_str(bufo, err); /* put error message */
buf_put_end(bufo);
str_put_start(bufo); /* reset output */
str_put_str(bufo, err); /* put error message */
logfileOut(LOG_NET, " (%s)\n", err);
logfileMask(LOG_NET);
}
ERR_SI(send(cl->fd, bufo->buf, bufo->size, 0));
ERR_SI(send(cl->fd, bufo->buf, bufo->wrpos, 0));
}
}
cl0=cl; cl=cl->next;
}
}
return(status);
if (modified) return(2);
return(3);
OnError: return(-1);
}
@@ -176,12 +178,20 @@ int CocHandleRequests(int tmo_msec, int fd) {
struct timeb tim1, tim0;
int tdif, iret;
if (modified && fd==0) { /* earlier modification */
modified=0;
return(2);
}
ftime(&tim0);
tdif=tmo_msec;
while (tdif>=0) {
ERR_I(iret=CocHandle1Request(tdif, fd));
if (iret==2 && fd==0) return(iret); /* a variable was changed */
if (iret!=3) return(iret); /* event on fd or timeout */
if (fd==0) {
if (iret==2) return(2); /* modification of a varaible */
} else {
if (iret==1) return(1); /* event on fd */
}
if (iret==0) return(0); /* timeout */
ftime(&tim1);
tdif=tmo_msec-((tim1.time-tim0.time)*1000+tim1.millitm-tim0.millitm);
}
@@ -223,6 +233,6 @@ void CocCloseServer() {
}
free(cList);
close(mainFd);
buf_free(buf); buf_free(bufo);
str_free_buf(buf); str_free_buf(bufo);
logfileClose();
}