poll test works @high speed
This commit is contained in:
@@ -24,7 +24,8 @@
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <usefull_macros.h>
|
#include <usefull_macros.h>
|
||||||
|
|
||||||
#define XYBUFSZ (2048)
|
// suppose that we ONLY poll data
|
||||||
|
#define XYBUFSZ (128)
|
||||||
|
|
||||||
struct{
|
struct{
|
||||||
int help;
|
int help;
|
||||||
@@ -79,66 +80,29 @@ static int op(const char *nm){
|
|||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int eolcnt(buf_t *buf){
|
|
||||||
if(!buf) return -1;
|
|
||||||
int cnt = 0;
|
|
||||||
for(int i = 0; i < buf->len; ++i)
|
|
||||||
if(buf->buf[i] == '\n') ++cnt;
|
|
||||||
return cnt;
|
|
||||||
}
|
|
||||||
// move last record (if any) into head of buffer
|
|
||||||
static void movelast(buf_t *buf){
|
|
||||||
FNAME();
|
|
||||||
if(!buf) return;
|
|
||||||
DBG("buf was: %s", buf->buf);
|
|
||||||
int cnt = eolcnt(buf);
|
|
||||||
char *E = strrchr(buf->buf, '\n');
|
|
||||||
int idx = -1;
|
|
||||||
if(E){
|
|
||||||
idx = ++E - buf->buf; // position of symbol after '\n'
|
|
||||||
}else{
|
|
||||||
buf->len = strlen(buf->buf);
|
|
||||||
DBG("leave as is (%s)", buf->buf);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
DBG("cnt=%d, idx=%d", cnt, idx);
|
|
||||||
switch(cnt){
|
|
||||||
case 0: // EOL not found - clear buf
|
|
||||||
buf->len = 0;
|
|
||||||
break;
|
|
||||||
case 1: // only one record - move all after '\n'
|
|
||||||
if(idx > 0 && idx < XYBUFSZ){
|
|
||||||
buf->len = XYBUFSZ - idx;
|
|
||||||
memmove(buf->buf, E, buf->len);
|
|
||||||
}else buf->len = 0;
|
|
||||||
break;
|
|
||||||
default: // more than one record - move
|
|
||||||
{
|
|
||||||
int i = idx - 2;
|
|
||||||
for(; i > -1; --i)
|
|
||||||
if(buf->buf[i] == '\n') break;
|
|
||||||
++i;
|
|
||||||
buf->len = XYBUFSZ - i;
|
|
||||||
memmove(buf->buf, &buf->buf[i], buf->len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buf->buf[buf->len] = 0;
|
|
||||||
DBG("MOVED; now buf[%d]=%s", buf->len, buf->buf);
|
|
||||||
}
|
|
||||||
// write to buffer next data portion; return FALSE in case of error
|
// write to buffer next data portion; return FALSE in case of error
|
||||||
static int readstrings(buf_t *buf, int fd){
|
static int readstrings(buf_t *buf, int fd){
|
||||||
|
FNAME();
|
||||||
if(!buf){WARNX("Empty buffer"); return FALSE;}
|
if(!buf){WARNX("Empty buffer"); return FALSE;}
|
||||||
int L = XYBUFSZ - buf->len;
|
int L = XYBUFSZ - buf->len;
|
||||||
if(L == 0){
|
if(L == 0){
|
||||||
DBG("len: %d", buf->len);
|
DBG("buffer overfull!", buf->len);
|
||||||
movelast(buf);
|
char *lastn = strrchr(buf->buf, '\n');
|
||||||
|
if(lastn){
|
||||||
|
fprintf(stderr, "BUFOVR: _%s_", buf->buf);
|
||||||
|
++lastn;
|
||||||
|
buf->len = XYBUFSZ - (lastn - buf->buf);
|
||||||
|
DBG("Memmove %d", buf->len);
|
||||||
|
memmove(lastn, buf->buf, buf->len);
|
||||||
|
buf->buf[buf->len] = 0;
|
||||||
|
}else buf->len = 0;
|
||||||
L = XYBUFSZ - buf->len;
|
L = XYBUFSZ - buf->len;
|
||||||
}
|
}
|
||||||
int got = read(fd, &buf->buf[buf->len], L);
|
int got = read(fd, &buf->buf[buf->len], L);
|
||||||
if(got < 0){
|
if(got < 0){
|
||||||
WARN("read()");
|
WARN("read()");
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}else if(got == 0) return TRUE;
|
}else if(got == 0){ DBG("NO data"); return TRUE; }
|
||||||
buf->len += got;
|
buf->len += got;
|
||||||
buf->buf[buf->len] = 0;
|
buf->buf[buf->len] = 0;
|
||||||
DBG("buf[%d]: %s", buf->len, buf->buf);
|
DBG("buf[%d]: %s", buf->len, buf->buf);
|
||||||
@@ -146,28 +110,22 @@ static int readstrings(buf_t *buf, int fd){
|
|||||||
}
|
}
|
||||||
// return TRUE if got, FALSE if no data found
|
// return TRUE if got, FALSE if no data found
|
||||||
static int getdata(buf_t *buf, long *out){
|
static int getdata(buf_t *buf, long *out){
|
||||||
if(!buf) return -1;
|
if(!buf || buf->len < 1) return FALSE;
|
||||||
// read record between last '\n' and previous (or start of string)
|
// read record between last '\n' and previous (or start of string)
|
||||||
int cnt = eolcnt(buf);
|
char *last = &buf->buf[buf->len - 1];
|
||||||
if(cnt < 1) return FALSE;
|
//DBG("buf: _%s_", buf->buf);
|
||||||
char *last = strrchr(buf->buf, '\n');
|
if(*last != '\n') return FALSE;
|
||||||
if(!last) return FALSE; // WTF?
|
|
||||||
*last = 0;
|
*last = 0;
|
||||||
char *prev = buf->buf;
|
//DBG("buf: _%s_", buf->buf);
|
||||||
if(cnt > 1) prev = strrchr(buf->buf, '\n') + 1;
|
char *prev = strrchr(buf->buf, '\n');
|
||||||
if(!prev) prev = buf->buf; // ??
|
if(!prev) prev = buf->buf;
|
||||||
if(out) *out = atol(prev);
|
else{
|
||||||
int l = strlen(++last);
|
fprintf(stderr, "MORETHANONE: _%s_", buf->buf);
|
||||||
if(l < XYBUFSZ){
|
++prev; // after last '\n'
|
||||||
buf->len = l;
|
|
||||||
if(l){
|
|
||||||
memmove(buf->buf, last, l);
|
|
||||||
DBG("moved: %s", buf->buf);
|
|
||||||
}else DBG("empty line");
|
|
||||||
}else{
|
|
||||||
buf->len = 0;
|
|
||||||
DBG("buffer clear");
|
|
||||||
}
|
}
|
||||||
|
if(out) *out = atol(prev);
|
||||||
|
// clear buffer
|
||||||
|
buf->len = 0;
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
// try to write '\n' asking new data portion; return FALSE if failed
|
// try to write '\n' asking new data portion; return FALSE if failed
|
||||||
@@ -177,7 +135,7 @@ static int asknext(int fd){
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
for(; i < 5; ++i){
|
for(; i < 5; ++i){
|
||||||
int l = write(fd, "\n", 1);
|
int l = write(fd, "\n", 1);
|
||||||
DBG("l=%d", l);
|
//DBG("l=%d", l);
|
||||||
if(1 == l) return TRUE;
|
if(1 == l) return TRUE;
|
||||||
usleep(100);
|
usleep(100);
|
||||||
}
|
}
|
||||||
@@ -204,14 +162,16 @@ int main(int argc, char **argv){
|
|||||||
t0x = t0y = tstart = sl_dtime();
|
t0x = t0y = tstart = sl_dtime();
|
||||||
DBG("Start");
|
DBG("Start");
|
||||||
do{ // main cycle
|
do{ // main cycle
|
||||||
if(poll(pfds, 2, 1) < 0){
|
if(poll(pfds, 2, 0) < 0){
|
||||||
WARN("poll()");
|
WARN("poll()");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(pfds[0].revents && POLLIN){
|
if(pfds[0].revents && POLLIN){
|
||||||
|
DBG("got X");
|
||||||
if(!readstrings(&xbuf, Xfd)) break;
|
if(!readstrings(&xbuf, Xfd)) break;
|
||||||
}
|
}
|
||||||
if(pfds[1].revents && POLLIN){
|
if(pfds[1].revents && POLLIN){
|
||||||
|
DBG("got Y");
|
||||||
if(!readstrings(&ybuf, Yfd)) break;
|
if(!readstrings(&ybuf, Yfd)) break;
|
||||||
}
|
}
|
||||||
double curt = sl_dtime();
|
double curt = sl_dtime();
|
||||||
|
|||||||
Reference in New Issue
Block a user