setup more proper logging and get stuff working maybe

This commit is contained in:
2025-03-26 20:57:23 -05:00
parent ea952e1981
commit ed8c594a30
5 changed files with 234 additions and 25 deletions

View File

@@ -4,7 +4,24 @@
#include <opencv2/core/mat.hpp>
#include <opencv2/imgcodecs.hpp>
#include <sys/socket.h>
#include <iostream>
#include <logging.h>
using namespace std;
struct imageHeader {
size_t size;
chrono::milliseconds timestamp;
};
chrono::milliseconds getMillis() {
// Get current time
const auto now = chrono::system_clock::now();
// Get time since epoch in milliseconds
const chrono::duration ms = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch());
return chrono::milliseconds(ms.count());
}
inline void serializeImage(const cv::Mat& image, std::vector<uchar>& buffer) {
cv::imencode(".jpg", image, buffer);
@@ -16,9 +33,13 @@ int sendImage(int socket, const cv::Mat& image, std::vector<uchar>& buffer) {
// first send the size of the serialized image
const size_t size = buffer.size();
std::cout << "Buffer size: " << size << std::endl;
if (const ssize_t sent = send(socket, &size, sizeof(size), 0); sent == -1) {
perror("Error sending data");
const chrono::milliseconds timestamp = getMillis();
imageHeader header;
header.size = size;
header.timestamp = timestamp;
trace("Buffer size: ", size);
if (const ssize_t sent = send(socket, &header, sizeof(header), 0); sent == -1) {
error("Error sending data header");
return -1;
}
@@ -26,25 +47,27 @@ int sendImage(int socket, const cv::Mat& image, std::vector<uchar>& buffer) {
while (totalSent < size) {
const ssize_t sent = send(socket, buffer.data() + totalSent, size - totalSent, 0);
if (sent == -1) {
perror("Error sending data");
error("Error sending data");
return -1;
}
totalSent += sent;
std::cout << "Packet sent (" << sent << " bytes, total " << totalSent << " bytes)" << std::endl;
debug("Packet sent (", sent, " bytes, total ", totalSent, " bytes)");
}
return 0;
}
int recvImage(int socket, std::vector<uchar>& buffer) {
size_t dataSize;
// first receive the size of the image
ssize_t sizeReceived = recv(socket, &dataSize, sizeof(size_t), 0);
if (sizeReceived <= 0) {
imageHeader header;
if (ssize_t recvd = recv(socket, &header, sizeof(imageHeader), 0); recvd <= 0) {
error("Error receiving data header");
return -1;
}
std::cout << "Buffer size: " << dataSize << std::endl;
size_t dataSize = header.size;
chrono::milliseconds sentTime = header.timestamp;
trace("Buffer size: ", dataSize);
// resize the buffer to fit the whole image
buffer.resize(dataSize);
@@ -54,18 +77,23 @@ int recvImage(int socket, std::vector<uchar>& buffer) {
while (totalReceived < dataSize) {
ssize_t bytesReceived = recv(socket, buffer.data() + totalReceived, dataSize, 0);
if (bytesReceived <= 0) {
error("Error receiving data");
buffer.clear();
return -1;
}
totalReceived += bytesReceived;
std::cout << "Packet received (" << bytesReceived << " bytes, total " << totalReceived << " bytes)" << std::endl;
debug("Packet received (", bytesReceived, " bytes, total ", totalReceived, " bytes)");
}
chrono::milliseconds currentTime = getMillis();
chrono::milliseconds diff = currentTime - sentTime;
debug("Packet latency: ", diff.count(), "ms");
return 0;
}
bool applyImage(cv::Mat& image, std::vector<uchar> *src) {
// decode the image into an OpenCV Mat
cv::imdecode(*src, 0, &image);
cv::imdecode(*src, cv::IMREAD_UNCHANGED, &image);
return true;
}
#endif //TRANSFER_H