Skip to content

Commit 32cb888

Browse files
committed
Implement recvfile and use in server and client
1 parent 8ea6035 commit 32cb888

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

assets/recvfile

1 KB
Binary file not shown.

assets/sendfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is data

progs/client.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <string.h>
33
#include <stdlib.h>
44
#include <netdb.h>
5+
#include <fcntl.h>
6+
#include <unistd.h>
57
#include "../include/udt.h"
68

79
#define HOST "127.0.0.1"
@@ -48,6 +50,12 @@ int main(int argc, char *argv[])
4850

4951
freeaddrinfo(result);
5052

53+
/* send file */
54+
int filefd = open("assets/sendfile", O_RDONLY);
55+
if (filefd < -1) return 2;
56+
if (udt_sendfile(sock, filefd, 0, 10, 0) < 0) return 1;
57+
close(filefd);
58+
5159
/* send, recv */
5260
char buffer[BUFFER_SIZE];
5361
size_t size;

progs/server.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdio.h>
33
#include <string.h>
44
#include <netdb.h>
5+
#include <fcntl.h>
6+
#include <unistd.h>
57

68
#include "../include/udt.h"
79

@@ -70,6 +72,13 @@ int main(int argc, char *argv[])
7072
fprintf(stdout, "Active on %s\n", PORT);
7173
}
7274

75+
/* recv file */
76+
int filefd = open("assets/recvfile", O_WRONLY | O_TRUNC | O_CREAT);
77+
int64_t offset = 0;
78+
if (filefd < 0) return 2;
79+
if (udt_recvfile(conn, filefd, &offset, 10, 0) < 0) return 1;
80+
close(filefd);
81+
7382
/* send, recv */
7483
char buffer[BUFFER_SIZE];
7584
char msg[] = "Servito ergo sum";

src/api.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,16 @@ int udt_close(socket_t sock)
109109
int64_t udt_recvfile(socket_t sock, int file, int64_t *offset, int64_t filesize,
110110
int blocksize)
111111
{
112-
if (!connection.is_connected) return -1;
113-
return recv_file_buffer_read(file, offset, filesize, blocksize);
112+
int num_read = 0;
113+
114+
do {
115+
if (connection.is_open == 0 && connection.is_connected == 0)
116+
return 0;
117+
118+
num_read = recv_file_buffer_read(file, offset, filesize, blocksize);
119+
} while (num_read == 0);
120+
121+
return num_read;
114122
}
115123

116124
int64_t udt_sendfile(socket_t sock, int file, int64_t offset, int64_t filesize,

src/recv_buffer.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <unistd.h>
2+
#include <stdio.h>
23

34
#include "packet.h"
45
#include "buffer.h"
@@ -25,14 +26,19 @@ int64_t recv_file_buffer_read(int fd, int64_t *offset, int64_t size, int64_t blo
2526
char data[PACKET_DATA_SIZE];
2627
int retval = 0;
2728
int len = 0;
29+
int read;
2830

2931
if (fd < 0) return -1;
3032

3133
while (size > 0) {
32-
buffer_read(&buffer, data, PACKET_DATA_SIZE);
34+
35+
read = buffer_read(&buffer, data, PACKET_DATA_SIZE);
36+
if (read == 0) continue;
37+
3338
len = pwrite(fd, &data, PACKET_DATA_SIZE, *offset);
34-
if (len < 0) return -1;
39+
if (len < 1) return len;
3540

41+
*offset += len;
3642
retval += len;
3743
size -= len;
3844
}

0 commit comments

Comments
 (0)