本文整理汇总了C++中sdsavail函数的典型用法代码示例。如果您正苦于以下问题:C++ sdsavail函数的具体用法?C++ sdsavail怎么用?C++ sdsavail使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sdsavail函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: catClientInfoString
/* Concatenate a string representing the state of a client in an human
* readable format, into the sds string 's'. */
sds catClientInfoString(sds s, client *client) {
char flags[16], events[3], *p;
int emask;
p = flags;
if (client->flags & CLIENT_CLOSE_AFTER_REPLY) *p++ = 'c';
if (client->flags & CLIENT_CLOSE_ASAP) *p++ = 'A';
if (p == flags) *p++ = 'N';
*p++ = '\0';
emask = client->fd == -1 ? 0 : aeGetFileEvents(server.el,client->fd);
p = events;
if (emask & AE_READABLE) *p++ = 'r';
if (emask & AE_WRITABLE) *p++ = 'w';
*p = '\0';
return sdscatfmt(s,
"id=%U fd=%i name=%s age=%I idle=%I flags=%s qbuf=%U qbuf-free=%U obl=%U cmd=%s",
(unsigned long long) client->id,
client->fd,
client->name ? (char*)client->name : "",
(long long)(server.unixtime - client->ctime),
(long long)(server.unixtime - client->lastinteraction),
flags,
(unsigned long long) sdslen(client->querybuf),
(unsigned long long) sdsavail(client->querybuf),
(unsigned long long) client->bufpos,
client->lastcmd ? client->lastcmd->name : "NULL");
}
开发者ID:icoulter,项目名称:workspace_tubii,代码行数:30,代码来源:networking.c
示例2: sdsMakeRoomFor
/**
* make room for addlen byte after s
* @s : the original string
* @addlen: the length we want to add
**/
static sds sdsMakeRoomFor(sds s, size_t addlen) {
/* @sh : point the the struct contains s
* @newsh : points the new struct
*/
struct sdshdr *sh, *newsh;
/* calculate the free space of sh */
size_t free = sdsavail(s);
size_t len, newlen;
/* if there are enough free space for addlen */
if (free >= addlen) return s;
len = sdslen(s);
sh = (void*) (s-(sizeof(struct sdshdr)));
/**
* allocate (len + addlen)*2 space
* we want (len + addlen)
* so we may not reallocate memory next time
**/
newlen = (len+addlen)*2;
/* realloc the memory */
newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);
#ifdef SDS_ABORT_ON_OOM
if (newsh == NULL) sdsOomAbort();
#else
if (newsh == NULL) return NULL;
#endif
/**
* update the struct , newsh->len doesn't change
**/
newsh->free = newlen - len;
return newsh->buf;
}
开发者ID:klion26,项目名称:redis-1.0-annotation,代码行数:36,代码来源:sds.c
示例3: redisReaderFeed
int redisReaderFeed(redisReader *r, const char *buf, size_t len) {
sds newbuf;
/* Return early when this reader is in an erroneous state. */
if (r->err)
return REDIS_ERR;
/* Copy the provided buffer. */
if (buf != NULL && len >= 1) {
/* Destroy internal buffer when it is empty and is quite large. */
if (r->len == 0 && sdsavail(r->buf) > 16*1024) {
sdsfree(r->buf);
r->buf = sdsempty();
r->pos = 0;
/* r->buf should not be NULL since we just free'd a larger one. */
assert(r->buf != NULL);
}
newbuf = sdscatlen(r->buf,buf,len);
if (newbuf == NULL) {
__redisReaderSetErrorOOM(r);
return REDIS_ERR;
}
r->buf = newbuf;
r->len = sdslen(r->buf);
}
return REDIS_OK;
}
开发者ID:esromneb,项目名称:hiredis,代码行数:31,代码来源:hiredis.c
示例4: main
int main(){
char hello[20]={"hello,world!"};
printf("hello:%s\n",hello);
printf("str new\n");
sds str=sdsnew(hello);
printf("str:%s\n",str);
printf("str len:%d\n",sdslen(str));
printf("str avail:%d\n",sdsavail(str));
printf("str cat\n");
sds newstr=sdscat(str,"this is strcat content");
printf("str:%s\n",str);
printf("str:%s\n",newstr);
printf("str len:%d\n",sdslen(newstr));
printf("str avail:%d\n",sdsavail(newstr));
printf("str cpy\n");
char* teststr="test a string";
sdscpy(newstr,teststr);
printf("str:%s\n",newstr);
printf("str len:%d\n",sdslen(newstr));
printf("str avail:%d\n",sdsavail(newstr));
printf("trim char \n");
sdstrim(newstr,"g");
printf("str:%s\n",newstr);
printf("sds range \n");
sdsrange(newstr,1,3);
printf("str:%s\n",newstr);
printf("sds toupper \n");
sdstoupper(newstr);
printf("str:%s\n",newstr);
printf("sds tolower \n");
sdstolower(newstr);
printf("str:%s\n",newstr);
printf("sds cmp \n");
sds cmpstr=sdsnew("est");
printf("str:%d\n",sdscmp(newstr,cmpstr));
return 0;
}
开发者ID:prownd,项目名称:ccufl,代码行数:47,代码来源:sds_test.c
示例5: redisReplyReaderGetReply
int redisReplyReaderGetReply(void *reader, void **reply) {
redisReader *r = reader;
if (reply != NULL) *reply = NULL;
/* When the buffer is empty, there will never be a reply. */
if (r->len == 0)
return REDIS_OK;
/* Set first item to process when the stack is empty. */
if (r->ridx == -1) {
r->rstack[0].type = -1;
r->rstack[0].elements = -1;
r->rstack[0].idx = -1;
r->rstack[0].obj = NULL;
r->rstack[0].parent = NULL;
r->rstack[0].privdata = r->privdata;
r->ridx = 0;
}
/* Process items in reply. */
while (r->ridx >= 0)
if (processItem(r) < 0)
break;
/* Discard the consumed part of the buffer. */
if (r->pos > 0) {
if (r->pos == r->len) {
/* sdsrange has a quirck on this edge case. */
sdsfree(r->buf);
r->buf = sdsempty();
} else {
r->buf = sdsrange(r->buf,r->pos,r->len);
}
r->pos = 0;
r->len = sdslen(r->buf);
}
/* Emit a reply when there is one. */
if (r->ridx == -1) {
void *aux = r->reply;
r->reply = NULL;
/* Destroy the buffer when it is empty and is quite large. */
if (r->len == 0 && sdsavail(r->buf) > 16*1024) {
sdsfree(r->buf);
r->buf = sdsempty();
r->pos = 0;
}
/* Check if there actually *is* a reply. */
if (r->error != NULL) {
return REDIS_ERR;
} else {
if (reply != NULL) *reply = aux;
}
}
return REDIS_OK;
}
开发者ID:sidorares,项目名称:redis-server-gem,代码行数:58,代码来源:hiredis.c
示例6: redisAssertWithInfo
/* Try to encode a string object in order to save space */
robj *tryObjectEncoding(robj *o) {
long value;
sds s = o->ptr;
size_t len;
if (o->encoding != REDIS_ENCODING_RAW)
return o; /* Already encoded */
/* It's not safe to encode shared objects: shared objects can be shared
* everywhere in the "object space" of Redis. Encoded objects can only
* appear as "values" (and not, for instance, as keys) */
if (o->refcount > 1) return o;
/* Currently we try to encode only strings */
redisAssertWithInfo(NULL,o,o->type == REDIS_STRING);
/* Check if we can represent this string as a long integer */
len = sdslen(s);
if (len > 21 || !string2l(s,len,&value)) {
/* We can't encode the object...
*
* Do the last try, and at least optimize the SDS string inside
* the string object to require little space, in case there
* is more than 10% of free space at the end of the SDS string.
*
* We do that for larger strings, using the arbitrary value
* of 32 bytes. This code was backported from the unstable branch
* where this is performed when the object is too large to be
* encoded as EMBSTR. */
if (len > 32 &&
o->encoding == REDIS_ENCODING_RAW &&
sdsavail(s) > len/10)
{
o->ptr = sdsRemoveFreeSpace(o->ptr);
}
/* Return the original object. */
return o;
}
/* Ok, this object can be encoded...
*
* Can I use a shared object? Only if the object is inside a given range
*
* Note that we also avoid using shared integers when maxmemory is used
* because every object needs to have a private LRU field for the LRU
* algorithm to work well. */
if (server.maxmemory == 0 && value >= 0 && value < REDIS_SHARED_INTEGERS) {
decrRefCount(o);
incrRefCount(shared.integers[value]);
return shared.integers[value];
} else {
o->encoding = REDIS_ENCODING_INT;
sdsfree(o->ptr);
o->ptr = (void*) value;
return o;
}
}
开发者ID:ducky-hong,项目名称:nbase-arc,代码行数:58,代码来源:object.c
示例7: sdsMakeRoomFor
static sds sdsMakeRoomFor(sds s,size_t addlen)
{
struct sdshdr*sh,*newsh;
size_t free=sdsavail(s);
size_t len,newlen;
if(free>=addlen) return s;
len=sdslen(s);
sh=(void*)(s-(sizeof(struct sdshdr)));
newlen=(len+addlen)*2;
newsh=realloc(sh,sizeof(struct sdshdr)+newlen+1);
if(newsh==NULL) sdsOomAbort();
newsh->free=newlen-len;
return newsh->buf;
}
开发者ID:fankeke,项目名称:simple-redis-client,代码行数:15,代码来源:sds.c
示例8: sdsMakeRoomFor
sds sdsMakeRoomFor(sds s, size_t addlen) {
size_t free = sdsavail(s);
size_t len, newlen;
if (free >= addlen) return s;
len = sdslen(s);
struct sdshdr *sh = (void*)(s - (sizeof(struct sdshdr)));
newlen = (len + addlen);
if (newlen < SDS_MAX_PREALLOC)
newlen *= 2;
else newlen += SDS_MAX_PREALLOC;
struct sdshdr *newsh = zrealloc(sh, sizeof(struct sdshdr) + newlen + 1);
if (newsh == NULL) return NULL;
newsh->free = newlen - len;
return newsh->buf;
}
开发者ID:Git-WillWang,项目名称:myRedis,代码行数:15,代码来源:sds.c
示例9: sdslen
// try encode a string value as integer.
value_t *tryValueEncoding(value_t *val) {
if (val->encoding == ENCODING_INT)
return val;
size_t len = sdslen(val->ptr);
long v;
if (len > 21 || !string2l(val->ptr, len, &v)) {
if (len > 32 && sdsavail(val->ptr) > len / 10) {
val->ptr = sdsRemoveFreeSpace(val->ptr);
}
return val;
} else {
val->encoding = ENCODING_INT;
sdsfree(val->ptr);
val->ptr = (void*) ((long) v);
}
}
开发者ID:yuyang0,项目名称:mdb,代码行数:17,代码来源:db.c
示例10: redisReplyReaderFeed
void redisReplyReaderFeed(void *reader, const char *buf, size_t len) {
redisReader *r = reader;
/* Copy the provided buffer. */
if (buf != NULL && len >= 1) {
/* Destroy internal buffer when it is empty and is quite large. */
if (r->len == 0 && sdsavail(r->buf) > 16*1024) {
sdsfree(r->buf);
r->buf = sdsempty();
r->pos = 0;
}
r->buf = sdscatlen(r->buf,buf,len);
r->len = sdslen(r->buf);
}
}
开发者ID:ambakshi,项目名称:redis,代码行数:16,代码来源:hiredis.c
示例11: sdsMakeRoomFor
/* Enlarge the free space at the end of the sds string so that the caller
* is sure that after calling this function can overwrite up to addlen
* bytes after the end of the string, plus one more byte for nul term.
*
* Note: this does not change the *length* of the sds string as returned
* by sdslen(), but only the free buffer space we have. */
sds sdsMakeRoomFor(sds s, size_t addlen) {
void *sh, *newsh;
size_t avail = sdsavail(s);
size_t len, newlen;
char type, oldtype = s[-1] & SDS_TYPE_MASK;
int hdrlen;
/* Return ASAP if there is enough space left. */
if (avail >= addlen) return s;
len = sdslen(s);
sh = (char*)s-sdsHdrSize(oldtype);
newlen = (len+addlen);
if (newlen < SDS_MAX_PREALLOC)
newlen *= 2;
else
newlen += SDS_MAX_PREALLOC;
type = sdsReqType(newlen);
/* Don't use type 5: the user is appending to the string and type 5 is
* not able to remember empty space, so sdsMakeRoomFor() must be called
* at every appending operation. */
if (type == SDS_TYPE_5) type = SDS_TYPE_8;
hdrlen = sdsHdrSize(type);
if (oldtype==type) {
newsh = s_realloc(sh, hdrlen+newlen+1);
if (newsh == NULL) return NULL;
s = (char*)newsh+hdrlen;
} else {
/* Since the header size changes, need to move the string forward,
* and can't use realloc */
newsh = s_malloc(hdrlen+newlen+1);
if (newsh == NULL) return NULL;
memcpy((char*)newsh+hdrlen, s, len+1);
s_free(sh);
s = (char*)newsh+hdrlen;
s[-1] = type;
sdssetlen(s, len);
}
sdssetalloc(s, newlen);
return s;
}
开发者ID:Ardnived,项目名称:flarum-chat-server,代码行数:50,代码来源:sds.c
示例12: main
int main(void) {
{
struct sdshdr *sh;
sds x = sdsempty();
test_cond("sdsempty() should be strlen 0",
strlen(x) == 0 && sdslen(x) == 0 && memcmp(x,"\0",1) == 0);
sdsfree(x);
x = sdsalloc(NULL, 2);
test_cond("Create a NULL string with reserved space 2 bytes",
sdslen(x) == 0 && sdsavail(x) == 2);
sdsfree(x);
}
test_report()
return 0;
}
开发者ID:clouddb,项目名称:idb.c,代码行数:19,代码来源:isdk_sds.c
示例13: sdsMakeRoomFor
static sds sdsMakeRoomFor(sds s, size_t addlen) {
struct sdshdr *sh, *newsh;
size_t free = sdsavail(s);
size_t len, newlen;
if (free >= addlen) return s;
len = sdslen(s);
sh = (void*) (s-(sizeof(struct sdshdr)));
newlen = (len+addlen)*2;
newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);
#ifdef SDS_ABORT_ON_OOM
if (newsh == NULL) sdsOomAbort();
#else
if (newsh == NULL) return NULL;
#endif
newsh->free = (int)(newlen - len);
return newsh->buf;
}
开发者ID:mdavid,项目名称:nodejs-redis-azure,代码行数:19,代码来源:sds.c
示例14: sdsMakeRoomFor
/* Enlarge the free space at the end of the sds string so that the caller
* is sure that after calling this function can overwrite up to addlen
* bytes after the end of the string, plus one more byte for nul term.
*
* Note: this does not change the *length* of the sds string as returned
* by sdslen(), but only the free buffer space we have. */
sds sdsMakeRoomFor(sds s, size_t addlen) {
if (s == NULL) return NULL;
sdshdr *sh, *newsh;
size_t free = sdsavail(s);
size_t len, newlen;
if (free >= addlen) return s;
len = sdslen(s);
sh = sds_start(s);
newlen = (len+addlen);
if (newlen < SDS_MAX_PREALLOC)
newlen *= 2;
else
newlen += SDS_MAX_PREALLOC;
newsh = (sdshdr*) realloc(sh, sizeof *newsh+newlen+1);
if (newsh == NULL) return NULL;
newsh->free = newlen - len;
return newsh->buf;
}
开发者ID:asanosoyokaze,项目名称:sds,代码行数:27,代码来源:sds.c
示例15: sdsMakeRoomFor
/*
* 对 sds 中 buf 的长度进行扩展,确保在函数执行之后,
* buf 至少会有 addlen + 1 长度的空余空间
* (额外的 1 字节是为 \0 准备的)
*
* 返回值
* sds :扩展成功返回扩展后的 sds
* 扩展失败返回 NULL
*
* 复杂度
* T = O(N)
*/
sds sdsMakeRoomFor(sds s, size_t addlen) {
struct sdshdr *sh, *newsh;
// 获取 s 目前的空余空间长度
size_t free = sdsavail(s);
size_t len, newlen;
// s 目前的空余空间已经足够,无须再进行扩展,直接返回
if (free >= addlen) return s;
// 获取 s 目前已占用空间的长度
len = sdslen(s);
sh = (void*) (s-(sizeof(struct sdshdr)));
// s 最少需要的长度
newlen = (len+addlen);
// 根据新长度,为 s 分配新空间所需的大小
if (newlen < SDS_MAX_PREALLOC)
// 如果新长度小于 SDS_MAX_PREALLOC
// 那么为它分配两倍于所需长度的空间
newlen *= 2;
else
// 否则,分配长度为目前长度加上 SDS_MAX_PREALLOC
newlen += SDS_MAX_PREALLOC;
// T = O(N)
newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);
// 内存不足,分配失败,返回
if (newsh == NULL) return NULL;
// 更新 sds 的空余长度
newsh->free = newlen - len;
// 返回 sds
return newsh->buf;
}
开发者ID:DevinLow,项目名称:Reading-and-comprehense-redis-2.9.11,代码行数:51,代码来源:sds.c
示例16: sdsMakeRoomFor
sds sdsMakeRoomFor(sds s, size_t addlen) {
struct sdshdr *sh, *newsh;
size_t free = sdsavail(s);
size_t len, newlen;
if (free >= addlen) return s;
len = sdslen(s);
sh = (void*) (s-(sizeof(struct sdshdr)));
newlen = (len+addlen);
if (newlen < SDS_MAX_PREALLOC)
newlen *= 2;
else
newlen += SDS_MAX_PREALLOC;
newsh = realloc(sh, sizeof(struct sdshdr)+newlen+1);
#ifdef SDS_ABORT_ON_OOM
if (newsh == NULL) sdsOomAbort();
#else
if (newsh == NULL) return NULL;
#endif
newsh->free = newlen - len;
return newsh->buf;
}
开发者ID:truongminh,项目名称:miniweb,代码行数:23,代码来源:sds.c
示例17: sdsMakeRoomFor
/* 在原有字符串中取得更大的空间,并返回扩展空间后的字符串 */
sds sdsMakeRoomFor(sds s, size_t addlen) {
struct sdshdr *sh, *newsh;
//获取当前字符串的可用长度
size_t free = sdsavail(s);
size_t len, newlen;
//如果当前可用空间已经大于需要值,直接返回原字符串
if (free >= addlen) return s;
len = sdslen(s);
sh = (void*) (s-(sizeof(struct sdshdr)));
//计算要获取新字符串所要的长度大小=原长度+addlen
newlen = (len+addlen);
if (newlen < SDS_MAX_PREALLOC)
newlen *= 2;
else
newlen += SDS_MAX_PREALLOC;
newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);
if (newsh == NULL) return NULL;
//新字符串可用空间等于新长度减去原使用的长度
newsh->free = newlen - len;
//返回洗字符串中的buf字符串数组
return newsh->buf;
}
开发者ID:AplayER,项目名称:Redis-Code,代码行数:25,代码来源:sds.c
示例18: clientThread
static void *
clientThread (void *arg)
{
client_t *c = (client_t *) arg;
fd_set rfds, wfds;
int ret;
c->querybuf = sdsMakeRoomFor (sdsempty (), DEFAULT_QUERY_BUF_SIZE);
c->replybuf = sdsMakeRoomFor (sdsempty (), DEFAULT_QUERY_BUF_SIZE);
c->argc = 0;
c->argv = NULL;
c->argvlen = NULL;
c->reqtype = 0;
c->multibulklen = 0;
c->bulklen = -1;
c->rqst = arc_create_request ();
c->flags = 0;
c->total_append_command = 0;
FD_ZERO (&rfds);
FD_ZERO (&wfds);
while (1)
{
struct timeval timeout;
FD_CLR (c->fd, &rfds);
FD_CLR (c->fd, &wfds);
if (!(c->flags & REDIS_CLOSE_AFTER_REPLY))
FD_SET (c->fd, &rfds);
if (sdslen (c->replybuf) > 0)
FD_SET (c->fd, &wfds);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
ret = select (c->fd + 1, &rfds, &wfds, NULL, &timeout);
if (ret == -1)
{
perror ("select");
freeClient (c);
}
if (server.shutdown_signal)
{
c->flags |= REDIS_CLOSE_AFTER_REPLY;
}
/* readable */
if (FD_ISSET (c->fd, &rfds))
{
int pos = sdslen (c->querybuf);
int avail = sdsavail (c->querybuf);
ssize_t nread;
if (avail == 0)
{
c->querybuf =
sdsMakeRoomFor (c->querybuf, sdslen (c->querybuf));
avail = sdsavail (c->querybuf);
}
nread = read (c->fd, c->querybuf + pos, avail);
if (nread > 0)
{
sdsIncrLen (c->querybuf, nread);
processInputBuffer (c);
if (c->total_append_command)
{
int arc_errno, arc_be_errno, be_errno;
arc_ref_t *arc_ref;
arc_ref = acquire_arc_ref ();
ret =
arc_do_request (arc_ref->arc, c->rqst,
server.query_timeout_millis, &be_errno);
if (ret == -1)
{
arc_errno = errno;
arc_be_errno = be_errno;
}
else
{
ret = processReply (c, &be_errno);
if (ret == -1)
{
arc_errno = errno;
arc_be_errno = be_errno;
}
}
arc_free_request (c->rqst);
release_arc_ref (arc_ref);
c->rqst = arc_create_request ();
if (ret == -1)
{
if (arc_errno == ARC_ERR_TIMEOUT
|| (arc_errno == ARC_ERR_BACKEND
&& arc_be_errno == ARC_ERR_TIMEOUT))
{
//.........这里部分代码省略.........
开发者ID:naver,项目名称:nbase-arc,代码行数:101,代码来源:local_proxy.c
示例19: sdscatfmt
/* This function is similar to sdscatprintf, but much faster as it does
* not rely on sprintf() family functions implemented by the libc that
* are often very slow. Moreover directly handling the sds string as
* new data is concatenated provides a performance improvement.
*
* However this function only handles an incompatible subset of printf-alike
* format specifiers:
*
* %s - C String
* %S - SDS string
* %i - signed int
* %I - 64 bit signed integer (long long, int64_t)
* %u - unsigned int
* %U - 64 bit unsigned integer (unsigned long long, uint64_t)
* %% - Verbatim "%" character.
*/
sds sdscatfmt(sds s, char const *fmt, ...) {
size_t initlen = sdslen(s);
const char *f = fmt;
int i;
va_list ap;
va_start(ap,fmt);
f = fmt; /* Next format specifier byte to process. */
i = initlen; /* Position of the next byte to write to dest str. */
while(*f) {
char next, *str;
size_t l;
long long num;
unsigned long long unum;
/* Make sure there is always space for at least 1 char. */
if (sdsavail(s)==0) {
s = sdsMakeRoomFor(s,1);
}
switch(*f) {
case '%':
next = *(f+1);
f++;
switch(next) {
case 's':
case 'S':
str = va_arg(ap,char*);
l = (next == 's') ? strlen(str) : sdslen(str);
if (sdsavail(s) < l) {
s = sdsMakeRoomFor(s,l);
}
memcpy(s+i,str,l);
sdsinclen(s,l);
i += l;
break;
case 'i':
case 'I':
if (next == 'i')
num = va_arg(ap,int);
else
num = va_arg(ap,long long);
{
char buf[SDS_LLSTR_SIZE];
l = sdsll2str(buf,num);
if (sdsavail(s) < l) {
s = sdsMakeRoomFor(s,l);
}
memcpy(s+i,buf,l);
sdsinclen(s,l);
i += l;
}
break;
case 'u':
case 'U':
if (next == 'u')
unum = va_arg(ap,unsigned int);
else
unum = va_arg(ap,unsigned long long);
{
char buf[SDS_LLSTR_SIZE];
l = sdsull2str(buf,unum);
if (sdsavail(s) < l) {
s = sdsMakeRoomFor(s,l);
}
memcpy(s+i,buf,l);
sdsinclen(s,l);
i += l;
}
break;
default: /* Handle %% and generally %<unknown>. */
s[i++] = next;
sdsinclen(s,1);
break;
}
break;
default:
s[i++] = *f;
sdsinclen(s,1);
break;
}
开发者ID:Ardnived,项目名称:flarum-chat-server,代码行数:97,代码来源:sds.c
示例20: se_try_read
static int se_try_read(lua_State *L, int fd, int size, sds *pcache)
{
char sbuf[4 << 10];
char *cache = *pcache;
char *buf;
int bufsize;
int nread;
if (cache) {
bufsize = sdsavail(cache);
buf = cache + sdslen(cache);
printf("continue try read: %d / %d\n", bufsize, size);
} else { // first try
bufsize = size > 0 ? size : size < 0 ? -size : sizeof(sbuf);
if (bufsize <= sizeof(sbuf)) {
buf = sbuf;
} else {
cache = sdsnewlen(NULL, bufsize);
oom_check(cache);
sdsclear(cache);
*pcache = cache;
buf = cache;
}
printf("try read: %d / %d\n", bufsize, size);
}
nread = read(fd, buf, bufsize);
if (nread > 0) {
if (size <= 0 || nread == bufsize) { // done
if (cache) {
lua_pushlstring(L, cache, sdslen(cache) + nread);
sdsfree(cache);
*pcache = NULL;
} else {
lua_pushlstring(L, buf, nread);
}
printf("read done: %d / %d / %d\n", nread, bufsize, size);
return 1;
}
// partial read
if (!cache) {
cache = sdsnewlen(NULL, bufsize);
oom_check(cache);
sdsclear(cache);
*pcache = cache;
memcpy(cache, buf, nread);
}
sdsIncrLen(cache, nread);
printf("partial read: %d / %d / %d\n", nread, bufsize, size);
return -1;
}
if (nread == 0)
return se_read_error(L, pcache, "EOF");
if (errno == EAGAIN || errno == EWOULDBLOCK)
return -1;
se_assert(L, errno != EBADF, "read(%d) error", fd);
return se_read_error(L, pcache, strerror(errno));
}
开发者ID:itgb,项目名称:ap_apps,代码行数:61,代码来源:se_core.c
注:本文中的sdsavail函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论