big svn cleanup
[anytun.git] / src / Sockets / tests / retry.cpp
blob36954a6fcf8674805c53abdd048479a00455d320
1 #include <TcpSocket.h>
2 #include <SocketHandler.h>
3 #include <ListenSocket.h>
6 bool quit = false;
8 /*
9 virtual bool OnConnectRetry();
10 - void SetRetryClientConnect(bool x = true);
11 - bool RetryClientConnect();
12 void SetConnectionRetry(int n);
13 int GetConnectionRetry();
14 void IncreaseConnectionRetries();
15 int GetConnectionRetries();
16 void ResetConnectionRetries();
18 class RetrySocket : public TcpSocket
20 public:
21 RetrySocket(ISocketHandler& h) : TcpSocket(h) {
22 SetConnectTimeout(2);
23 SetConnectionRetry(-1);
26 bool OnConnectRetry() {
27 printf("Connection attempt#%d\n", GetConnectionRetries());
28 if (GetConnectionRetries() == 3)
30 ListenSocket<RetrySocket> *l = new ListenSocket<RetrySocket>(Handler());
31 if (l -> Bind(12345))
33 printf("Bind port 12345 failed\n");
35 l -> SetDeleteByHandler();
36 Handler().Add(l);
38 return true;
41 void OnConnect() {
42 printf("Connected\n");
43 printf("GetRemoteAddress(): %s\n", GetRemoteAddress().c_str());
44 printf("Remote address: %s\n", GetRemoteSocketAddress() -> Convert(false).c_str());
45 printf("Remote address: %s\n", GetRemoteSocketAddress() -> Convert(true).c_str());
46 SetCloseAndDelete();
49 void OnDelete() {
50 quit = true;
55 int main(int argc, char *argv[])
57 SocketHandler h;
58 RetrySocket sock(h);
59 sock.Open("localhost", 12345);
60 h.Add(&sock);
61 while (!quit)
63 h.Select(0, 200000);