49 struct sockaddr_in PeerAddr;
50 long int BytesReceived = 0;
52 int PeerLength =
sizeof(
struct sockaddr_in);
54 socklen_t PeerLength =
sizeof(
struct sockaddr_in);
58 #if defined(WIN32) && !defined(__MINGW64__)
59 #pragma warning (push)
60 #pragma warning (disable : 4267)
62 BytesReceived = recvfrom(
m_Ip->
Sock(), (
char*)buf, len, 0, (
struct sockaddr *)&PeerAddr, &PeerLength);
63 #if defined(WIN32) && !defined(__MINGW64__)
66 if (BytesReceived<0) {
68 std::cerr <<
"Failed to receive from socket: " <<
SOCKET_STR_ERR <<std::endl;
70 }
else if (BytesReceived==0) {
71 std::cerr <<
"Socket connection closed!\n";
76 *RxLen = BytesReceived;
86 int PeerLength =
sizeof(
struct sockaddr_in);
88 socklen_t PeerLength =
sizeof(
struct sockaddr_in);
94 #if defined(WIN32) && !defined(__MINGW64__)
95 #pragma warning (push)
96 #pragma warning (disable : 4267)
98 Sent = sendto(
m_Ip->
Sock(), (
char*)buf, len, 0, (
struct sockaddr *)&server, PeerLength);
99 #if defined(WIN32) && !defined(__MINGW64__)
100 #pragma warning (pop)
104 std::cerr <<
"Failed to send to socket: " <<
SOCKET_STR_ERR << std::endl;
108 std::cerr <<
"Sent 0 len datagram on socket!\n";
118 uint32_t crc32_final = 0;
119 uint32_t crc32_initial = 0;
120 uint32_t bytesWritten = 0;
122 long int bytesRcvd = 0;
127 file.open(filePath, std::ios::in | std::ios::binary);
129 std::cerr <<
"Cannot open file: `" << filePath <<
"`\n";
133 }
catch (
const std::exception &e) {
134 std::cerr <<
"Exception accessing `" << filePath <<
"` Error: " << e.what() << std::endl;
138 file.seekg(0, file.end);
139 long long fileLength = file.tellg();
140 file.seekg(0, file.beg);
147 crc32_initial =
crc32(crc32_initial, chunk,
static_cast<uint32_t
>(l));
149 }
while(!file.eof());
151 crc32_final = crc32_initial;
154 file.seekg(0, file.beg);
158 std::cerr <<
"Update file too large (" << fileLength <<
"B), can't send\n";
171 std::cerr <<
"Error: Failed to read chunk from File\n";
176 Messages::MessageFileBlock Block(
static_cast<uint32_t
>(fileLength), bytesWritten, crc32_final,
static_cast<uint32_t
>(l), blockSeq++, chunk);
177 sent =
Send((uint8_t*)&Block,
sizeof(Block));
178 if (sent!=
sizeof(Block))
180 std::cerr <<
"Warning: Truncated send expected: " <<
sizeof(Block) <<
" sent: " << sent << std::endl;
183 bytesWritten +=
static_cast<uint32_t
>(l);
185 bytesRcvd =
Receive((uint8_t *)&BlockAck,
sizeof(BlockAck), NULL);
188 std::cerr <<
"Socket timed out waiting for ACK, check connections and try again\n";
193 if (bytesRcvd < (
long int)
sizeof(BlockAck))
195 std::cerr <<
"Invalid ACK\n";
198 if (BlockAck.Sequence != Block.Sequence)
200 std::cerr <<
"Invalid Sequence, Resending block\n";
204 std::cout <<
"Sending Block (" << BlockAck.Sequence <<
"/" << numBlocks <<
")\r";
206 }
while(!file.eof());
208 std::cout <<
"Finished sending file to camera!\n";