capmt: recode - use polling and write queue

This commit is contained in:
Jaroslav Kysela 2014-06-05 15:59:12 +02:00
parent 5ecd88305b
commit db59cf12a5
4 changed files with 662 additions and 324 deletions

File diff suppressed because it is too large Load diff

View file

@ -54,7 +54,7 @@ tcp_connect(const char *hostname, int port, const char *bindaddr,
int fd, r, res, err;
struct addrinfo *ai;
char portstr[6];
socklen_t errlen = sizeof(int);
socklen_t errlen = sizeof(err);
snprintf(portstr, 6, "%u", port);
res = getaddrinfo(hostname, portstr, NULL, &ai);
@ -159,6 +159,12 @@ tcp_connect(const char *hostname, int port, const char *bindaddr,
}
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
/* Set the keep-alive active */
err = 1;
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&err, errlen);
return fd;
}

View file

@ -472,7 +472,8 @@ typedef struct sbuf {
uint8_t *sb_data;
int sb_ptr;
int sb_size;
int sb_err;
unsigned int sb_err : 1;
unsigned int sb_bswap: 1;
} sbuf_t;
@ -651,6 +652,22 @@ void sbuf_put_byte(sbuf_t *sb, uint8_t u8);
ssize_t sbuf_read(sbuf_t *sb, int fd);
static inline uint8_t sbuf_peek_u8(sbuf_t *sb, int off) { return sb->sb_data[off]; }
static inline int8_t sbuf_peek_s8(sbuf_t *sb, int off) { return sb->sb_data[off]; }
uint16_t sbuf_peek_u16(sbuf_t *sb, int off);
static inline int16_t sbuf_peek_s16(sbuf_t *sb, int off) { return sbuf_peek_u16(sb, off); }
uint16_t sbuf_peek_u16le(sbuf_t *sb, int off);
static inline int16_t sbuf_peek_s16le(sbuf_t *sb, int off) { return sbuf_peek_u16le(sb, off); }
uint16_t sbuf_peek_u16be(sbuf_t *sb, int off);
static inline int16_t sbuf_peek_s16be(sbuf_t *sb, int off) { return sbuf_peek_u16be(sb, off); }
uint32_t sbuf_peek_u32(sbuf_t *sb, int off);
static inline int32_t sbuf_peek_s32(sbuf_t *sb, int off) { return sbuf_peek_u32(sb, off); }
uint32_t sbuf_peek_u32le(sbuf_t *sb, int off);
static inline int32_t sbuf_peek_s32le(sbuf_t *sb, int off) { return sbuf_peek_u32le(sb, off); }
uint32_t sbuf_peek_u32be(sbuf_t *sb, int off);
static inline int32_t sbuf_peek_s32be(sbuf_t *sb, int off) { return sbuf_peek_u32be(sb, off); }
static inline uint8_t *sbuf_peek(sbuf_t *sb, int off) { return sb->sb_data + off; }
char *md5sum ( const char *str );
int makedirs ( const char *path, int mode );

View file

@ -25,8 +25,24 @@
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <endian.h>
#include "tvheadend.h"
#ifndef BYTE_ORDER
#define BYTE_ORDER __BYTE_ORDER
#endif
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN __LITTLE_ENDIAN
#endif
#ifndef BIG_ENDIAN
#define BIG_ENDIAN __BIG_ENDIAN
#endif
#if BYTE_ORDER == LITTLE_ENDIAN
#define ENDIAN_SWAP_COND(x) (!(x))
#else
#define ENDIAN_SWAP_COND(x) (x)
#endif
/**
* CRC32
*/
@ -390,6 +406,51 @@ sbuf_put_byte(sbuf_t *sb, uint8_t u8)
sbuf_append(sb, &u8, 1);
}
uint16_t sbuf_peek_u16(sbuf_t *sb, int off)
{
uint8_t *p = sb->sb_data + off;
if (ENDIAN_SWAP_COND(sb->sb_bswap))
return p[0] | (((uint16_t)p[1]) << 8);
else
return (((uint16_t)p[0]) << 8) | p[1];
}
uint16_t sbuf_peek_u16le(sbuf_t *sb, int off)
{
uint8_t *p = sb->sb_data + off;
return p[0] | (((uint16_t)p[1]) << 8);
}
uint16_t sbuf_peek_u16be(sbuf_t *sb, int off)
{
uint8_t *p = sb->sb_data + off;
return (((uint16_t)p[0]) << 8) | p[1];
}
uint32_t sbuf_peek_u32(sbuf_t *sb, int off)
{
uint8_t *p = sb->sb_data + off;
if (ENDIAN_SWAP_COND(sb->sb_bswap))
return p[0] | (((uint32_t)p[1]) << 8) |
(((uint32_t)p[2]) << 16) | (((uint32_t)p[3]) << 24);
else
return (((uint16_t)p[0]) << 24) | (((uint16_t)p[1]) << 16) |
(((uint16_t)p[2]) << 8) | p[3];
}
uint32_t sbuf_peek_u32le(sbuf_t *sb, int off)
{
uint8_t *p = sb->sb_data + off;
return p[0] | (((uint32_t)p[1]) << 8) |
(((uint32_t)p[2]) << 16) | (((uint32_t)p[3]) << 24);
}
uint32_t sbuf_peek_u32be(sbuf_t *sb, int off)
{
uint8_t *p = sb->sb_data + off;
return (((uint16_t)p[0]) << 24) | (((uint16_t)p[1]) << 16) |
(((uint16_t)p[2]) << 8) | p[3];
}
void
sbuf_cut(sbuf_t *sb, int off)