int CPeerBase::Send() { if (buffer_size(m_outBuffer) <= 0) return 0; int iBufferLeft = fdwatch_get_buffer_size(m_fdWatcher, m_fd); int iBytesToWrite = MIN(iBufferLeft, buffer_size(m_outBuffer)); if (iBytesToWrite == 0) return 0; int result = socket_write(m_fd, (const char *) buffer_read_peek(m_outBuffer), iBytesToWrite); if (result == 0) { buffer_read_proceed(m_outBuffer, iBytesToWrite); if (buffer_size(m_outBuffer) != 0) fdwatch_add_fd(m_fdWatcher, m_fd, this, FDW_WRITE, true); } return (result); }
int io_loop(LPFDWATCH fdw) { LPDESC d; int num_events, event_idx; DESC_MANAGER::instance().DestroyClosed(); // PHASE_CLOSEАО БўјУµйА» ІчѕоБШґЩ. DESC_MANAGER::instance().TryConnect(); if ((num_events = fdwatch(fdw, 0)) < 0) return 0; for (event_idx = 0; event_idx < num_events; ++event_idx) { d = (LPDESC) fdwatch_get_client_data(fdw, event_idx); if (!d) { if (FDW_READ == fdwatch_check_event(fdw, tcp_socket, event_idx)) { DESC_MANAGER::instance().AcceptDesc(fdw, tcp_socket); fdwatch_clear_event(fdw, tcp_socket, event_idx); } else if (FDW_READ == fdwatch_check_event(fdw, p2p_socket, event_idx)) { DESC_MANAGER::instance().AcceptP2PDesc(fdw, p2p_socket); fdwatch_clear_event(fdw, p2p_socket, event_idx); } /* else if (FDW_READ == fdwatch_check_event(fdw, udp_socket, event_idx)) { char buf[256]; struct sockaddr_in cliaddr; socklen_t socklen = sizeof(cliaddr); int iBytesRead; if ((iBytesRead = socket_udp_read(udp_socket, buf, 256, (struct sockaddr *) &cliaddr, &socklen)) > 0) { static CInputUDP s_inputUDP; s_inputUDP.SetSockAddr(cliaddr); int iBytesProceed; s_inputUDP.Process(NULL, buf, iBytesRead, iBytesProceed); } fdwatch_clear_event(fdw, udp_socket, event_idx); } */ continue; } int iRet = fdwatch_check_event(fdw, d->GetSocket(), event_idx); switch (iRet) { case FDW_READ: if (db_clientdesc == d) { int size = d->ProcessInput(); if (size) sys_log(1, "DB_BYTES_READ: %d", size); if (size < 0) { d->SetPhase(PHASE_CLOSE); } } else if (d->ProcessInput() < 0) { d->SetPhase(PHASE_CLOSE); } break; case FDW_WRITE: if (db_clientdesc == d) { int buf_size = buffer_size(d->GetOutputBuffer()); int sock_buf_size = fdwatch_get_buffer_size(fdw, d->GetSocket()); int ret = d->ProcessOutput(); if (ret < 0) { d->SetPhase(PHASE_CLOSE); } if (buf_size) sys_log(1, "DB_BYTES_WRITE: size %d sock_buf %d ret %d", buf_size, sock_buf_size, ret); } else if (d->ProcessOutput() < 0) { d->SetPhase(PHASE_CLOSE); } else if (g_TeenDesc==d) { int buf_size = buffer_size(d->GetOutputBuffer()); int sock_buf_size = fdwatch_get_buffer_size(fdw, d->GetSocket()); int ret = d->ProcessOutput(); if (ret < 0) { d->SetPhase(PHASE_CLOSE); } if (buf_size) sys_log(0, "TEEN::Send(size %d sock_buf %d ret %d)", buf_size, sock_buf_size, ret); } break; case FDW_EOF: { d->SetPhase(PHASE_CLOSE); } break; default: sys_err("fdwatch_check_event returned unknown %d", iRet); d->SetPhase(PHASE_CLOSE); break; } } return 1; }