new: debug and version macros, cleaning up client/server APIs

This commit is contained in:
ngn
2024-06-28 20:51:56 +03:00
parent 6c2f34e8d5
commit 3b19e2840b
25 changed files with 231 additions and 128 deletions

58
src/mptp/utils.c Normal file
View File

@ -0,0 +1,58 @@
#include "../../include/mptp.h"
#include <stdio.h>
bool lm_mptp_sendfile(int sock, struct sockaddr *addr, char *path){
lm_mptp_t packet;
bool ret = false;
if (NULL == path) {
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
goto end;
}
FILE *file = fopen(path, "r");
size_t read = 0;
if(NULL == file){
lm_mptp_init(&packet, false, MPTP_S2C_BRUH, true);
goto end;
}
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
while ((read = fread(packet.data, 1, MPTP_DATA_MAX, file)) > 0) {
packet.header.data_size = read;
lm_mptp_server_send(sock, &packet, addr);
lm_mptp_init(&packet, false, MPTP_S2C_COOL, false);
}
fclose(file);
lm_mptp_init(&packet, false, MPTP_S2C_COOL, true);
ret = true;
end:
lm_mptp_server_send(sock, &packet, addr);
return ret;
}
bool lm_mptp_recvfile(int sock, char *path){
FILE *file = fopen(path, "a");
bool ret = false;
lm_mptp_t packet;
while(lm_mptp_client_recv(sock, &packet)){
if(!lm_mptp_client_verify(&packet))
goto end;
if(MPTP_IS_LAST(&packet))
break;
if(fwrite(packet.data, 1, packet.header.data_size, file)==0)
goto end;
}
ret = true;
end:
fclose(file);
return ret;
}