Go to the documentation of this file.
96 if (sl->
len+size >
sizeof(sl->
buf)) {
192 assert(len <= 65535);
207 size =
sizeof(sl->
buf)-sl->
len;
209 va_start(arg, format);
210 n = vsnprintf((
char *)sl->
buf+sl->
len, size, format, arg);
213 if (n <= -1 || (
size_t)n >= size) {
214 LOG(
llevError,
"Truncating message exceeding MAXSOCKBUF. The message was:\n%s\n", sl->
buf+sl->
len);
216 sl->
len += (size_t)n;
247 return sizeof(sl->
buf)-sl->
len;
282 stat = recv(fd,
reinterpret_cast<char *
>(sl->
buf+sl->
len), 2-sl->
len, 0);
286 stat = read(fd, sl->
buf+sl->
len, 2-sl->
len);
287 }
while ((stat == -1) && (errno == EINTR));
294 if ((stat == -1) && WSAGetLastError() != WSAEWOULDBLOCK) {
295 if (WSAGetLastError() == WSAECONNRESET)
298 LOG(
llevDebug,
"ReadPacket got error %d, returning -1\n", WSAGetLastError());
303 if (errno == ECONNRESET) {
304 LOG(
llevDebug,
"ReadPacket got error %s, returning -1\n", strerror(errno));
307 if (errno != EAGAIN && errno != EWOULDBLOCK) {
308 LOG(
llevDebug,
"ReadPacket got error %s, returning 0\n", strerror(errno));
326 toread = 2+(sl->
buf[0]<<8)+sl->
buf[1]-sl->
len;
327 if ((toread+(
int)sl->
len) >= len) {
328 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);
334 stat = recv(fd,
reinterpret_cast<char *
>(sl->
buf+2), 100, 0);
336 stat = read(fd, sl->
buf+2, 100);
345 stat = recv(fd,
reinterpret_cast<char *
>(sl->
buf+sl->
len), toread, 0);
348 stat = read(fd, sl->
buf+sl->
len, toread);
349 }
while ((stat < 0) && (errno == EINTR));
353 if ((stat == -1) && WSAGetLastError() != WSAEWOULDBLOCK) {
354 if (WSAGetLastError() == WSAECONNRESET)
357 LOG(
llevDebug,
"ReadPacket got error %d, returning -1\n", WSAGetLastError());
362 if (errno != EAGAIN && errno != EWOULDBLOCK) {
363 LOG(
llevDebug,
"ReadPacket got error %s, returning 0\n", strerror(errno));
379 LOG(
llevError,
"SockList_ReadPacket: Read more bytes than desired.\n");
382 }
while (toread > 0);
401 LOG(
llevDebug,
"Write_To_Socket called with dead socket\n");
406 const int amt =
send(ns->
fd,
reinterpret_cast<const char *
>(
buf), len, 0);
408 const int amt =
send(ns->
fd,
buf, len, 0);
412 if (amt == -1 && WSAGetLastError() != WSAEWOULDBLOCK) {
413 LOG(
llevInfo,
"socket write failed: error code %d, disconnecting client\n",
416 if (errno != EWOULDBLOCK) {
417 LOG(
llevInfo,
"socket write failed: %s, disconnecting client\n",
424 "Write_To_Socket: write would block; disconnecting. Try "
425 "increasing SOCKETBUFSIZE.\n");
429 }
else if (amt != len) {
430 LOG(
llevError,
"Write_To_Socket: write wrote less than requested; "
431 "disconnecting. Try increasing SOCKETBUFSIZE.\n");
451 sl->
buf[0] = ((sl->
len-2)>>8)&0xFF;
452 sl->
buf[1] = (sl->
len-2)&0xFF;
464 static int count_all_players() {
481 #define STAT(name, fmt, val) fprintf(f, "%s %" fmt "\n", name, val);
487 time_t now = time(NULL);
489 int players_total = count_all_players();
492 LOG(
llevInfo,
"STAT: players: %d active, %d total\n", players_active, players_total);
493 LOG(
llevInfo,
"CSSTAT: %.16s tot %d %d %d %ld inc %d %d %d %ld\n",
508 STAT(
"players_total",
"d", players_total);
509 STAT(
"players_active",
"d", players_active);
510 STAT(
"ticks_overtime_percent",
"f", ticks_over * 100);
512 STAT(
"ticktime_avg",
"f", avg / 1e3);
516 int maps_in_memory = 0;
517 int maps_swapped = 0;
519 switch (
m->in_memory) {
528 STAT(
"maps_in_memory",
"d", maps_in_memory);
531 STAT(
"maps_swapped",
"d", maps_swapped);
void SockList_AddShort(SockList *sl, uint16_t data)
int object_count_active(void)
unsigned long total_ticktime
void Send_With_Handling(socket_struct *ns, SockList *sl)
void SockList_NullTerminate(SockList *sl)
void LOG(LogLevel logLevel, const char *format,...)
int of_close(OutputFile *of)
FILE * of_open(OutputFile *of, const char *fname)
short GetShort_String(const unsigned char *data)
void SockList_AddData(SockList *sl, const void *data, size_t len)
void SockList_Reset(SockList *sl)
static void SockList_Ensure(const SockList *sl, size_t size)
void SockList_Term(SockList *sl)
static event_registration m
char * stringbuffer_finish(StringBuffer *sb)
void SockList_Init(SockList *sl)
void SockList_AddLen8Data(SockList *sl, const void *data, size_t len)
void SockList_AddChar(SockList *sl, unsigned char data)
void fatal(enum fatal_error err)
unsigned long max_ticktime
void SockList_AddInt(SockList *sl, uint32_t data)
std::vector< archetype * > players
void write_cs_stats(void)
void SockList_AddStringBuffer(SockList *sl, StringBuffer *sb)
int SockList_ReadPacket(int fd, SockList *sl, int len)
size_t SockList_Avail(const SockList *sl)
static void Write_To_Socket(socket_struct *ns, const unsigned char *buf, const int len)
void reset_stats(struct CS_Stats *stats)
void SockList_AddInt64(SockList *sl, uint64_t data)
void SockList_ResetRead(SockList *sl)
void SockList_AddLen16Data(SockList *sl, const void *data, size_t len)
unsigned long ticks_overtime
unsigned char buf[MAXSOCKBUF]
void SockList_AddPrintf(SockList *sl, const char *format,...)
void SockList_AddString(SockList *sl, const char *data)
int GetInt_String(const unsigned char *data)