summaryrefslogtreecommitdiff
path: root/src/Sockets/tests/retry.cpp
blob: 36954a6fcf8674805c53abdd048479a00455d320 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <TcpSocket.h>
#include <SocketHandler.h>
#include <ListenSocket.h>


	bool quit = false;

/*
	virtual bool OnConnectRetry();
-	void SetRetryClientConnect(bool x = true);
-	bool RetryClientConnect();
	void SetConnectionRetry(int n);
	int GetConnectionRetry();
	void IncreaseConnectionRetries();
	int GetConnectionRetries();
	void ResetConnectionRetries();
*/
class RetrySocket : public TcpSocket
{
public:
	RetrySocket(ISocketHandler& h) : TcpSocket(h) {
		SetConnectTimeout(2);
		SetConnectionRetry(-1);
	}

	bool OnConnectRetry() {
		printf("Connection attempt#%d\n", GetConnectionRetries());
		if (GetConnectionRetries() == 3)
		{
			ListenSocket<RetrySocket> *l = new ListenSocket<RetrySocket>(Handler());
			if (l -> Bind(12345))
			{
				printf("Bind port 12345 failed\n");
			}
			l -> SetDeleteByHandler();
			Handler().Add(l);
		}
		return true;
	}

	void OnConnect() {
		printf("Connected\n");
		printf("GetRemoteAddress(): %s\n", GetRemoteAddress().c_str());
		printf("Remote address: %s\n", GetRemoteSocketAddress() -> Convert(false).c_str());
		printf("Remote address: %s\n", GetRemoteSocketAddress() -> Convert(true).c_str());
		SetCloseAndDelete();
	}

	void OnDelete() {
		quit = true;
	}
};


int main(int argc, char *argv[])
{
	SocketHandler h;
	RetrySocket sock(h);
	sock.Open("localhost", 12345);
	h.Add(&sock);
	while (!quit)
	{
		h.Select(0, 200000);
	}
}