/* * Serve_Client * Thread, started by the server, for serving a client connection. * Reads data from socket and writes it back, unmodified, and * closes the socket */ static void PR_CALLBACK Serve_Client(void *arg) { Serve_Client_Param *scp = (Serve_Client_Param *) arg; buffer *in_buf; Session *sp; PRJob *jobp; sp = PR_NEW(Session); sp->iod = scp->iod; in_buf = PR_NEW(buffer); if (in_buf == NULL) { fprintf(stderr,"%s: failed to alloc buffer struct\n",program_name); failed_already=1; return; } sp->in_buf = in_buf; sp->bytes = scp->datalen; sp->msg_num = 0; sp->bytes_read = 0; sp->tp = scp->tp; sp->exit_mon = scp->exit_mon; sp->job_counterp = scp->job_counterp; sp->iod.timeout = PR_SecondsToInterval(60); jobp = PR_QueueJob_Read(sp->tp, &sp->iod, serve_client_read, sp, PR_FALSE); PR_ASSERT(NULL != jobp); PR_DELETE(scp); }
nsresult CBFDNSSDService::SetupNotifications() { if ( m_stopped ) return NS_OK; m_iod.socket = m_fileDesc; m_iod.timeout = PR_INTERVAL_NO_TIMEOUT; m_job = PR_QueueJob_Read( m_threadPool, &m_iod, Read, this, PR_FALSE ); return ( m_job ) ? NS_OK : NS_ERROR_FAILURE; }
nsresult CDNSSDService::SetupNotifications() { NS_PRECONDITION( m_threadPool != NULL, "m_threadPool is NULL" ); NS_PRECONDITION( m_fileDesc != NULL, "m_fileDesc is NULL" ); NS_PRECONDITION( m_job == NULL, "m_job is not NULL" ); m_iod.socket = m_fileDesc; m_iod.timeout = PR_INTERVAL_MAX; m_job = PR_QueueJob_Read( m_threadPool, &m_iod, Read, this, PR_FALSE ); return ( m_job ) ? NS_OK : NS_ERROR_FAILURE; }
static void serve_client_write(void *arg) { Session *sp = (Session *) arg; int bytes; PRFileDesc *sockfd; char *buf; PRJob *jobp; sockfd = sp->iod.socket; buf = sp->in_buf->data; PR_ASSERT(sp->msg_num < num_tcp_mesgs_per_connection); bytes = PR_Send(sockfd, buf, sp->bytes, 0, PR_INTERVAL_NO_TIMEOUT); PR_ASSERT(bytes == sp->bytes); if (bytes < 0) { return; } DPRINTF(("serve_client: write complete, msg(%d) \n", sp->msg_num)); sp->msg_num++; if (sp->msg_num < num_tcp_mesgs_per_connection) { sp->bytes_read = 0; sp->iod.timeout = PR_SecondsToInterval(60); jobp = PR_QueueJob_Read(sp->tp, &sp->iod, serve_client_read, sp, PR_FALSE); PR_ASSERT(NULL != jobp); return; } DPRINTF(("serve_client: read/write complete, msg(%d) \n", sp->msg_num)); if (PR_Shutdown(sockfd, PR_SHUTDOWN_BOTH) < 0) { fprintf(stderr,"%s: ERROR - PR_Shutdown\n", program_name); } PR_Close(sockfd); PR_EnterMonitor(sp->exit_mon); --(*sp->job_counterp); PR_Notify(sp->exit_mon); PR_ExitMonitor(sp->exit_mon); PR_DELETE(sp->in_buf); PR_DELETE(sp); return; }
static void serve_client_read(void *arg) { Session *sp = (Session *) arg; int rem; int bytes; int offset; PRFileDesc *sockfd; char *buf; PRJob *jobp; PRIntervalTime timeout = PR_INTERVAL_NO_TIMEOUT; sockfd = sp->iod.socket; buf = sp->in_buf->data; PR_ASSERT(sp->msg_num < num_tcp_mesgs_per_connection); PR_ASSERT(sp->bytes_read < sp->bytes); offset = sp->bytes_read; rem = sp->bytes - offset; bytes = PR_Recv(sockfd, buf + offset, rem, 0, timeout); if (bytes < 0) { return; } sp->bytes_read += bytes; sp->iod.timeout = PR_SecondsToInterval(60); if (sp->bytes_read < sp->bytes) { jobp = PR_QueueJob_Read(sp->tp, &sp->iod, serve_client_read, sp, PR_FALSE); PR_ASSERT(NULL != jobp); return; } PR_ASSERT(sp->bytes_read == sp->bytes); DPRINTF(("serve_client: read complete, msg(%d) \n", sp->msg_num)); sp->iod.timeout = PR_SecondsToInterval(60); jobp = PR_QueueJob_Write(sp->tp, &sp->iod, serve_client_write, sp, PR_FALSE); PR_ASSERT(NULL != jobp); return; }