Replace all use of write() with tvh_write().

tvh_write() will deal with incomplete/interrupted writes and will ensure
the write operation is completed unless a fatal error occurs.
This commit is contained in:
Adam Sutton 2012-12-30 19:55:46 +00:00
parent 993c2decb3
commit 54ceb4ef00
7 changed files with 24 additions and 40 deletions

View file

@ -227,7 +227,7 @@ static int
capmt_send_msg(capmt_t *capmt, int sid, const uint8_t *buf, size_t len)
{
if (capmt->capmt_oscam) {
int i, sent = 0;
int i;
// dumping current SID table
for (i = 0; i < MAX_SOCKETS; i++)
@ -280,18 +280,17 @@ capmt_send_msg(capmt_t *capmt, int sid, const uint8_t *buf, size_t len)
tvhlog(LOG_DEBUG, "capmt", "created socket with socket_fd=%d", capmt->capmt_sock[i]);
}
if (capmt->capmt_sock[i] > 0) {
sent = write(capmt->capmt_sock[i], buf, len);
tvhlog(LOG_DEBUG, "capmt", "socket_fd=%d len=%d sent=%d", capmt->capmt_sock[i], (int)len, sent);
if (sent != len) {
tvhlog(LOG_ERR, "capmt", "%s: len != sent", __FUNCTION__);
if (tvh_write(capmt->capmt_sock[i], buf, len)) {
tvhlog(LOG_DEBUG, "capmt", "socket_fd=%d send failed", capmt->capmt_sock[i]);
close(capmt->capmt_sock[i]);
capmt->capmt_sock[i] = 0;
return -1;
}
}
return sent;
}
else // standard old capmt mode
return write(capmt->capmt_sock[0], buf, len);
tvh_write(capmt->capmt_sock[0], buf, len);
return 0;
}
static void

View file

@ -477,7 +477,7 @@ cwc_send_msg(cwc_t *cwc, const uint8_t *msg, size_t len, int sid, int enq)
{
cwc_message_t *cm = malloc(sizeof(cwc_message_t));
uint8_t *buf = cm->cm_data;
int seq, n;
int seq;
if(len + 12 > CWS_NETMSGSIZE) {
free(cm);
@ -513,8 +513,7 @@ cwc_send_msg(cwc_t *cwc, const uint8_t *msg, size_t len, int sid, int enq)
pthread_cond_signal(&cwc->cwc_writer_cond);
pthread_mutex_unlock(&cwc->cwc_writer_mutex);
} else {
n = write(cwc->cwc_fd, buf, len);
if(n != len)
if (tvh_write(cwc->cwc_fd, buf, len))
tvhlog(LOG_INFO, "cwc", "write error %s", strerror(errno));
free(cm);
@ -1032,7 +1031,8 @@ cwc_writer_thread(void *aux)
TAILQ_REMOVE(&cwc->cwc_writeq, cm, cm_link);
pthread_mutex_unlock(&cwc->cwc_writer_mutex);
// int64_t ts = getmonoclock();
r = write(cwc->cwc_fd, cm->cm_data, cm->cm_len);
if (tvh_write(cwc->cwc_fd, cm->cm_data, cm->cm_len))
tvhlog(LOG_INFO, "cwc", "write error %s", strerror(errno));
// printf("Write took %lld usec\n", getmonoclock() - ts);
free(cm);
pthread_mutex_lock(&cwc->cwc_writer_mutex);

View file

@ -623,8 +623,8 @@ dvb_adapter_stop ( th_dvb_adapter_t *tda )
/* Stop DVR thread */
if (tda->tda_dvr_pipe.rd != -1) {
tvhlog(LOG_DEBUG, "dvb", "%s stopping thread", tda->tda_rootpath);
int err = write(tda->tda_dvr_pipe.wr, "", 1);
assert(err != -1);
int err = tvh_write(tda->tda_dvr_pipe.wr, "", 1);
assert(!err);
pthread_join(tda->tda_dvr_thread, NULL);
close(tda->tda_dvr_pipe.rd);
close(tda->tda_dvr_pipe.wr);

View file

@ -241,9 +241,8 @@ static int _epg_write ( int fd, htsmsg_t *m )
int r = htsmsg_binary_serialize(m, &msgdata, &msglen, 0x10000);
htsmsg_destroy(m);
if (!r) {
ssize_t w = write(fd, msgdata, msglen);
ret = tvh_write(fd, msgdata, msglen);
free(msgdata);
if(w == msglen) ret = 0;
}
} else {
ret = 0;

View file

@ -1669,7 +1669,6 @@ static void *
htsp_write_scheduler(void *aux)
{
htsp_connection_t *htsp = aux;
int r;
htsp_msg_q_t *hmq;
htsp_msg_t *hm;
void *dptr;
@ -1706,33 +1705,21 @@ htsp_write_scheduler(void *aux)
pthread_mutex_unlock(&htsp->htsp_out_mutex);
r = htsmsg_binary_serialize(hm->hm_msg, &dptr, &dlen, INT32_MAX);
if (htsmsg_binary_serialize(hm->hm_msg, &dptr, &dlen, INT32_MAX) != 0) {
tvhlog(LOG_WARNING, "htsp", "%s: failed to serialize data",
htsp->htsp_logname);
}
htsp_msg_destroy(hm);
void *freeme = dptr;
while(dlen > 0) {
r = write(htsp->htsp_fd, dptr, dlen);
if(r < 0) {
if(errno == EAGAIN || errno == EWOULDBLOCK)
continue;
tvhlog(LOG_INFO, "htsp", "%s: Write error -- %s",
htsp->htsp_logname, strerror(errno));
break;
}
if(r == 0) {
tvhlog(LOG_ERR, "htsp", "%s: write() returned 0",
htsp->htsp_logname);
}
dptr += r;
dlen -= r;
if (tvh_write(htsp->htsp_fd, dptr, dlen)) {
tvhlog(LOG_INFO, "htsp", "%s: Write error -- %s",
htsp->htsp_logname, strerror(errno));
break;
}
free(freeme);
free(dptr);
pthread_mutex_lock(&htsp->htsp_out_mutex);
if(dlen)
break;
}
// Shutdown socket to make receive thread terminate entire HTSP connection

View file

@ -158,8 +158,7 @@ hts_settings_save(htsmsg_t *record, const char *pathfmt, ...)
htsbuf_queue_init(&hq, 0);
htsmsg_json_serialize(record, &hq, 1);
TAILQ_FOREACH(hd, &hq.hq_q, hd_link)
if(write(fd, hd->hd_data + hd->hd_data_off, hd->hd_data_len) !=
hd->hd_data_len) {
if(tvh_write(fd, hd->hd_data + hd->hd_data_off, hd->hd_data_len)) {
tvhlog(LOG_ALERT, "settings", "Failed to write file \"%s\" - %s",
tmppath, strerror(errno));
ok = 0;

View file

@ -253,7 +253,7 @@ v4l_service_stop(service_t *t)
assert(va->va_current_service != NULL);
if(write(va->va_pipe[1], &c, 1) != 1)
if(tvh_write(va->va_pipe[1], &c, 1))
tvhlog(LOG_ERR, "v4l", "Unable to close video thread -- %s",
strerror(errno));