Skip to content

Commit 1dd7a90

Browse files
authored
Merge pull request #3 from UtkarshMe/send_recv_file
Implement sendfile and recvfile
2 parents ff2615a + 32cb888 commit 1dd7a90

File tree

10 files changed

+137
-9
lines changed

10 files changed

+137
-9
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

include/buffer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ int send_buffer_write (char *data, int len);
4343
int send_packet_buffer_write (packet_t *);
4444
int send_packet_buffer_read (packet_t *);
4545

46+
int64_t recv_file_buffer_read (int, int64_t *, int64_t, int64_t);
47+
int64_t send_file_buffer_write (int, int64_t, int64_t, int64_t);
48+
4649
#endif /* end of include guard: BUFFER_H_IBSOFJRY */

include/udt.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,25 @@ int udt_send (socket_t, char *, int, int);
101101
* Receive a file from the socket
102102
*
103103
* @param socket_t The socket to receive from
104-
* @param void* Pointer to the file to store incoming data
104+
* @param int The file descriptor to a writable open file
105105
* @param int64_t The offset position to store. After transfer, contains
106106
* new offset
107107
* @param int64_t The total size to be received
108108
* @param int The size of data block
109109
* @return int64_t Size of data sent, -1 on error
110110
*/
111-
int64_t udt_recvfile(socket_t, void *, int64_t *, int64_t, int);
111+
int64_t udt_recvfile(socket_t, int, int64_t *, int64_t, int);
112112

113113
/**
114114
* Send a file to the socket
115115
*
116116
* @param socket_t The socket to send to
117-
* @param void* The file buffer to send
117+
* @param int The file descriptor to a readable open file
118118
* @param int64_t The offset position to read data
119119
* @param int64_t The total size to be sent
120120
* @param int The size of data block
121121
* @return int64_t Size of data sent, -1 on error
122122
*/
123-
int64_t udt_sendfile(socket_t, void *, int64_t, int64_t, int);
123+
int64_t udt_sendfile(socket_t, int, int64_t, int64_t, int);
124124

125125
#endif /* end of include guard: UDT_H_JDNRQTZQ */

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
size_t size;
5361
char *line;

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
while (udt_recv(conn, buffer, BUFFER_SIZE, 0)) {

src/api.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,25 @@ int udt_close(socket_t sock)
105105
while (connection.is_open);
106106
return close(sock);
107107
}
108+
109+
int64_t udt_recvfile(socket_t sock, int file, int64_t *offset, int64_t filesize,
110+
int blocksize)
111+
{
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;
122+
}
123+
124+
int64_t udt_sendfile(socket_t sock, int file, int64_t offset, int64_t filesize,
125+
int blocksize)
126+
{
127+
if (!connection.is_connected) return -1;
128+
return send_file_buffer_write(file, offset, filesize, blocksize);
129+
}

src/buffer.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ int buffer_read(buffer_t *buffer, char *data, int len)
4949

5050
last = block -> last;
5151
if (pos >= len) {
52-
free(block -> data);
53-
free(block);
54-
continue;
52+
break;
5553
}
5654

5755
int n = ((len - pos) < block -> len) ? len - pos : block -> len;

src/recv_buffer.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#include <pthread.h>
1+
#include <unistd.h>
2+
#include <stdio.h>
3+
4+
#include "packet.h"
25
#include "buffer.h"
36

47
static buffer_t buffer;
@@ -17,3 +20,28 @@ int recv_buffer_read(char *data, int len)
1720
{
1821
return buffer_read(&buffer, data, len);
1922
}
23+
24+
int64_t recv_file_buffer_read(int fd, int64_t *offset, int64_t size, int64_t blocksize)
25+
{
26+
char data[PACKET_DATA_SIZE];
27+
int retval = 0;
28+
int len = 0;
29+
int read;
30+
31+
if (fd < 0) return -1;
32+
33+
while (size > 0) {
34+
35+
read = buffer_read(&buffer, data, PACKET_DATA_SIZE);
36+
if (read == 0) continue;
37+
38+
len = pwrite(fd, &data, PACKET_DATA_SIZE, *offset);
39+
if (len < 1) return len;
40+
41+
*offset += len;
42+
retval += len;
43+
size -= len;
44+
}
45+
46+
return retval;
47+
}

src/send_buffer.c

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
#include <stdio.h>
1+
#include <unistd.h>
2+
#include <sys/types.h>
3+
#include <sys/stat.h>
4+
#include <fcntl.h>
5+
26
#include "buffer.h"
37
#include "packet.h"
48

@@ -66,3 +70,58 @@ int send_buffer_write(char *data, int len)
6670

6771
return retval;
6872
}
73+
74+
int64_t send_file_buffer_write(int fd, int64_t offset, int64_t size, int64_t blocksize)
75+
{
76+
77+
packet_t packet;
78+
79+
int retval;
80+
int seqnum;
81+
char buffer[PACKET_DATA_SIZE];
82+
int boundary;
83+
int len;
84+
85+
retval = 0;
86+
seqnum = 2142894844; /* TODO: generate random number */
87+
boundary = PACKET_BOUNDARY_START;
88+
89+
if (fd < 0) return -1;
90+
91+
while (size > 0)
92+
{
93+
len = pread(fd, buffer, PACKET_DATA_SIZE, offset);
94+
if (len < 0) break;
95+
retval += len;
96+
size -= len;
97+
98+
boundary |= (size > 0) ? PACKET_BOUNDARY_NONE : PACKET_BOUNDARY_END;
99+
100+
packet_clear_header (packet);
101+
packet_set_data (packet);
102+
packet_set_seqnum (packet, seqnum++);
103+
packet_set_boundary (packet, boundary);
104+
packet_set_order (packet, 1);
105+
packet_set_msgnum (packet, 1);
106+
packet_set_timestamp(packet, 0x0000051c); /* TODO: calculate time */
107+
packet_set_id (packet, 0x08c42c74); /* TODO: generate an id */
108+
109+
packet_new(&packet, buffer, len);
110+
send_packet_buffer_write(&packet);
111+
112+
boundary = PACKET_BOUNDARY_NONE;
113+
114+
offset += len;
115+
}
116+
117+
packet_clear_header (packet);
118+
packet_set_ctrl (packet);
119+
packet_set_type (packet, PACKET_TYPE_ACK);
120+
packet_set_timestamp(packet, 0x0000051c); /* TODO: calculate time */
121+
packet_set_id (packet, 0x08c42c74); /* TODO: generate an id */
122+
123+
packet_new(&packet, NULL, 0);
124+
send_packet_buffer_write(&packet);
125+
126+
return retval;
127+
}

0 commit comments

Comments
 (0)