added resend logic
[rsync/rsync.git] / token.c
CommitLineData
70d794dc
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include "rsync.h"
21
22extern int do_compression;
23
24
25/* non-compressing recv token */
26static int simple_recv_token(int f,char **data)
27{
28 static int residue = 0;
29 static char *buf = NULL;
30 int n;
31
32 if (!buf) {
33 buf = (char *)malloc(CHUNK_SIZE);
34 if (!buf) out_of_memory("simple_recv_token");
35 }
36
37
38 if (residue == 0) {
39 int i = read_int(f);
40 if (i <= 0) return i;
41 residue = i;
42 }
43
44 *data = buf;
45 n = MIN(CHUNK_SIZE,residue);
46 residue -= n;
47 read_buf(f,buf,n);
48 return n;
49}
50
51
52/* non-compressing send token */
c6e7fcb4
AT
53static void simple_send_token(int f,int token,
54 struct map_struct *buf,int offset,int n)
70d794dc
AT
55{
56 if (n > 0) {
57 int l = 0;
58 while (l < n) {
59 int n1 = MIN(CHUNK_SIZE,n-l);
60 write_int(f,n1);
61 write_buf(f,map_ptr(buf,offset+l,n1),n1);
62 l += n1;
63 }
64 }
65 write_int(f,-(token+1));
66}
67
68
69
70
71/*
72 * transmit a verbatim buffer of length n followed by a token
73 * If token == -1 then we have reached EOF
74 * If n == 0 then don't send a buffer
70d794dc 75 */
9e31c482
AT
76void send_token(int f,int token,struct map_struct *buf,int offset,
77 int n,int toklen)
70d794dc
AT
78{
79 if (!do_compression) {
80 simple_send_token(f,token,buf,offset,n);
81 return;
82 }
83
84 /* compressed transmit here */
85}
86
87
88/*
89 * receive a token or buffer from the other end. If the reurn value is >0 then
90 * it is a data buffer of that length, and *data will point at the data.
91 * if the return value is -i then it represents token i-1
92 * if the return value is 0 then the end has been reached
93 */
94int recv_token(int f,char **data)
95{
96 if (!do_compression) {
97 return simple_recv_token(f,data);
98 }
99
100 /* compressed receive here */
101 return 0;
102}