void writeInternal(const std::vector<T>& values) { writeInternal(values.size()); for (const T& value: values) { writeInternal(value); } }
void SendThread::write(BoxMessage* message) { int len = message->length; if (len < 0) { len = len & 0xFFFF; } if (len <= BoxMessage::PACKAGE_MAX_LENGTH) { writeInternal(message->marshal()); return; } if ( message->data.GetPt() == NULL) return; BoxMessage* msg = new BoxMessage(message->command); if (bigDataBuffer->get_capcity() < len) { bigDataBuffer = new RingBytesBuffer(len + BoxMessage::PACKAGE_MAX_LENGTH); } bigDataBuffer->push(message->data); while (bigDataBuffer->readable() >= BoxMessage::PACKAGE_MAX_LENGTH) { msg->data = bigDataBuffer->pop(BoxMessage::PACKAGE_MAX_LENGTH); writeInternal(msg->marshal()); } if(bigDataBuffer->readable()>0){ msg->data= bigDataBuffer->popAll(); writeInternal(msg->marshal()); } }
void SerialTunnel::sendCommand(uint8_t cmdId) { if(cmdId < 0 || cmdId > 0x1e) { lcd.print("invalid command :"); lcd.print(cmdId, HEX); } writeInternal(cmdId | 0x40); }
void SerialTunnel::sendData(uint8_t dataValue) { if(dataValue < 0 || dataValue > 0x40) { lcd.print("invalid data: "); lcd.print(dataValue, HEX); } writeInternal(dataValue); }
void SerialTunnel::sendByte(uint8_t b) { if(b < 0 || b > 95) { lcd.print("oob byte: "); lcd.print(b, HEX); } writeInternal(b); }
bool FileOutputStream::write (const void* const src, const size_t numBytes) { jassert (src != nullptr && ((ssize_t) numBytes) >= 0); if (bytesInBuffer + numBytes < bufferSize) { memcpy (buffer + bytesInBuffer, src, numBytes); bytesInBuffer += numBytes; currentPosition += numBytes; } else { if (! flushBuffer()) return false; if (numBytes < bufferSize) { memcpy (buffer + bytesInBuffer, src, numBytes); bytesInBuffer += numBytes; currentPosition += numBytes; } else { const ssize_t bytesWritten = writeInternal (src, numBytes); if (bytesWritten < 0) return false; currentPosition += bytesWritten; return bytesWritten == (ssize_t) numBytes; } } return true; }
void writeRequested(int connfd,char* protocol,char* url){ struct stat fs; char response[1024]; char* page; char* contType; contType = contentType(url); int fd = open(url+1, O_RDONLY); if(fd==-1){ writeFileNotFound(connfd,protocol); return; } if(fstat(fd,&fs)==-1){ printf("Could Not retrieve file size!\n"); writeInternal(connfd,protocol); } page = (char*)malloc(fs.st_size); read(fd,page,fs.st_size); sprintf(response,"%s 200 OK\nContent-Type: %s\nContent-Length: %d\r\n\r\n",protocol,contType,(int)fs.st_size); if(write(connfd,response,strlen(response))==-1){ fprintf(stderr,"200 Errno %d: %s\n",errno,strerror(errno)); } if(write(connfd,page,fs.st_size)==-1){ fprintf(stderr,"200 Errno %d: %s\n",errno,strerror(errno)); } printf("Returning Response from Thread %lu\n",pthread_self()); printf("****Writing Response****\n%s\n",response); close(fd); free(page); }
void XmlFileWriter::write(const char *pcszFilename, bool fSafe) { if (!fSafe) writeInternal(pcszFilename, fSafe); else { /* Empty string and directory spec must be avoid. */ if (RTPathFilename(pcszFilename) == NULL) throw xml::LogicError(RT_SRC_POS); /* Construct both filenames first to ease error handling. */ char szTmpFilename[RTPATH_MAX]; int rc = RTStrCopy(szTmpFilename, sizeof(szTmpFilename) - strlen(s_pszTmpSuff), pcszFilename); if (RT_FAILURE(rc)) throw EIPRTFailure(rc, "RTStrCopy"); strcat(szTmpFilename, s_pszTmpSuff); char szPrevFilename[RTPATH_MAX]; rc = RTStrCopy(szPrevFilename, sizeof(szPrevFilename) - strlen(s_pszPrevSuff), pcszFilename); if (RT_FAILURE(rc)) throw EIPRTFailure(rc, "RTStrCopy"); strcat(szPrevFilename, s_pszPrevSuff); /* Write the XML document to the temporary file. */ writeInternal(szTmpFilename, fSafe); /* Make a backup of any existing file (ignore failure). */ uint64_t cbPrevFile; rc = RTFileQuerySize(pcszFilename, &cbPrevFile); if (RT_SUCCESS(rc) && cbPrevFile >= 16) RTFileRename(pcszFilename, szPrevFilename, RTPATHRENAME_FLAGS_REPLACE); /* Commit the temporary file. Just leave the tmp file behind on failure. */ rc = RTFileRename(szTmpFilename, pcszFilename, RTPATHRENAME_FLAGS_REPLACE); if (RT_FAILURE(rc)) throw EIPRTFailure(rc, "Failed to replace '%s' with '%s'", pcszFilename, szTmpFilename); /* Flush the directory changes (required on linux at least). */ RTPathStripFilename(szTmpFilename); rc = RTDirFlush(szTmpFilename); AssertMsg(RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED || rc == VERR_NOT_IMPLEMENTED, ("%Rrc\n", rc)); } }
void QJsonPipe::outReady(int) { Q_D(QJsonPipe); Q_ASSERT(d->mOut); d->mOut->setEnabled(false); if (d->mOutBuffer.size()) { writeInternal(d->mOut->socket()); if (d->mOut && !d->mOutBuffer.isEmpty()) d->mOut->setEnabled(true); } }
bool FileOutputStream::flushBuffer() { bool ok = true; if (bytesInBuffer > 0) { ok = (writeInternal (buffer, bytesInBuffer) == (ssize_t) bytesInBuffer); bytesInBuffer = 0; } return ok; }
void Font::writeLine(float x, float y, float z, const char *text, int count, Alignment alignment) { if (count <= 0) count = getTextLength(text); if (alignment == Alignment::CENTER) { float w = getTextWidth(text, count); x -= w / 2; } else if (alignment == Alignment::RIGHT) { float w = getTextWidth(text, count); x -= w; } beginRender(); writeInternal(x, y, z, text, count); endRender(); }
ErrorCode WdtSocket::finalizeWrites(bool doTagIOs) { VLOG(1) << "Finalizing writes/encryption " << port_ << " " << fd_; ErrorCode code = OK; std::string tag; if (!encryptor_.finish(tag)) { code = ENCRYPTION_ERROR; } if (!tag.empty() && doTagIOs) { const int timeoutMs = threadCtx_.getOptions().write_timeout_millis; const int expected = tag.size(); if (writeInternal(tag.data(), tag.size(), timeoutMs, false) != expected) { PLOG(ERROR) << "Encryption Tag write error"; code = ENCRYPTION_ERROR; } } writesFinalized_ = true; return code; }
int Sequencer_oss::write(int device, int aByte) { int status = 0; switch (getOutputType(device)) { case MIDI_EXTERNAL: midi_write_packet[1] = (uchar) (0xff & aByte); midi_write_packet[2] = getOutDeviceValue(device); status = ::write(getFd(), midi_write_packet,sizeof(midi_write_packet)); break; case MIDI_INTERNAL: status = writeInternal(getOutDeviceValue(device), aByte); break; } if (status > 0) { return 1; } else { return 0; } }
int WdtSocket::write(char *buf, int nbyte, bool retry) { WDT_CHECK_GT(nbyte, 0); if (writeErrorCode_ != OK) { LOG(ERROR) << "Socket write failed before, not trying to write again " << port_; return -1; } writeEncryptionSettingsOnce(); if (writeErrorCode_ != OK) { return -1; } bool encrypt = encryptionParams_.isSet(); if (encrypt && !encryptor_.encrypt((uint8_t *)buf, nbyte, (uint8_t *)buf)) { writeErrorCode_ = ENCRYPTION_ERROR; return -1; } int written = writeInternal(buf, nbyte, options_.write_timeout_millis, retry); if (written != nbyte) { LOG(ERROR) << "Socket write failure " << written << " " << nbyte; writeErrorCode_ = SOCKET_WRITE_ERROR; } return written; }
void Font::write(float x, float y, float z, const char *text, int count, Alignment alignment) { if (count <= 0) count = getTextLength(text); // Get first line int pos = 0; int len = findTextChar(text, pos, count, '\n'); if (len == -1) len = count; beginRender(); while (pos < count) { float cx = x; if (alignment == Alignment::CENTER) { float w = getTextWidth(&text[pos], len); cx -= w / 2; } else if (alignment == Alignment::RIGHT) { float w = getTextWidth(&text[pos], len); cx -= w; } writeInternal(cx, y, z, &text[pos], len); y -= getLineHeight(); // Get next line pos += len; int ch = getTextChar(text, pos, &pos); if (ch == '\n') { len = findTextChar(text, pos, count, '\n'); if (len == -1) len = count - pos; else len = len - pos; } } endRender(); }
bool QJsonPipe::waitForBytesWritten(int msecs) { Q_D(QJsonPipe); if (!d->mOut || d->mOutBuffer.isEmpty()) return false; d->mOut->setEnabled(false); QElapsedTimer stopWatch; stopWatch.start(); while (d->mOut && !d->mOutBuffer.isEmpty()) { fd_set wfds; FD_ZERO(&wfds); FD_SET(d->mOut->socket(),&wfds); int timeout = msecs - stopWatch.elapsed(); struct timeval tv; struct timeval *tvptr = ((msecs > 0 && timeout > 0) ? &tv : NULL); if (tvptr) { tv.tv_sec = timeout / 1000; tv.tv_usec = (timeout % 1000) * 1000; } int retval = ::select(d->mOut->socket() + 1, NULL, &wfds, NULL, tvptr); if (retval == -1 && errno == EINTR) continue; if (retval <= 0) break; writeInternal(d->mOut->socket()); } if (d->mOut && !d->mOutBuffer.isEmpty()) d->mOut->setEnabled(true); return d->mOutBuffer.isEmpty(); }
void WdtSocket::writeEncryptionSettingsOnce() { if (!encryptionParams_.isSet() || encryptionSettingsWritten_) { return; } WDT_CHECK(!encryptionParams_.getSecret().empty()); int timeoutMs = threadCtx_.getOptions().write_timeout_millis; std::string iv; if (!encryptor_.start(encryptionParams_, iv)) { writeErrorCode_ = ENCRYPTION_ERROR; return; } int64_t off = 0; buf_[off++] = Protocol::ENCRYPTION_CMD; Protocol::encodeEncryptionSettings(buf_, off, off + Protocol::kMaxEncryption, encryptionParams_.getType(), iv); int written = writeInternal(buf_, off, timeoutMs, false); if (written != off) { LOG(ERROR) << "Failed to write encryption settings " << written << " " << port_; return; } encryptionSettingsWritten_ = true; }
inline void VolumeFile::write(const V& volume) { writeInternal(volume); }
void writeInternal(const std::array<T, N>& values) { for (const T& value: values) { writeInternal(value); } }
void visit(const std::string&, const T& value) { writeInternal(value); }
void writeInternal(const std::string& value) { writeInternal(value.size()); mStore.write(value.c_str(), value.size()); }
void writeInternal(const char* &value) { size_t size = std::strlen(value); writeInternal(size); mStore.write(value, size); }
void Font::writeBox(float x, float y, float z, float width, const char *text, int count, Alignment alignment) { if (count <= 0) count = getTextLength(text); float currWidth = 0, wordWidth; int lineS = 0, lineE = 0, wordS = 0, wordE = 0; int wordCount = 0; const char *s = " "; float spaceWidth = getTextWidth(s, 1); bool softBreak = false; beginRender(); for (; lineS < count;) { // Determine the extent of the line for (;;) { // Determine the number of characters in the word while (wordE < count && getTextChar(text, wordE) != ' ' && getTextChar(text, wordE) != '\n') // Advance the cursor to the next character getTextChar(text, wordE, &wordE); // Determine the width of the word if (wordE > wordS) { wordCount++; wordWidth = getTextWidth(&text[wordS], wordE - wordS); } else wordWidth = 0; // Does the word fit on the line? The first word is always accepted. if (wordCount == 1 || currWidth + (wordCount > 1 ? spaceWidth : 0) + wordWidth <= width) { // Increase the line extent to the end of the word lineE = wordE; currWidth += (wordCount > 1 ? spaceWidth : 0) + wordWidth; // Did we reach the end of the line? if (wordE == count || getTextChar(text, wordE) == '\n') { softBreak = false; // Skip the newline character if (wordE < count) // Advance the cursor to the next character getTextChar(text, wordE, &wordE); break; } // Skip the trailing space if (wordE < count && getTextChar(text, wordE) == ' ') // Advance the cursor to the next character getTextChar(text, wordE, &wordE); // Move to next word wordS = wordE; } else { softBreak = true; // Skip the trailing space if (wordE < count && getTextChar(text, wordE) == ' ') // Advance the cursor to the next character getTextChar(text, wordE, &wordE); break; } } // Write the line if (alignment == Alignment::JUSTIFY) { float spacing = 0; if (softBreak) { if (wordCount > 2) spacing = (width - currWidth) / (wordCount - 2); else spacing = (width - currWidth); } writeInternal(x, y, z, &text[lineS], lineE - lineS, spacing); } else { float cx = x; if (alignment == Alignment::RIGHT) cx = x + width - currWidth; else if (alignment == Alignment::CENTER) cx = x + 0.5f*(width - currWidth); writeInternal(cx, y, z, &text[lineS], lineE - lineS); } if (softBreak) { // Skip the trailing space if (lineE < count && getTextChar(text, lineE) == ' ') // Advance the cursor to the next character getTextChar(text, lineE, &lineE); // We've already counted the first word on the next line currWidth = wordWidth; wordCount = 1; } else { // Skip the line break if (lineE < count && getTextChar(text, lineE) == '\n') // Advance the cursor to the next character getTextChar(text, lineE, &lineE); currWidth = 0; wordCount = 0; } // Move to next line lineS = lineE; wordS = wordE; y -= getLineHeight(); } endRender(); }