packet Resolver::xchg(packet const& q) { if (Config::nameservers[ns_].typ == Config::sock_type::stream) { CHECK_EQ(ns_fd_, -1); uint16_t sz = htons(std::size(q)); ns_sock_->out().write(reinterpret_cast<char const*>(&sz), sizeof sz); ns_sock_->out().write(reinterpret_cast<char const*>(begin(q)), size(q)); ns_sock_->out().flush(); sz = 0; ns_sock_->in().read(reinterpret_cast<char*>(&sz), sizeof sz); sz = ntohs(sz); DNS::packet::container_t bfr(sz); ns_sock_->in().read(reinterpret_cast<char*>(bfr.data()), sz); if (!ns_sock_->in()) { LOG(WARNING) << "Resolver::xchg was able to read only " << ns_sock_->in().gcount() << " octets"; } return packet{std::move(bfr)}; } CHECK(Config::nameservers[ns_].typ == Config::sock_type::dgram); CHECK_GE(ns_fd_, 0); CHECK_EQ(send(ns_fd_, std::begin(q), std::size(q), 0), std::size(q)); auto sz = Config::max_udp_sz; DNS::packet::container_t bfr(sz); auto constexpr hook{[]() {}}; auto t_o{false}; auto const a_buf = reinterpret_cast<char*>(bfr.data()); auto const a_buflen = POSIX::read(ns_fd_, a_buf, int(sz), hook, Config::read_timeout, t_o); if (a_buflen < 0) { LOG(WARNING) << "DNS read failed"; return packet{0}; } if (t_o) { LOG(WARNING) << "DNS read timed out"; return packet{0}; } sz = a_buflen; bfr.resize(sz); bfr.shrink_to_fit(); return packet{std::move(bfr)}; }
void processor(const std::string& ifname, const std::string& ofname) { binFileReader bfr(ifname); if (!bfr.fileOk){ return; } binFileWriter bfw(ofname); if (!bfw.fileOk) { return; } uint32_t maxTime = 0; bool isRecordValid; dataStruct1_t ds1; while (bfr.readDataStruct(ds1)) { isRecordValid = true; if ((!acceptableOperations.count(ds1.type)) || (ds1.time + 2 <= maxTime)) { isRecordValid = false; } if (maxTime < ds1.time) { maxTime = ds1.time; } if (isRecordValid) { bfw.writeDataStruct(ds1); } } }
void String::readObject(std::streambuf & istrm) { UInt32 len; BinarySerialization::readLen(istrm, len); AutoPtrVec<char> bfr(new char[len+1]); BinarySerialization::read(istrm, bfr.get(), len); bfr[len] = '\0'; m_buf = new ByteBuf(bfr, len); }
String::String(Real64 val) : m_buf(NULL) { char tmpbuf[128]; int len = ::snprintf(tmpbuf, sizeof(tmpbuf), "%lf", val); AutoPtrVec<char> bfr(new char[len+1]); ::snprintf(bfr.get(), len+1, "%lf", val); m_buf = new ByteBuf(bfr, len); }
String::String(Int64 val) : m_buf(NULL) { char tmpbuf[32]; int len = ::snprintf(tmpbuf, sizeof(tmpbuf), "%llu", (long long)val); AutoPtrVec<char> bfr(new char[len+1]); ::snprintf(bfr.get(), len+1, "%llu", (long long)val); m_buf = new ByteBuf(bfr, len); }
void processor(const std::string& ifname, const std::string& ofname) { binFileReader bfr(ifname); if (!bfr.fileOk){ return; } binFileWriter bfw(ofname); if (!bfw.fileOk) { return; } uint32_t messageSize; uint32_t currentTime = 0; uint32_t secondsCount = 0; static const int buffSize = 2048; counting_map_t buffersSizes; counting_map_t messagesCount; dataStruct1_t ds1; //input while (bfr.readDataStruct(ds1)) { //new second if (currentTime != ds1.time) { currentTime = ds1.time; secondsCount++; buffersSizes.clear(); } messageSize = sizeof ds1.type + sizeof ds1.time + sizeof ds1.len + (sizeof(char)) * ds1.len; if (!buffersSizes.count(ds1.type)) { buffersSizes[ds1.type] = buffSize - messageSize; if(buffersSizes[ds1.type] > 0){ messagesCount[ds1.type] += 1; } } else { buffersSizes[ds1.type] = buffersSizes[ds1.type] - messageSize; if (buffersSizes[ds1.type] > 0) { messagesCount[ds1.type] += 1; } } } //output double avgMsg; for (counting_map_t::const_iterator it = messagesCount.begin(); it != messagesCount.end(); ++it) { bfw.writeRawValue(it->first); avgMsg = (double) it->second / secondsCount; bfw.writeRawValue(avgMsg); } }
size_t TMC_RemoteDevice::Write(const uint8_t * msg, size_t len) { std::vector<uint8_t> bfr(len + PKT_HEADER_SIZE); std::copy(msg, msg + len, bfr.begin() + PKT_HEADER_SIZE); bfr[0] = 10; bfr[1] = 0; bfr[2] = seqNum++; bfr[3] = ~bfr[2]; *(uint32_t*)&(bfr)[4] = htonl(len); *(uint32_t*)&(bfr)[8] = 0; conn.SendMessage(&bfr[0], bfr.size()); return len; }
String& String::concat(char arg) { size_t newlen = length() + 1; AutoPtrVec<char> bfr(new char[newlen+1]); bfr[0] = 0; if (m_buf) { ::strcpy(bfr.get(), m_buf->data()); } *(bfr.get()+length()) = arg; *(bfr.get()+newlen) = 0; m_buf = new ByteBuf(bfr, newlen); return *this; }
String::String(const char* str, size_t len) : m_buf(NULL) { if (NULL == str) { m_buf = 0; } else { AutoPtrVec<char> bfr(new char[len+1]); ::memcpy(bfr.get(), str, len); bfr[len] = '\0'; m_buf = new ByteBuf(bfr, len); } }
String& String::concat(const char* arg) { if (arg && *arg) { size_t len = length() + ::strlen(arg); AutoPtrVec<char> bfr(new char[len+1]); bfr[0] = 0; if (m_buf) { ::strcpy(bfr.get(), m_buf->data()); } ::strcat(bfr.get(), arg); m_buf = new ByteBuf(bfr, len); } return *this; }
TMC_RemoteDevice::TMC_RemoteDevice(uint16_t vendID, uint16_t prodID, const std::string & sernum, int sockfd): conn(sockfd), seqNum(0) { // conn.SetBlocking(); std::vector<uint8_t> bfr(sernum.size() + PKT_HEADER_SIZE); std::copy(sernum.begin(), sernum.end(), bfr.begin() + PKT_HEADER_SIZE); bfr[0] = 1; bfr[1] = 1; bfr[2] = seqNum++; bfr[3] = ~bfr[2]; *(uint32_t*)&(bfr)[4] = htonl(sernum.size()); *(uint32_t*)&(bfr)[8] = htonl((uint32_t)vendID << 16 | prodID); // *(uint16_t*)&(bfr)[8] = htons(vendID); // *(uint16_t*)&(bfr)[10] = htons(prodID); conn.SendMessage(&bfr[0], bfr.size()); }