2 #include <SocketHandler.h>
3 #include <ListenSocket.h>
9 #define MIN(a,b) (a<b?a:b)
14 static size_t lim
= 120000;
17 class CopySocket
: public TcpSocket
20 CopySocket(ISocketHandler
& h
) : TcpSocket(h
), m_fil(NULL
), m_out(NULL
), m_b_server(false), m_sz(0) {
21 SetTransferLimit(60000);
23 CopySocket(ISocketHandler
& h
, size_t sz
) : TcpSocket(h
), m_fil(NULL
), m_out(NULL
), m_b_server(false), m_sz(sz
) {
24 SetTransferLimit(60000);
27 CopySocket(ISocketHandler
& h
, const std::string
& filename
) : TcpSocket(h
), m_filename(filename
), m_fil(NULL
), m_out(NULL
), m_b_server(false), m_sz(0) {
28 SetTransferLimit(60000);
36 Utility::GetTime(&m_start
);
37 if (!m_filename
.size())
41 int n
= MIN(m_sz
, BUFSZ
);
42 while (n
> 0 && GetOutputLength() < lim
)
60 for (size_t i
= 0; i
< m_filename
.size(); i
++)
61 if (m_filename
[i
] == '/')
63 Send(m_filename
.substr(x
) + "\n");
64 m_fil
= fopen(m_filename
.c_str(), "rb");
68 int n
= fread(buf
, 1, BUFSZ
, m_fil
);
69 while (n
> 0 && GetOutputLength() < lim
)
72 n
= fread(buf
, 1, BUFSZ
, m_fil
);
87 void OnWriteComplete() {
90 void OnTransferLimit() {
91 if (!m_filename
.size())
94 int n
= MIN(m_sz
, BUFSZ
);
95 while (n
> 0 && GetOutputLength() < lim
)
115 int n
= fread(buf
, 1, BUFSZ
, m_fil
);
116 while (n
> 0 && GetOutputLength() < lim
)
119 n
= fread(buf
, 1, BUFSZ
, m_fil
);
135 Utility::GetTime(&m_start
);
138 DisableInputBuffer();
141 void OnLine(const std::string
& line
) {
143 m_out
= fopen(line
.c_str(), "wb");
144 SetLineProtocol(false);
145 DisableInputBuffer();
150 Utility::GetTime(&stop
);
151 stop
.tv_sec
-= m_start
.tv_sec
;
152 stop
.tv_usec
-= m_start
.tv_usec
;
153 if (stop
.tv_usec
< 0)
155 stop
.tv_usec
+= 1000000;
158 double t
= stop
.tv_usec
;
161 printf("OnDelete: %s\n", m_b_server
? "SERVER" : "CLIENT");
162 printf(" Time: %ld.%06ld (%f)\n", stop
.tv_sec
, stop
.tv_usec
, t
);
163 double r
= GetBytesReceived();
164 printf(" bytes in: %lld (%f Mbytes/sec)\n", GetBytesReceived(), r
/ t
/ 1000000);
165 double s
= GetBytesSent();
166 printf(" bytes out: %lld (%f Mbytes/sec)\n", GetBytesSent(), s
/ t
/ 1000000);
172 void OnRawData(const char *buf
, size_t len
) {
174 fwrite(buf
, 1, len
, m_out
);
178 std::string m_filename
;
182 struct timeval m_start
;
187 int main(int argc
, char *argv
[])
189 std::string host
= "127.0.0.1";
191 std::list
<std::string
> filenames
;
193 for (int i
= 1; i
< argc
; i
++)
195 if (!strcmp(argv
[i
], "-host") && i
< argc
- 1)
198 if (!strcmp(argv
[i
], "-port") && i
< argc
- 1)
199 port
= atoi(argv
[++i
]);
201 if (!strcmp(argv
[i
], "-h"))
203 fprintf(stderr
, "Usage: %s [-host <host>] [-port <port>] [<file to send>]\n", *argv
);
204 fprintf(stderr
, " Will run as host only if <file to send> isn't specified.\n");
205 fprintf(stderr
, " host default: 127.0.0.1\n");
206 fprintf(stderr
, " port default: 12344\n");
211 filenames
.push_back( argv
[i
] );
218 ListenSocket
<CopySocket
> l(h
);
219 if (filenames
.empty())
221 if (l
.Bind( port
) != 0)
223 fprintf(stderr
, "Bind() port %d failed - exiting\n", port
);
228 for (std::list
<std::string
>::iterator it
= filenames
.begin(); it
!= filenames
.end(); it
++)
230 std::string filename
= *it
;
231 size_t sz
= atol(filename
.c_str());
234 CopySocket
*sock
= new CopySocket(h
, sz
);
235 sock
-> SetDeleteByHandler();
236 sock
-> Open(host
, port
);
241 CopySocket
*sock
= new CopySocket(h
, filename
);
242 sock
-> SetDeleteByHandler();
243 sock
-> Open(host
, port
);
247 if (!filenames
.size())
249 fprintf(stderr
, "Starting as server only, listening on port %d\n", port
);
258 catch (const Exception
& e
)
260 printf("%s\n", e
.ToString().c_str());