Skip to content

Commit 2695233

Browse files
committed
refactor: send metadata fn in datachannel subject
1 parent 065b554 commit 2695233

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/conductor.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,7 @@ void Conductor::SendMetadata(std::shared_ptr<DataChannelSubject> datachannel, st
200200
}
201201
try {
202202
MetaMessage metadata(path);
203-
auto body = metadata.ToString();
204-
int body_size = body.length();
205-
auto header = std::to_string(body_size);
206-
int header_size = header.length();
207-
208-
datachannel->Send(CommandType::METADATA, (uint8_t *)header.c_str(), header_size);
209-
datachannel->Send(CommandType::METADATA, (uint8_t *)body.c_str(), body_size);
210-
datachannel->Send(CommandType::METADATA, nullptr, 0);
203+
datachannel->Send(metadata);
211204
} catch (const std::exception &e) {
212205
ERROR_PRINT("%s", e.what());
213206
}

src/data_channel_subject.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ void DataChannelSubject::Send(CommandType type, const uint8_t *data, size_t size
8181

8282
while (bytes_read < size) {
8383
if (data_channel_->buffered_amount() + CHUNK_SIZE > data_channel_->MaxSendQueueSize()) {
84-
sleep(1);
85-
DEBUG_PRINT("Sleeping for 1 second due to MaxSendQueueSize reached.");
84+
usleep(100);
85+
DEBUG_PRINT("Sleeping for 100 microsecond due to MaxSendQueueSize reached.");
8686
continue;
8787
}
8888
int read_size = std::min(CHUNK_SIZE - header_size, size - bytes_read);
@@ -104,6 +104,18 @@ void DataChannelSubject::Send(const uint8_t *data, size_t size) {
104104
data_channel_->Send(data_buffer);
105105
}
106106

107+
void DataChannelSubject::Send(MetaMessage metadata) {
108+
auto type = CommandType::METADATA;
109+
auto body = metadata.ToString();
110+
int body_size = body.length();
111+
auto header = std::to_string(body_size);
112+
int header_size = header.length();
113+
114+
Send(type, (uint8_t *)header.c_str(), header_size);
115+
Send(type, (uint8_t *)body.c_str(), body_size);
116+
Send(type, nullptr, 0);
117+
}
118+
107119
void DataChannelSubject::Send(Buffer image) {
108120
const int file_size = image.length;
109121
auto type = CommandType::SNAPSHOT;

src/data_channel_subject.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class DataChannelSubject : public webrtc::DataChannelObserver,
8585
std::shared_ptr<Observable<std::string>> AsObservable(CommandType type);
8686
void UnSubscribe() override;
8787

88-
void Send(CommandType type, const uint8_t *data, size_t size);
88+
void Send(MetaMessage metadata);
8989
void Send(Buffer image);
9090
void Send(std::ifstream &file);
9191
void SetDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel);
@@ -95,6 +95,7 @@ class DataChannelSubject : public webrtc::DataChannelObserver,
9595
std::map<CommandType, std::vector<std::shared_ptr<Observable<std::string>>>> observers_map_;
9696

9797
void Send(const uint8_t *data, size_t size);
98+
void Send(CommandType type, const uint8_t *data, size_t size);
9899
};
99100

100101
#endif // DATA_CHANNEL_H_

0 commit comments

Comments
 (0)