big svn cleanup
[anytun.git] / src / Sockets / tests / copy.cpp
blob2134ac26a2ac849853cd07aee01d45fb7afa88ca
1 #include <TcpSocket.h>
2 #include <SocketHandler.h>
3 #include <ListenSocket.h>
4 #include <Utility.h>
6 #define BUFSZ 7024
8 #ifdef _WIN32
9 #define MIN(a,b) (a<b?a:b)
11 #endif
13 static int quit = 0;
14 static size_t lim = 120000;
17 class CopySocket : public TcpSocket
19 public:
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);
25 quit++;
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);
29 quit++;
31 ~CopySocket() {
32 quit--;
35 void OnConnect() {
36 Utility::GetTime(&m_start);
37 if (!m_filename.size())
39 Send("\n");
40 char buf[BUFSZ];
41 int n = MIN(m_sz, BUFSZ);
42 while (n > 0 && GetOutputLength() < lim)
44 SendBuf(buf, n);
45 m_sz -= n;
46 n = MIN(m_sz, BUFSZ);
48 if (!n)
50 SetCloseAndDelete();
52 else
54 SendBuf(buf, n);
55 m_sz -= n;
57 return;
59 size_t x = 0;
60 for (size_t i = 0; i < m_filename.size(); i++)
61 if (m_filename[i] == '/')
62 x = i + 1;
63 Send(m_filename.substr(x) + "\n");
64 m_fil = fopen(m_filename.c_str(), "rb");
65 if (m_fil)
67 char buf[BUFSZ];
68 int n = fread(buf, 1, BUFSZ, m_fil);
69 while (n > 0 && GetOutputLength() < lim)
71 SendBuf(buf, n);
72 n = fread(buf, 1, BUFSZ, m_fil);
74 if (!n)
76 SetCloseAndDelete();
77 fclose(m_fil);
78 m_fil = NULL;
80 else
82 SendBuf(buf, n);
87 void OnWriteComplete() {
90 void OnTransferLimit() {
91 if (!m_filename.size())
93 char buf[BUFSZ];
94 int n = MIN(m_sz, BUFSZ);
95 while (n > 0 && GetOutputLength() < lim)
97 SendBuf(buf, n);
98 m_sz -= n;
99 n = MIN(m_sz, BUFSZ);
101 if (!n)
103 SetCloseAndDelete();
105 else
107 SendBuf(buf, n);
108 m_sz -= n;
110 return;
112 if (m_fil)
114 char buf[BUFSZ];
115 int n = fread(buf, 1, BUFSZ, m_fil);
116 while (n > 0 && GetOutputLength() < lim)
118 SendBuf(buf, n);
119 n = fread(buf, 1, BUFSZ, m_fil);
121 if (!n)
123 SetCloseAndDelete();
124 fclose(m_fil);
125 m_fil = NULL;
127 else
129 SendBuf(buf, n);
134 void OnAccept() {
135 Utility::GetTime(&m_start);
136 m_b_server = true;
137 SetLineProtocol();
138 DisableInputBuffer();
141 void OnLine(const std::string& line) {
142 if (line.size())
143 m_out = fopen(line.c_str(), "wb");
144 SetLineProtocol(false);
145 DisableInputBuffer();
148 void OnDelete() {
149 struct timeval stop;
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;
156 stop.tv_sec -= 1;
158 double t = stop.tv_usec;
159 t /= 1000000;
160 t += stop.tv_sec;
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);
167 printf("\n");
168 if (m_out)
169 fclose(m_out);
172 void OnRawData(const char *buf, size_t len) {
173 if (m_out)
174 fwrite(buf, 1, len, m_out);
177 private:
178 std::string m_filename;
179 FILE *m_fil;
180 FILE *m_out;
181 bool m_b_server;
182 struct timeval m_start;
183 size_t m_sz;
187 int main(int argc, char *argv[])
189 std::string host = "127.0.0.1";
190 int port = 12344;
191 std::list<std::string> filenames;
193 for (int i = 1; i < argc; i++)
195 if (!strcmp(argv[i], "-host") && i < argc - 1)
196 host = argv[++i];
197 else
198 if (!strcmp(argv[i], "-port") && i < argc - 1)
199 port = atoi(argv[++i]);
200 else
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");
207 return 0;
209 else
211 filenames.push_back( argv[i] );
217 SocketHandler h;
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);
224 return -1;
226 h.Add(&l);
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());
232 if (sz)
234 CopySocket *sock = new CopySocket(h, sz);
235 sock -> SetDeleteByHandler();
236 sock -> Open(host, port);
237 h.Add(sock);
239 else
241 CopySocket *sock = new CopySocket(h, filename);
242 sock -> SetDeleteByHandler();
243 sock -> Open(host, port);
244 h.Add(sock);
247 if (!filenames.size())
249 fprintf(stderr, "Starting as server only, listening on port %d\n", port);
250 quit++;
252 while (quit > 0)
254 h.Select(5, 0);
256 return 0;
258 catch (const Exception& e)
260 printf("%s\n", e.ToString().c_str());