| 57 | } |
| 58 | |
| 59 | bool SharedMemoryWriter::write(const std::vector<uint8_t>& buffer, const int64_t timestamp) { |
| 60 | using namespace boost::interprocess; |
| 61 | using namespace boost::posix_time; |
| 62 | |
| 63 | try { |
| 64 | if (!shared_segment_->ptr_) |
| 65 | // while (!shared_segment_->mtx_->timed_lock(microsec_clock::universal_time() + |
| 66 | // microseconds(max_lock_wait_microsecond_))) |
| 67 | { |
| 68 | // printf("SharedMemeory icannot get lock,remove lock\n"); |
| 69 | if (!reset()) { |
| 70 | printf("SharedMemory write init failed\n"); |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (shared_segment_->ptr_) { |
| 76 | int sz = buffer.size(); |
| 77 | int ptr_sz = sizeof(int) + sizeof(int64_t) + sizeof(int) + sz + 1024; |
| 78 | if (shared_segment_->ptr_->size() < ptr_sz) { |
| 79 | shared_segment_->ptr_->resize(ptr_sz); |
| 80 | } |
| 81 | char* buf = shared_segment_->ptr_->data(); |
| 82 | memcpy(buf, &kMagicNumber_, sizeof(int)); |
| 83 | buf += sizeof(int); |
| 84 | memcpy(buf, ×tamp, sizeof(int64_t)); |
| 85 | buf += sizeof(int64_t); |
| 86 | memcpy(buf, &sz, sizeof(int)); |
| 87 | buf += sizeof(int); |
| 88 | memcpy(buf, buffer.data(), sz); |
| 89 | // shared_segment_->mtx_->unlock(); |
| 90 | } |
| 91 | } catch (...) { |
| 92 | printf("SharedMemory write failed\n"); |
| 93 | // shared_segment_->mtx_->unlock(); |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | bool SharedMemoryWriter::remove() { |
| 101 | // if (shared_segment_) |