void np_conn_respond(Npreq *req) { int n; Npconn *conn = req->conn; Npsrv *srv = conn->srv; Npfcall *rc = req->rcall; _debug_trace (srv, rc); xpthread_mutex_lock(&conn->wlock); n = np_trans_send(conn->trans, rc); xpthread_mutex_unlock(&conn->wlock); if (n < 0) np_logerr (srv, "send to '%s'", conn->client_id); }
/* Called by srv workers to transmit req->rcall->pkt. */ void np_conn_respond(Npreq *req) { int n, send; Npconn *conn = req->conn; Npsrv *srv = conn->srv; Npfcall *rc = req->rcall; Nptrans *trans = NULL; if (!rc) goto done; xpthread_mutex_lock(&conn->lock); send = conn->trans && !conn->resetting; xpthread_mutex_unlock(&conn->lock); if (send) { if ((srv->flags & SRV_FLAGS_DEBUG_9PTRACE)) _debug_trace (srv, rc); xpthread_mutex_lock(&conn->wlock); n = np_trans_write(conn->trans, rc->pkt, rc->size); conn->reqs_out++; xpthread_mutex_unlock(&conn->wlock); if (n <= 0) { /* write error */ xpthread_mutex_lock(&conn->lock); trans = conn->trans; conn->trans = NULL; xpthread_mutex_unlock(&conn->lock); } } done: _free_npfcall(req->tcall); free(req->rcall); req->tcall = NULL; req->rcall = NULL; if (conn->resetting) { xpthread_mutex_lock(&conn->srv->lock); xpthread_cond_broadcast(&conn->resetcond); xpthread_mutex_unlock(&conn->srv->lock); } if (trans) /* np_conn_read_proc will take care of resetting */ np_trans_destroy(trans); }
/* Per-connection read thread. */ static void * np_conn_read_proc(void *a) { int i, n, size; Npsrv *srv; Npconn *conn = (Npconn *)a; Nptrans *trans; Npreq *req; Npfcall *fc, *fc1; pthread_detach(pthread_self()); np_conn_incref(conn); srv = conn->srv; fc = _alloc_npfcall(conn->msize); n = 0; while (fc && conn->trans && (i = np_trans_read(conn->trans, fc->pkt + n, conn->msize - n)) > 0) { n += i; again: size = np_peek_size (fc->pkt, n); if (size == 0 || n < size) continue; /* Corruption on the transport, unhandled op, etc. * is fatal to the connection. We could consider returning * an error to the client here. However, various kernels * may not handle that well, depending on where it happens. */ if (!np_deserialize(fc, fc->pkt)) { _debug_trace (srv, fc); np_logerr (srv, "protocol error - " "dropping connection to '%s'", conn->client_id); break; } if ((srv->flags & SRV_FLAGS_DEBUG_9PTRACE)) _debug_trace (srv, fc); /* Replace fc, and copy any data past the current packet * to the replacement. */ fc1 = _alloc_npfcall(conn->msize); if (!fc1) { np_logerr (srv, "out of memory in receive path - " "dropping connection to '%s'", conn->client_id); break; } if (n > size) memmove(fc1->pkt, fc->pkt + size, n - size); n -= size; /* Encapsulate fc in a request and hand to srv worker threads. * In np_req_alloc, req->fid is looked up/initialized. */ req = np_req_alloc(conn, fc); if (!req) { np_logerr (srv, "out of memory in receive path - " "dropping connection to '%s'", conn->client_id); break; } np_srv_add_req(srv, req); xpthread_mutex_lock(&conn->lock); conn->reqs_in++; xpthread_mutex_unlock(&conn->lock); fc = fc1; if (n > 0) goto again; } /* Just got EOF on read, or some other fatal error for the * connection like out of memory. */ xpthread_mutex_lock(&conn->lock); trans = conn->trans; conn->trans = NULL; if (fc) _free_npfcall(fc); xpthread_mutex_unlock(&conn->lock); np_srv_remove_conn(conn->srv, conn); np_conn_reset(conn); if (trans) np_trans_destroy(trans); np_conn_decref(conn); return NULL; }
/* Per-connection read thread. */ static void * np_conn_read_proc(void *a) { Npconn *conn = (Npconn *)a; Npsrv *srv = conn->srv; Npreq *req; Npfcall *fc; pthread_detach(pthread_self()); for (;;) { if (np_trans_recv(conn->trans, &fc, conn->msize) < 0) { np_logerr (srv, "recv error - " "dropping connection to '%s'", conn->client_id); break; } if (!fc) /* EOF */ break; _debug_trace (srv, fc); /* Encapsulate fc in a request and hand to srv worker threads. * In np_req_alloc, req->fid is looked up/initialized. */ req = np_req_alloc(conn, fc); if (!req) { np_logmsg (srv, "out of memory in receive path - " "dropping connection to '%s'", conn->client_id); free (fc); break; } /* Enqueue request for processing by next available worker * thread, except P9_TFLUSH which is handled immediately. */ if (fc->type == P9_TFLUSH) { if (np_flush (req, fc)) { np_req_respond_flush (req); np_req_unref(req); } xpthread_mutex_lock (&srv->lock); srv->tpool->stats.nreqs[P9_TFLUSH]++; xpthread_mutex_unlock (&srv->lock); } else { xpthread_mutex_lock(&srv->lock); np_srv_add_req(srv, req); xpthread_mutex_unlock(&srv->lock); } } /* Just got EOF on read, or some other fatal error for the * connection like out of memory. */ np_conn_flush (conn); xpthread_mutex_lock(&conn->lock); while (conn->refcount > 0) xpthread_cond_wait(&conn->refcond, &conn->lock); xpthread_mutex_unlock(&conn->lock); np_conn_destroy(conn); return NULL; }