void BufferedSocket::threadSendData() { dcassert(sock); if(!sock) return; { Lock l(cs); if(writeBuf.empty()) return; writeBuf.swap(sendBuf); } size_t left = sendBuf.size(); size_t done = 0; while(left > 0) { if(disconnecting) { return; } int w = sock->wait(POLL_TIMEOUT, Socket::WAIT_READ | Socket::WAIT_WRITE); if(w & Socket::WAIT_READ) { threadRead(); } if(w & Socket::WAIT_WRITE) { int n = sock->write(&sendBuf[done], left); if(n > 0) { left -= n; done += n; } } } sendBuf.clear(); }
int main() { boost::thread threadRead(boost::ref(*read)); boost::thread threadWrite(boost::ref(*write)); threadRead.join(); threadWrite.join(); }
void BufferedSocket::checkSocket() { int waitFor = sock->wait(POLL_TIMEOUT, Socket::WAIT_READ); if(waitFor & Socket::WAIT_READ) { threadRead(); } }
void BufferedSocket::checkSocket() { dcassert(sock); if(!sock) return; int waitFor = sock->wait(POLL_TIMEOUT, Socket::WAIT_READ); if(waitFor & Socket::WAIT_READ) { threadRead(); } }
void Server::incomingConnection(int socketDescriptor) { Thread *thread = new Thread(socketDescriptor, this); for (int i = 0; i < numberOfPlayers; i++){ if (!players[i]->isConnected()){ players[i]->setConnectionThread(thread); connect(players[i], SIGNAL(fromPlayer(QString)), this, SLOT(fromClient(QString))); break; } } connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); connect(thread, SIGNAL(started()), thread, SLOT(threadStarted())); connect(thread, SIGNAL(threadRead()), this, SLOT(checkPlayers())); thread->start(); }
void BufferedSocket::threadSendFile(InputStream* file) throw(Exception) { dcassert(sock); if(!sock) return; dcassert(file != NULL); size_t sockSize = (size_t)sock->getSocketOptInt(SO_SNDBUF); size_t bufSize = max(sockSize, (size_t)64*1024); vector<uint8_t> readBuf(bufSize); vector<uint8_t> writeBuf(bufSize); size_t readPos = 0; bool readDone = false; dcdebug("Starting threadSend\n"); while(true) { if(!readDone && readBuf.size() > readPos) { // Fill read buffer size_t bytesRead = readBuf.size() - readPos; size_t actual = file->read(&readBuf[readPos], bytesRead); if(bytesRead > 0) { fire(BufferedSocketListener::BytesSent(), bytesRead, 0); } if(actual == 0) { readDone = true; } else { readPos += actual; } } if(readDone && readPos == 0) { fire(BufferedSocketListener::TransmitDone()); return; } readBuf.swap(writeBuf); readBuf.resize(bufSize); writeBuf.resize(readPos); readPos = 0; size_t writePos = 0; while(writePos < writeBuf.size()) { if(disconnecting) return; size_t writeSize = min(sockSize / 2, writeBuf.size() - writePos); int written = sock->write(&writeBuf[writePos], writeSize); if(written > 0) { writePos += written; fire(BufferedSocketListener::BytesSent(), 0, written); } else if(written == -1) { if(!readDone && readPos < readBuf.size()) { // Read a little since we're blocking anyway... size_t bytesRead = min(readBuf.size() - readPos, readBuf.size() / 2); size_t actual = file->read(&readBuf[readPos], bytesRead); if(bytesRead > 0) { fire(BufferedSocketListener::BytesSent(), bytesRead, 0); } if(actual == 0) { readDone = true; } else { readPos += actual; } } else { while(!disconnecting) { int w = sock->wait(POLL_TIMEOUT, Socket::WAIT_WRITE | Socket::WAIT_READ); if(w & Socket::WAIT_READ) { threadRead(); } if(w & Socket::WAIT_WRITE) { break; } } } } } } }
void BufferedSocket::threadSendFile(InputStream* file) throw(Exception) { if(state != RUNNING) return; if(disconnecting) return; dcassert(file != NULL); size_t sockSize = (size_t)sock->getSocketOptInt(SO_SNDBUF); size_t bufSize = max(sockSize, (size_t)64*1024); ByteVector readBuf(bufSize); ByteVector writeBuf(bufSize); size_t readPos = 0; bool readDone = false; dcdebug("Starting threadSend\n"); UploadManager *um = UploadManager::getInstance(); size_t sendMaximum, start = 0, current= 0; bool throttling; while(true) { if(!readDone && readBuf.size() > readPos) { // Fill read buffer size_t bytesRead = readBuf.size() - readPos; size_t actual = file->read(&readBuf[readPos], bytesRead); if(bytesRead > 0) { fire(BufferedSocketListener::BytesSent(), bytesRead, 0); } if(actual == 0) { readDone = true; } else { readPos += actual; } } if(readDone && readPos == 0) { fire(BufferedSocketListener::TransmitDone()); return; } readBuf.swap(writeBuf); readBuf.resize(bufSize); writeBuf.resize(readPos); readPos = 0; size_t writePos = 0; while(writePos < writeBuf.size()) { if(disconnecting) return; throttling = BOOLSETTING(THROTTLE_ENABLE); size_t writeSize; if(throttling) { start = TimerManager::getTick(); sendMaximum = um->throttleGetSlice(); if(sendMaximum < 0) { throttling = false; writeSize = min(sockSize / 2, writeBuf.size() - writePos); } else { writeSize = min(min(sockSize / 2, writeBuf.size() - writePos), sendMaximum); } } else { writeSize = min(sockSize / 2, writeBuf.size() - writePos); } int written = sock->write(&writeBuf[writePos], writeSize); if(written > 0) { writePos += written; fire(BufferedSocketListener::BytesSent(), 0, written); if(throttling) { int32_t cycle_time = um->throttleCycleTime(); current = TimerManager::getTick(); int32_t sleep_time = cycle_time - (current - start); if (sleep_time > 0 && sleep_time <= cycle_time) { Thread::sleep(sleep_time); } } } else if(written == -1) { if(!readDone && readPos < readBuf.size()) { // Read a little since we're blocking anyway... size_t bytesRead = min(readBuf.size() - readPos, readBuf.size() / 2); size_t actual = file->read(&readBuf[readPos], bytesRead); if(bytesRead > 0) { fire(BufferedSocketListener::BytesSent(), bytesRead, 0); } if(actual == 0) { readDone = true; } else { readPos += actual; } } else { while(!disconnecting) { int w = sock->wait(POLL_TIMEOUT, Socket::WAIT_WRITE | Socket::WAIT_READ); if(w & Socket::WAIT_READ) { threadRead(); } if(w & Socket::WAIT_WRITE) { break; } } } } } } }