105 sl->
buf[sl->
len++] = data;
115 sl->
buf[sl->
len++] = (data>>8)&0xff;
116 sl->
buf[sl->
len++] = data&0xff;
126 sl->
buf[sl->
len++] = (data>>24)&0xff;
127 sl->
buf[sl->
len++] = (data>>16)&0xff;
128 sl->
buf[sl->
len++] = (data>>8)&0xff;
129 sl->
buf[sl->
len++] = data&0xff;
139 sl->
buf[sl->
len++] = (char)((data>>56)&0xff);
140 sl->
buf[sl->
len++] = (char)((data>>48)&0xff);
141 sl->
buf[sl->
len++] = (char)((data>>40)&0xff);
142 sl->
buf[sl->
len++] = (char)((data>>32)&0xff);
143 sl->
buf[sl->
len++] = (char)((data>>24)&0xff);
144 sl->
buf[sl->
len++] = (char)((data>>16)&0xff);
145 sl->
buf[sl->
len++] = (char)((data>>8)&0xff);
146 sl->
buf[sl->
len++] = (char)(data&0xff);
166 memcpy(sl->
buf+sl->
len, data, len);
192 size =
sizeof(sl->
buf)-sl->
len;
194 va_start(arg, format);
198 if (n <= -1 || (
size_t)n >= size) {
201 sl->
len += (size_t)n;
232 return sizeof(sl->
buf)-sl->
len;
240 return ((data[0]<<24)+(data[1]<<16)+(data[2]<<8)+data[3]);
244 return ((data[0]<<8)+data[1]);
268 stat = recv(fd, sl->
buf+sl->
len, 2-sl->
len, 0);
272 stat = read(fd, sl->
buf+sl->
len, 2-sl->
len);
273 }
while ((stat == -1) && (errno == EINTR));
280 if ((stat == -1) && WSAGetLastError() != WSAEWOULDBLOCK) {
281 if (WSAGetLastError() == WSAECONNRESET)
284 LOG(
llevDebug,
"ReadPacket got error %d, returning -1\n", WSAGetLastError());
289 if (errno == ECONNRESET) {
293 if (errno != EAGAIN && errno != EWOULDBLOCK) {
312 toread = 2+(sl->
buf[0]<<8)+sl->
buf[1]-sl->
len;
313 if ((toread+(
int)sl->
len) >= len) {
314 LOG(
llevError,
"SockList_ReadPacket: Want to read more bytes than will fit in buffer (%lu>=%lu).\n", (
unsigned long)toread+sl->
len, (
unsigned long)len);
320 recv(fd, sl->
buf+2, 100, 0);
322 read(fd, sl->
buf+2, 100);
330 stat = recv(fd, sl->
buf+sl->
len, toread, 0);
333 stat = read(fd, sl->
buf+sl->
len, toread);
334 }
while ((stat < 0) && (errno == EINTR));
338 if ((stat == -1) && WSAGetLastError() != WSAEWOULDBLOCK) {
339 if (WSAGetLastError() == WSAECONNRESET)
342 LOG(
llevDebug,
"ReadPacket got error %d, returning -1\n", WSAGetLastError());
347 if (errno != EAGAIN && errno != EWOULDBLOCK) {
364 LOG(
llevError,
"SockList_ReadPacket: Read more bytes than desired.\n");
367 }
while (toread > 0);
387 LOG(
llevDebug,
"Socket on fd %d has overrun internal buffer - marking as dead\n", ns->
fd);
422 LOG(
llevDebug,
"write_socket_buffer called when there is no data, fd=%d\n", ns->
fd);
436 }
while ((amt < 0) && (errno == EINTR));
442 if (amt == -1 && WSAGetLastError() != WSAEWOULDBLOCK) {
443 LOG(
llevError,
"New socket write failed (wsb) (%d).\n", WSAGetLastError());
445 if (errno != EWOULDBLOCK) {
479 const unsigned char *pos = buf;
483 LOG(
llevDebug,
"Write_To_Socket called with dead socket\n");
497 amt = send(ns->
fd, pos, len, 0);
500 amt = write(ns->
fd, pos, len);
501 }
while ((amt < 0) && (errno == EINTR));
506 if (amt == -1 && WSAGetLastError() != WSAEWOULDBLOCK) {
507 LOG(
llevError,
"New socket write failed WTS (%d).\n", WSAGetLastError());
509 if (errno != EWOULDBLOCK) {
523 }
else if (amt == 0) {
524 LOG(
llevError,
"Write_To_Socket: No data written out.\n");
545 sl->
buf[0] = ((sl->
len-2)>>8)&0xFF;
546 sl->
buf[1] = (sl->
len-2)&0xFF;
566 time_t now = time(NULL);
573 LOG(
llevInfo,
"CSSTAT: %.16s tot %d %d %d %ld inc %d %d %d %ld\n",
585 if (sl->
len+size >
sizeof(sl->
buf)) {
void SockList_AddData(SockList *sl, const void *data, size_t len)
void SockList_AddChar(SockList *sl, char data)
void write_socket_buffer(socket_struct *ns)
short GetShort_String(const unsigned char *data)
size_t SockList_Avail(const SockList *sl)
void SockList_Init(SockList *sl)
static void Write_To_Socket(socket_struct *ns, const unsigned char *buf, int len)
void SockList_AddStringBuffer(SockList *sl, StringBuffer *sb)
void SockList_AddLen8Data(SockList *sl, const void *data, size_t len)
void SockList_AddInt(SockList *sl, uint32 data)
unsigned char buf[2+65536UL+1]
buffer_struct outputbuffer
void SockList_AddInt64(SockList *sl, uint64 data)
static void SockList_Ensure(const SockList *sl, size_t size)
void SockList_NullTerminate(SockList *sl)
int GetInt_String(const unsigned char *data)
void SockList_AddPrintf(SockList *sl, const char *format,...)
void SockList_AddString(SockList *sl, const char *data)
static void add_to_buffer(socket_struct *ns, const unsigned char *buf, int len)
void Send_With_Handling(socket_struct *ns, SockList *sl)
void write_cs_stats(void)
void SockList_Term(SockList *sl)
void LOG(LogLevel logLevel, const char *format,...)
int SockList_ReadPacket(int fd, SockList *sl, int len)
void SockList_Reset(SockList *sl)
char * strerror_local(int errnum, char *buf, size_t size)
void SockList_ResetRead(SockList *sl)
void SockList_AddShort(SockList *sl, uint16 data)
char * stringbuffer_finish(StringBuffer *sb)