51 if (fd < 0)
return -EINVAL;
56 unsigned char *msg_ = msg;
60 n = write(fd, msg, m);
62 if (EINTR == errno )
continue;
63 if (errno != EWOULDBLOCK && errno != EAGAIN)
return -errno;
69 n = fcntl(fd, F_GETFL, 0);
70 if (-1 == n)
return -errno;
71 if (0 == (n & O_NONBLOCK)) {
73 dbg(ctx,
"Short write: %d bytes written instead of %d", (
int)(size - m), (
int)size);
81 n = select(fd, NULL, &myset, NULL, &to);
85 if (n < 0 && errno == EINTR)
continue;
87 if (n < 0 && m == size)
return -errno;
89 dbg(ctx,
"Write error %d: %d bytes written, instead of %d", -errno, (
int)(size-m), (
int)size);
93 if (n == 0 && m == size) {
98 dbg(ctx,
"Short write (timeout): %d bytes written instead of %d", (
int)(size-m), (
int) size);
105 unsigned int j = 0, k;
109 for (k = 0; k < size; k++, j++) {
111 dbg(ctx,
"> %s\n",buf);
115 sprintf(buf + strlen(buf),
" 0x%02x,", msg_[k]);
117 dbg(ctx,
"> %s\n",buf);
128 if (fd < 0)
return -EINVAL;
133 unsigned char *msg_ = msg;
137 n = read(fd, msg, m);
139 if(errno == EINTR)
continue;
140 if(errno != EWOULDBLOCK && errno != EAGAIN)
return -errno;
143 if (n == 0)
return size-m;
150 n = fcntl(fd, F_GETFL, 0);
151 if (-1 == n)
return -errno;
152 if (0 == (n & O_NONBLOCK)) {
154 dbg(ctx,
"Short read: %d instead of %d", (
int)(size-m), (
int) size);
161 i = select(fd, &myset, NULL, NULL, &to);
165 if (i < 0 && errno == EINTR)
continue;
167 if (i < 0 && m == size)
return -errno;
170 dbg(ctx,
"Read error %d: %d bytes read, instead of %d", -errno, (
int)(size-m), (
int)size);
174 if (i == 0 && m == size) {
180 dbg(ctx,
"Short read (timeout): %d instead of %d", (
int)(size-m), (
int)size);
187 unsigned int j = 0, k;
191 for (k = 0; k < size-m; k++, j++) {
193 dbg(ctx,
"< %s\n",buf);
197 sprintf(buf + strlen(buf),
" 0x%02x,", msg_[k]);
199 dbg(ctx,
"< %s\n",buf);
207 if (!ctx)
return -EINVAL;
213 if (!ctx)
return -EINVAL;
218 if (!ctx)
return -EINVAL;
230 return ctx->iotimeout;
LIGHTIFY_EXPORT int lightify_skt_setfd(struct lightify_ctx *ctx, int socket)
LIGHTIFY_EXPORT int lightify_skt_setiotimeout(struct lightify_ctx *ctx, struct timeval tv)
int write_to_socket(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
LIGHTIFY_EXPORT int lightify_skt_getfd(struct lightify_ctx *ctx)
LIGHTIFY_EXPORT struct timeval lightify_skt_getiotimeout(struct lightify_ctx *ctx)
int read_from_socket(struct lightify_ctx *ctx, unsigned char *msg, size_t size)
int lightify_get_log_priority(struct lightify_ctx *ctx)
struct lightify_ctx * ctx