本文整理汇总了C++中ThreadData::getBuf方法的典型用法代码示例。如果您正苦于以下问题:C++ ThreadData::getBuf方法的具体用法?C++ ThreadData::getBuf怎么用?C++ ThreadData::getBuf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ThreadData
的用法示例。
在下文中一共展示了ThreadData::getBuf方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sendLocalCheckpoint
/***SEND_LOCAL_CHECKPOINT STATE***/
Receiver::ReceiverState Receiver::sendLocalCheckpoint(ThreadData &data) {
LOG(INFO) << data << " entered SEND_LOCAL_CHECKPOINT state ";
auto &socket = data.socket_;
auto &threadStats = data.threadStats_;
auto &doneSendFailure = data.doneSendFailure_;
char *buf = data.getBuf();
// in case SEND_DONE failed, a special checkpoint(-1) is sent to signal this
// condition
auto checkpoint = doneSendFailure ? -1 : threadStats.getNumBlocks();
std::vector<Checkpoint> checkpoints;
checkpoints.emplace_back(threadServerSockets_[data.threadIndex_].getPort(),
checkpoint);
int64_t off = 0;
Protocol::encodeCheckpoints(buf, off, Protocol::kMaxLocalCheckpoint,
checkpoints);
auto written = socket.write(buf, Protocol::kMaxLocalCheckpoint);
if (written != Protocol::kMaxLocalCheckpoint) {
LOG(ERROR) << "unable to write local checkpoint. write mismatch "
<< Protocol::kMaxLocalCheckpoint << " " << written;
threadStats.setErrorCode(SOCKET_WRITE_ERROR);
return ACCEPT_WITH_TIMEOUT;
}
threadStats.addHeaderBytes(Protocol::kMaxLocalCheckpoint);
if (doneSendFailure) {
return SEND_DONE_CMD;
}
return READ_NEXT_CMD;
}
示例2: sendDoneCmd
Receiver::ReceiverState Receiver::sendDoneCmd(ThreadData &data) {
VLOG(1) << data << " entered SEND_DONE_CMD state ";
char *buf = data.getBuf();
auto &socket = data.socket_;
auto &threadStats = data.threadStats_;
auto &doneSendFailure = data.doneSendFailure_;
buf[0] = Protocol::DONE_CMD;
if (socket.write(buf, 1) != 1) {
PLOG(ERROR) << "unable to send DONE " << data.threadIndex_;
doneSendFailure = true;
return ACCEPT_WITH_TIMEOUT;
}
threadStats.addHeaderBytes(1);
auto read = socket.read(buf, 1);
if (read != 1 || buf[0] != Protocol::DONE_CMD) {
LOG(ERROR) << data << " did not receive ack for DONE";
doneSendFailure = true;
return ACCEPT_WITH_TIMEOUT;
}
read = socket.read(buf, Protocol::kMinBufLength);
if (read != 0) {
LOG(ERROR) << data << " EOF not found where expected";
doneSendFailure = true;
return ACCEPT_WITH_TIMEOUT;
}
socket.closeCurrentConnection();
LOG(INFO) << data << " got ack for DONE. Transfer finished";
return END;
}
示例3: sendGlobalCheckpoint
Receiver::ReceiverState Receiver::sendGlobalCheckpoint(ThreadData &data) {
LOG(INFO) << data << " entered SEND_GLOBAL_CHECKPOINTS state ";
char *buf = data.getBuf();
auto &off = data.off_;
auto &newCheckpoints = data.newCheckpoints_;
auto &socket = data.socket_;
auto &checkpointIndex = data.checkpointIndex_;
auto &pendingCheckpointIndex = data.pendingCheckpointIndex_;
auto &threadStats = data.threadStats_;
auto &numRead = data.numRead_;
auto bufferSize = data.bufferSize_;
buf[0] = Protocol::ERR_CMD;
off = 1;
// leave space for length
off += sizeof(int16_t);
auto oldOffset = off;
Protocol::encodeCheckpoints(buf, off, bufferSize, newCheckpoints);
int16_t length = off - oldOffset;
folly::storeUnaligned<int16_t>(buf + 1, folly::Endian::little(length));
auto written = socket.write(buf, off);
if (written != off) {
LOG(ERROR) << "unable to write error checkpoints";
threadStats.setErrorCode(SOCKET_WRITE_ERROR);
return ACCEPT_WITH_TIMEOUT;
} else {
threadStats.addHeaderBytes(off);
pendingCheckpointIndex = checkpointIndex + newCheckpoints.size();
numRead = off = 0;
return READ_NEXT_CMD;
}
}
示例4: processDoneCmd
Receiver::ReceiverState Receiver::processDoneCmd(ThreadData &data) {
VLOG(1) << data << " entered PROCESS_DONE_CMD state ";
auto &numRead = data.numRead_;
auto &threadStats = data.threadStats_;
auto &checkpointIndex = data.checkpointIndex_;
auto &pendingCheckpointIndex = data.pendingCheckpointIndex_;
auto &off = data.off_;
auto &oldOffset = data.oldOffset_;
char *buf = data.getBuf();
if (numRead != Protocol::kMinBufLength) {
LOG(ERROR) << "Unexpected state for done command"
<< " off: " << off << " numRead: " << numRead;
threadStats.setErrorCode(PROTOCOL_ERROR);
return WAIT_FOR_FINISH_WITH_THREAD_ERROR;
}
ErrorCode senderStatus = (ErrorCode)buf[off++];
int64_t numBlocksSend;
bool success = Protocol::decodeDone(buf, off, oldOffset + Protocol::kMaxDone,
numBlocksSend);
if (!success) {
LOG(ERROR) << "Unable to decode done cmd";
threadStats.setErrorCode(PROTOCOL_ERROR);
return WAIT_FOR_FINISH_WITH_THREAD_ERROR;
}
threadStats.setRemoteErrorCode(senderStatus);
// received a valid command, applying pending checkpoint write update
checkpointIndex = pendingCheckpointIndex;
std::unique_lock<std::mutex> lock(mutex_);
numBlocksSend_ = numBlocksSend;
return WAIT_FOR_FINISH_OR_NEW_CHECKPOINT;
}
示例5: processSizeCmd
Receiver::ReceiverState Receiver::processSizeCmd(ThreadData &data) {
VLOG(1) << data << " entered PROCESS_SIZE_CMD state ";
auto &threadStats = data.threadStats_;
auto &numRead = data.numRead_;
auto &off = data.off_;
auto &oldOffset = data.oldOffset_;
char *buf = data.getBuf();
std::lock_guard<std::mutex> lock(mutex_);
bool success = Protocol::decodeSize(buf, off, oldOffset + Protocol::kMaxSize,
totalSenderBytes_);
if (!success) {
LOG(ERROR) << "Unable to decode size cmd";
threadStats.setErrorCode(PROTOCOL_ERROR);
return WAIT_FOR_FINISH_WITH_THREAD_ERROR;
}
VLOG(1) << "Number of bytes to receive " << totalSenderBytes_;
auto msgLen = off - oldOffset;
numRead -= msgLen;
return READ_NEXT_CMD;
}
示例6: readNextCmd
/***READ_NEXT_CMD***/
Receiver::ReceiverState Receiver::readNextCmd(ThreadData &data) {
VLOG(1) << data << " entered READ_NEXT_CMD state ";
auto &socket = data.socket_;
auto &threadStats = data.threadStats_;
char *buf = data.getBuf();
auto &numRead = data.numRead_;
auto &off = data.off_;
auto &oldOffset = data.oldOffset_;
auto bufferSize = data.bufferSize_;
oldOffset = off;
numRead = readAtLeast(socket, buf + off, bufferSize - off,
Protocol::kMinBufLength, numRead);
if (numRead <= 0) {
LOG(ERROR) << "socket read failure " << Protocol::kMinBufLength << " "
<< numRead;
threadStats.setErrorCode(SOCKET_READ_ERROR);
return ACCEPT_WITH_TIMEOUT;
}
Protocol::CMD_MAGIC cmd = (Protocol::CMD_MAGIC)buf[off++];
if (cmd == Protocol::EXIT_CMD) {
return PROCESS_EXIT_CMD;
}
if (cmd == Protocol::DONE_CMD) {
return PROCESS_DONE_CMD;
}
if (cmd == Protocol::FILE_CMD) {
return PROCESS_FILE_CMD;
}
if (cmd == Protocol::SETTINGS_CMD) {
return PROCESS_SETTINGS_CMD;
}
if (cmd == Protocol::SIZE_CMD) {
return PROCESS_SIZE_CMD;
}
LOG(ERROR) << "received an unknown cmd";
threadStats.setErrorCode(PROTOCOL_ERROR);
return WAIT_FOR_FINISH_WITH_THREAD_ERROR;
}
示例7: sendAbortCmd
Receiver::ReceiverState Receiver::sendAbortCmd(ThreadData &data) {
LOG(INFO) << data << " entered SEND_ABORT_CMD state ";
auto &threadStats = data.threadStats_;
char *buf = data.getBuf();
auto &socket = data.socket_;
int32_t protocolVersion = data.threadProtocolVersion_;
int64_t offset = 0;
buf[offset++] = Protocol::ABORT_CMD;
Protocol::encodeAbort(buf, offset, protocolVersion,
threadStats.getErrorCode(), threadStats.getNumFiles());
socket.write(buf, offset);
// No need to check if we were successful in sending ABORT
// This thread will simply disconnect and sender thread on the
// other side will timeout
socket.closeCurrentConnection();
threadStats.addHeaderBytes(offset);
if (threadStats.getErrorCode() == VERSION_MISMATCH) {
// Receiver should try again expecting sender to have changed its version
return ACCEPT_WITH_TIMEOUT;
}
return WAIT_FOR_FINISH_WITH_THREAD_ERROR;
}
示例8: processFileCmd
/***PROCESS_FILE_CMD***/
Receiver::ReceiverState Receiver::processFileCmd(ThreadData &data) {
VLOG(1) << data << " entered PROCESS_FILE_CMD state ";
const auto &options = WdtOptions::get();
auto &socket = data.socket_;
auto &threadIndex = data.threadIndex_;
auto &threadStats = data.threadStats_;
char *buf = data.getBuf();
auto &numRead = data.numRead_;
auto &off = data.off_;
auto &oldOffset = data.oldOffset_;
auto bufferSize = data.bufferSize_;
auto &checkpointIndex = data.checkpointIndex_;
auto &pendingCheckpointIndex = data.pendingCheckpointIndex_;
auto &enableChecksum = data.enableChecksum_;
auto &protocolVersion = data.threadProtocolVersion_;
BlockDetails blockDetails;
auto guard = folly::makeGuard([&socket, &threadStats] {
if (threadStats.getErrorCode() != OK) {
threadStats.incrFailedAttempts();
}
});
ErrorCode transferStatus = (ErrorCode)buf[off++];
if (transferStatus != OK) {
// TODO: use this status information to implement fail fast mode
VLOG(1) << "sender entered into error state "
<< errorCodeToStr(transferStatus);
}
int16_t headerLen = folly::loadUnaligned<int16_t>(buf + off);
headerLen = folly::Endian::little(headerLen);
VLOG(2) << "Processing FILE_CMD, header len " << headerLen;
if (headerLen > numRead) {
int64_t end = oldOffset + numRead;
numRead =
readAtLeast(socket, buf + end, bufferSize - end, headerLen, numRead);
}
if (numRead < headerLen) {
LOG(ERROR) << "Unable to read full header " << headerLen << " " << numRead;
threadStats.setErrorCode(SOCKET_READ_ERROR);
return ACCEPT_WITH_TIMEOUT;
}
off += sizeof(int16_t);
bool success = Protocol::decodeHeader(protocolVersion, buf, off,
numRead + oldOffset, blockDetails);
int64_t headerBytes = off - oldOffset;
// transferred header length must match decoded header length
WDT_CHECK(headerLen == headerBytes);
threadStats.addHeaderBytes(headerBytes);
if (!success) {
LOG(ERROR) << "Error decoding at"
<< " ooff:" << oldOffset << " off: " << off
<< " numRead: " << numRead;
threadStats.setErrorCode(PROTOCOL_ERROR);
return WAIT_FOR_FINISH_WITH_THREAD_ERROR;
}
// received a well formed file cmd, apply the pending checkpoint update
checkpointIndex = pendingCheckpointIndex;
VLOG(1) << "Read id:" << blockDetails.fileName
<< " size:" << blockDetails.dataSize << " ooff:" << oldOffset
<< " off: " << off << " numRead: " << numRead;
FileWriter writer(threadIndex, &blockDetails, fileCreator_.get());
if (writer.open() != OK) {
threadStats.setErrorCode(FILE_WRITE_ERROR);
return SEND_ABORT_CMD;
}
int32_t checksum = 0;
int64_t remainingData = numRead + oldOffset - off;
int64_t toWrite = remainingData;
WDT_CHECK(remainingData >= 0);
if (remainingData >= blockDetails.dataSize) {
toWrite = blockDetails.dataSize;
}
threadStats.addDataBytes(toWrite);
if (enableChecksum) {
checksum = folly::crc32c((const uint8_t *)(buf + off), toWrite, checksum);
}
if (throttler_) {
// We might be reading more than we require for this file but
// throttling should make sense for any additional bytes received
// on the network
throttler_->limit(toWrite + headerBytes);
}
ErrorCode code = writer.write(buf + off, toWrite);
if (code != OK) {
threadStats.setErrorCode(code);
return SEND_ABORT_CMD;
}
off += toWrite;
remainingData -= toWrite;
// also means no leftOver so it's ok we use buf from start
while (writer.getTotalWritten() < blockDetails.dataSize) {
if (getCurAbortCode() != OK) {
LOG(ERROR) << "Thread marked for abort while processing a file."
<< " port : " << socket.getPort();
//.........这里部分代码省略.........
示例9: processSettingsCmd
/***PROCESS_SETTINGS_CMD***/
Receiver::ReceiverState Receiver::processSettingsCmd(ThreadData &data) {
VLOG(1) << data << " entered PROCESS_SETTINGS_CMD state ";
char *buf = data.getBuf();
auto &off = data.off_;
auto &oldOffset = data.oldOffset_;
auto &numRead = data.numRead_;
auto &senderReadTimeout = data.senderReadTimeout_;
auto &senderWriteTimeout = data.senderWriteTimeout_;
auto &threadStats = data.threadStats_;
auto &enableChecksum = data.enableChecksum_;
auto &threadProtocolVersion = data.threadProtocolVersion_;
Settings settings;
int senderProtocolVersion;
bool success = Protocol::decodeVersion(
buf, off, oldOffset + Protocol::kMaxVersion, senderProtocolVersion);
if (!success) {
LOG(ERROR) << "Unable to decode version " << data.threadIndex_;
threadStats.setErrorCode(PROTOCOL_ERROR);
return WAIT_FOR_FINISH_WITH_THREAD_ERROR;
}
if (senderProtocolVersion != threadProtocolVersion) {
LOG(ERROR) << "Receiver and sender protocol version mismatch "
<< senderProtocolVersion << " " << threadProtocolVersion;
int negotiatedProtocol = Protocol::negotiateProtocol(senderProtocolVersion,
threadProtocolVersion);
if (negotiatedProtocol == 0) {
LOG(WARNING) << "Can not support sender with version "
<< senderProtocolVersion << ", aborting!";
threadStats.setErrorCode(VERSION_INCOMPATIBLE);
return SEND_ABORT_CMD;
} else {
LOG_IF(INFO, threadProtocolVersion != negotiatedProtocol)
<< "Changing receiver protocol version to " << negotiatedProtocol;
threadProtocolVersion = negotiatedProtocol;
if (negotiatedProtocol != senderProtocolVersion) {
threadStats.setErrorCode(VERSION_MISMATCH);
return SEND_ABORT_CMD;
}
}
}
success = Protocol::decodeSettings(
threadProtocolVersion, buf, off,
oldOffset + Protocol::kMaxVersion + Protocol::kMaxSettings, settings);
if (!success) {
LOG(ERROR) << "Unable to decode settings cmd " << data.threadIndex_;
threadStats.setErrorCode(PROTOCOL_ERROR);
return WAIT_FOR_FINISH_WITH_THREAD_ERROR;
}
auto senderId = settings.transferId;
if (transferId_ != senderId) {
LOG(ERROR) << "Receiver and sender id mismatch " << senderId << " "
<< transferId_;
threadStats.setErrorCode(ID_MISMATCH);
return SEND_ABORT_CMD;
}
senderReadTimeout = settings.readTimeoutMillis;
senderWriteTimeout = settings.writeTimeoutMillis;
enableChecksum = settings.enableChecksum;
if (settings.sendFileChunks) {
// We only move to SEND_FILE_CHUNKS state, if download resumption is enabled
// in the sender side
numRead = off = 0;
return SEND_FILE_CHUNKS;
}
auto msgLen = off - oldOffset;
numRead -= msgLen;
return READ_NEXT_CMD;
}
示例10: waitForFinishOrNewCheckpoint
Receiver::ReceiverState Receiver::waitForFinishOrNewCheckpoint(
ThreadData &data) {
VLOG(1) << data << " entered WAIT_FOR_FINISH_OR_NEW_CHECKPOINT state ";
auto &threadStats = data.threadStats_;
auto &senderReadTimeout = data.senderReadTimeout_;
auto &checkpointIndex = data.checkpointIndex_;
auto &newCheckpoints = data.newCheckpoints_;
char *buf = data.getBuf();
auto &socket = data.socket_;
// should only be called if there are no errors
WDT_CHECK(threadStats.getErrorCode() == OK);
std::unique_lock<std::mutex> lock(mutex_);
// we have to check for checkpoints before checking to see if session ended or
// not. because if some checkpoints have not been sent back to the sender,
// session should not end
newCheckpoints = getNewCheckpoints(checkpointIndex);
if (!newCheckpoints.empty()) {
return SEND_GLOBAL_CHECKPOINTS;
}
waitingThreadCount_++;
if (areAllThreadsFinished(false)) {
endCurGlobalSession();
endCurThreadSession(data);
return SEND_DONE_CMD;
}
// we must send periodic wait cmd to keep the sender thread alive
while (true) {
WDT_CHECK(senderReadTimeout > 0); // must have received settings
int timeoutMillis = senderReadTimeout / kWaitTimeoutFactor;
auto waitingTime = std::chrono::milliseconds(timeoutMillis);
START_PERF_TIMER
conditionAllFinished_.wait_for(lock, waitingTime);
RECORD_PERF_RESULT(PerfStatReport::RECEIVER_WAIT_SLEEP)
// check if transfer finished or not
if (hasCurSessionFinished(data)) {
endCurThreadSession(data);
return SEND_DONE_CMD;
}
// check to see if any new checkpoints were added
newCheckpoints = getNewCheckpoints(checkpointIndex);
if (!newCheckpoints.empty()) {
waitingThreadCount_--;
return SEND_GLOBAL_CHECKPOINTS;
}
// must unlock because socket write could block for long time, as long as
// the write timeout, which is 5sec by default
lock.unlock();
// send WAIT cmd to keep sender thread alive
buf[0] = Protocol::WAIT_CMD;
if (socket.write(buf, 1) != 1) {
PLOG(ERROR) << data << " unable to write WAIT ";
threadStats.setErrorCode(SOCKET_WRITE_ERROR);
lock.lock();
// we again have to check if the session has finished or not. while
// writing WAIT cmd, some other thread could have ended the session, so
// going back to ACCEPT_WITH_TIMEOUT state would be wrong
if (!hasCurSessionFinished(data)) {
waitingThreadCount_--;
return ACCEPT_WITH_TIMEOUT;
}
endCurThreadSession(data);
return END;
}
threadStats.addHeaderBytes(1);
lock.lock();
}
}
示例11: sendFileChunks
Receiver::ReceiverState Receiver::sendFileChunks(ThreadData &data) {
LOG(INFO) << data << " entered SEND_FILE_CHUNKS state ";
char *buf = data.getBuf();
auto bufferSize = data.bufferSize_;
auto &socket = data.socket_;
auto &threadStats = data.threadStats_;
auto &senderReadTimeout = data.senderReadTimeout_;
int64_t toWrite;
int64_t written;
std::unique_lock<std::mutex> lock(mutex_);
while (true) {
switch (sendChunksStatus_) {
case SENT: {
lock.unlock();
buf[0] = Protocol::ACK_CMD;
toWrite = 1;
written = socket.write(buf, toWrite);
if (written != toWrite) {
LOG(ERROR) << "Socket write error " << toWrite << " " << written;
threadStats.setErrorCode(SOCKET_READ_ERROR);
return ACCEPT_WITH_TIMEOUT;
}
threadStats.addHeaderBytes(toWrite);
return READ_NEXT_CMD;
}
case IN_PROGRESS: {
lock.unlock();
buf[0] = Protocol::WAIT_CMD;
toWrite = 1;
written = socket.write(buf, toWrite);
if (written != toWrite) {
LOG(ERROR) << "Socket write error " << toWrite << " " << written;
threadStats.setErrorCode(SOCKET_READ_ERROR);
return ACCEPT_WITH_TIMEOUT;
}
threadStats.addHeaderBytes(toWrite);
WDT_CHECK(senderReadTimeout > 0); // must have received settings
int timeoutMillis = senderReadTimeout / kWaitTimeoutFactor;
auto waitingTime = std::chrono::milliseconds(timeoutMillis);
lock.lock();
conditionFileChunksSent_.wait_for(lock, waitingTime);
continue;
}
case NOT_STARTED: {
// This thread has to send file chunks
sendChunksStatus_ = IN_PROGRESS;
lock.unlock();
auto guard = folly::makeGuard([&] {
lock.lock();
sendChunksStatus_ = NOT_STARTED;
conditionFileChunksSent_.notify_one();
});
const auto &parsedFileChunksInfo =
transferLogManager_.getParsedFileChunksInfo();
int64_t off = 0;
buf[off++] = Protocol::CHUNKS_CMD;
const int64_t numParsedChunksInfo = parsedFileChunksInfo.size();
Protocol::encodeChunksCmd(buf, off, bufferSize, numParsedChunksInfo);
written = socket.write(buf, off);
if (written > 0) {
threadStats.addHeaderBytes(written);
}
if (written != off) {
LOG(ERROR) << "Socket write error " << off << " " << written;
threadStats.setErrorCode(SOCKET_READ_ERROR);
return ACCEPT_WITH_TIMEOUT;
}
int64_t numEntriesWritten = 0;
// we try to encode as many chunks as possible in the buffer. If a
// single
// chunk can not fit in the buffer, it is ignored. Format of encoding :
// <data-size><chunk1><chunk2>...
while (numEntriesWritten < numParsedChunksInfo) {
off = sizeof(int32_t);
int64_t numEntriesEncoded = Protocol::encodeFileChunksInfoList(
buf, off, bufferSize, numEntriesWritten, parsedFileChunksInfo);
int32_t dataSize = folly::Endian::little(off - sizeof(int32_t));
folly::storeUnaligned<int32_t>(buf, dataSize);
written = socket.write(buf, off);
if (written > 0) {
threadStats.addHeaderBytes(written);
}
if (written != off) {
break;
}
numEntriesWritten += numEntriesEncoded;
}
if (numEntriesWritten != numParsedChunksInfo) {
LOG(ERROR) << "Could not write all the file chunks "
<< numParsedChunksInfo << " " << numEntriesWritten;
threadStats.setErrorCode(SOCKET_WRITE_ERROR);
return ACCEPT_WITH_TIMEOUT;
}
// try to read ack
int64_t toRead = 1;
int64_t numRead = socket.read(buf, toRead);
if (numRead != toRead) {
LOG(ERROR) << "Socket read error " << toRead << " " << numRead;
threadStats.setErrorCode(SOCKET_READ_ERROR);
return ACCEPT_WITH_TIMEOUT;
//.........这里部分代码省略.........