1 |
yutakakn |
2728 |
/* |
2 |
|
|
Copyright (c) 1998-2001, Robert O'Callahan |
3 |
|
|
All rights reserved. |
4 |
|
|
|
5 |
|
|
Redistribution and use in source and binary forms, with or without modification, |
6 |
|
|
are permitted provided that the following conditions are met: |
7 |
|
|
|
8 |
|
|
Redistributions of source code must retain the above copyright notice, this list of |
9 |
|
|
conditions and the following disclaimer. |
10 |
|
|
|
11 |
|
|
Redistributions in binary form must reproduce the above copyright notice, this list |
12 |
|
|
of conditions and the following disclaimer in the documentation and/or other materials |
13 |
|
|
provided with the distribution. |
14 |
|
|
|
15 |
|
|
The name of Robert O'Callahan may not be used to endorse or promote products derived from |
16 |
|
|
this software without specific prior written permission. |
17 |
|
|
|
18 |
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND |
19 |
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 |
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
21 |
|
|
THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 |
|
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
23 |
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
24 |
|
|
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
25 |
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
26 |
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 |
|
|
*/ |
28 |
|
|
|
29 |
|
|
/* |
30 |
|
|
This code is copyright (C) 1998-1999 Robert O'Callahan. |
31 |
|
|
See LICENSE.TXT for the license. |
32 |
|
|
*/ |
33 |
|
|
|
34 |
|
|
#include "ttxssh.h" |
35 |
|
|
|
36 |
|
|
void UTIL_init_sock_write_buf(UTILSockWriteBuf FAR * buf) |
37 |
|
|
{ |
38 |
|
|
buf_create(&buf->bufdata, &buf->buflen); |
39 |
|
|
buf->datastart = 0; |
40 |
|
|
buf->datalen = 0; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
static int send_until_block(PTInstVar pvar, SOCKET s, |
44 |
|
|
const char FAR * data, int len) |
45 |
|
|
{ |
46 |
|
|
int total_sent = 0; |
47 |
|
|
|
48 |
|
|
while (len > 0) { |
49 |
|
|
int sent_amount = (pvar->Psend) (s, data, len, 0); |
50 |
|
|
|
51 |
|
|
if (sent_amount < 0) { |
52 |
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK) { |
53 |
|
|
return total_sent; |
54 |
|
|
} else { |
55 |
|
|
return sent_amount; |
56 |
|
|
} |
57 |
|
|
} else { |
58 |
|
|
total_sent += sent_amount; |
59 |
|
|
data += sent_amount; |
60 |
|
|
len -= sent_amount; |
61 |
|
|
} |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
return total_sent; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
BOOL UTIL_sock_buffered_write(PTInstVar pvar, UTILSockWriteBuf FAR * buf, |
68 |
|
|
UTILBlockingWriteCallback blocking_write, |
69 |
|
|
SOCKET socket, const char FAR * data, |
70 |
|
|
int len) |
71 |
|
|
{ |
72 |
|
|
int curlen; |
73 |
|
|
int desiredlen; |
74 |
|
|
int space_required; |
75 |
|
|
int amount_to_write_from_buffer; |
76 |
|
|
BOOL did_block = FALSE; |
77 |
|
|
int first_copy_start; |
78 |
|
|
int first_copy_amount; |
79 |
|
|
|
80 |
|
|
/* Fast path case: buffer is empty, try nonblocking write */ |
81 |
|
|
if (buf->datalen == 0) { |
82 |
|
|
int sent_amount = send_until_block(pvar, socket, data, len); |
83 |
|
|
|
84 |
|
|
if (sent_amount < 0) { |
85 |
|
|
return FALSE; |
86 |
|
|
} |
87 |
|
|
data += sent_amount; |
88 |
|
|
len -= sent_amount; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
if (len == 0) { |
92 |
|
|
return TRUE; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
/* We blocked or the buffer has data in it. We need to put this data |
96 |
|
|
into the buffer. |
97 |
|
|
First, expand buffer as much as possible and necessary. */ |
98 |
|
|
curlen = buf->buflen; |
99 |
|
|
desiredlen = |
100 |
|
|
min(pvar->session_settings.WriteBufferSize, buf->datalen + len); |
101 |
|
|
|
102 |
|
|
if (curlen < desiredlen) { |
103 |
|
|
int wrap_amount = buf->datastart + buf->datalen - curlen; |
104 |
|
|
int newlen = |
105 |
|
|
min(pvar->session_settings.WriteBufferSize, 2 * desiredlen); |
106 |
|
|
|
107 |
|
|
buf->bufdata = realloc(buf->bufdata, newlen); |
108 |
|
|
buf->buflen = newlen; |
109 |
|
|
|
110 |
|
|
if (wrap_amount > 0) { |
111 |
|
|
int wrap_to_copy = min(wrap_amount, newlen - curlen); |
112 |
|
|
|
113 |
|
|
memmove(buf->bufdata + curlen, buf->bufdata, wrap_to_copy); |
114 |
|
|
memmove(buf->bufdata, buf->bufdata + wrap_to_copy, |
115 |
|
|
wrap_amount - wrap_to_copy); |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
/* 1) Write data from buffer |
120 |
|
|
2) Write data from user |
121 |
|
|
3) Copy remaining user data into buffer |
122 |
|
|
*/ |
123 |
|
|
space_required = max(0, buf->datalen + len - buf->buflen); |
124 |
|
|
amount_to_write_from_buffer = min(buf->datalen, space_required); |
125 |
|
|
|
126 |
|
|
if (amount_to_write_from_buffer > 0) { |
127 |
|
|
int first_part = |
128 |
|
|
min(amount_to_write_from_buffer, buf->buflen - buf->datastart); |
129 |
|
|
|
130 |
|
|
did_block = TRUE; |
131 |
|
|
if (!blocking_write |
132 |
|
|
(pvar, socket, buf->bufdata + buf->datastart, first_part)) { |
133 |
|
|
return FALSE; |
134 |
|
|
} |
135 |
|
|
if (first_part < amount_to_write_from_buffer) { |
136 |
|
|
if (!blocking_write |
137 |
|
|
(pvar, socket, buf->bufdata, |
138 |
|
|
amount_to_write_from_buffer - first_part)) { |
139 |
|
|
return FALSE; |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
buf->datalen -= amount_to_write_from_buffer; |
144 |
|
|
if (buf->datalen == 0) { |
145 |
|
|
buf->datastart = 0; |
146 |
|
|
} else { |
147 |
|
|
buf->datastart = |
148 |
|
|
(buf->datastart + |
149 |
|
|
amount_to_write_from_buffer) % buf->buflen; |
150 |
|
|
} |
151 |
|
|
space_required -= amount_to_write_from_buffer; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
if (space_required > 0) { |
155 |
|
|
did_block = TRUE; |
156 |
|
|
if (!blocking_write(pvar, socket, data, space_required)) { |
157 |
|
|
return FALSE; |
158 |
|
|
} |
159 |
|
|
data += space_required; |
160 |
|
|
len -= space_required; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
first_copy_start = (buf->datastart + buf->datalen) % buf->buflen; |
164 |
|
|
first_copy_amount = min(len, buf->buflen - first_copy_start); |
165 |
|
|
memcpy(buf->bufdata + first_copy_start, data, first_copy_amount); |
166 |
|
|
if (first_copy_amount < len) { |
167 |
|
|
memcpy(buf->bufdata, data + first_copy_amount, |
168 |
|
|
len - first_copy_amount); |
169 |
|
|
} |
170 |
|
|
buf->datalen += len; |
171 |
|
|
|
172 |
|
|
if (did_block) { |
173 |
|
|
return UTIL_sock_write_more(pvar, buf, socket); |
174 |
|
|
} else { |
175 |
|
|
return TRUE; |
176 |
|
|
} |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
BOOL UTIL_sock_write_more(PTInstVar pvar, UTILSockWriteBuf FAR * buf, |
180 |
|
|
SOCKET socket) |
181 |
|
|
{ |
182 |
|
|
int first_amount = min(buf->buflen - buf->datastart, buf->datalen); |
183 |
|
|
int sent = |
184 |
|
|
send_until_block(pvar, socket, buf->bufdata + buf->datastart, |
185 |
|
|
first_amount); |
186 |
|
|
|
187 |
|
|
if (sent < 0) { |
188 |
|
|
return FALSE; |
189 |
|
|
} else { |
190 |
|
|
if (sent == first_amount && first_amount < buf->datalen) { |
191 |
|
|
int sentmore = |
192 |
|
|
send_until_block(pvar, socket, buf->bufdata, |
193 |
|
|
buf->datalen - first_amount); |
194 |
|
|
|
195 |
|
|
if (sentmore < 0) { |
196 |
|
|
return FALSE; |
197 |
|
|
} |
198 |
|
|
sent += sentmore; |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
buf->datalen -= sent; |
202 |
|
|
if (buf->datalen == 0) { |
203 |
|
|
buf->datastart = 0; |
204 |
|
|
} else { |
205 |
|
|
buf->datastart = (buf->datastart + sent) % buf->buflen; |
206 |
|
|
} |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
return TRUE; |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
void UTIL_destroy_sock_write_buf(UTILSockWriteBuf FAR * buf) |
213 |
|
|
{ |
214 |
|
|
memset(buf->bufdata, 0, buf->buflen); |
215 |
|
|
buf_destroy(&buf->bufdata, &buf->buflen); |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
BOOL UTIL_is_sock_deeply_buffered(UTILSockWriteBuf FAR * buf) |
219 |
|
|
{ |
220 |
|
|
return buf->buflen / 2 < buf->datalen; |
221 |
|
|
} |
222 |
yutakakn |
2761 |
|
223 |
maya |
2937 |
#ifdef I18N |
224 |
|
|
void UTIL_get_lang_msg(PCHAR key, PTInstVar pvar) |
225 |
|
|
{ |
226 |
|
|
GetI18nStr("TTSSH", key, pvar->ts->UIMsg, pvar->ts->UILanguageFile); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
int UTIL_get_lang_font(PCHAR key, HWND dlg, PLOGFONT logfont, HFONT *font, PTInstVar pvar) |
230 |
|
|
{ |
231 |
|
|
if (GetI18nLogfont("TTSSH", key, logfont, |
232 |
|
|
GetDeviceCaps(GetDC(dlg),LOGPIXELSY), |
233 |
|
|
pvar->ts->UILanguageFile) == FALSE) { |
234 |
|
|
return FALSE; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
if ((*font = CreateFontIndirect(logfont)) == NULL) { |
238 |
|
|
return FALSE; |
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
return TRUE; |
242 |
|
|
} |
243 |
|
|
#endif |
244 |
|
|
|
245 |
yutakakn |
2761 |
/* |
246 |
|
|
* $Log: not supported by cvs2svn $ |
247 |
maya |
2937 |
* Revision 1.2 2004/12/19 15:39:58 yutakakn |
248 |
|
|
* CVS LogID������ |
249 |
|
|
* |
250 |
yutakakn |
2761 |
*/ |