aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/socketcreator.h
blob: c4517ef667e2c593459bf9a3683635b5199558e6 (plain)
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
#pragma once
#include "NetAdr2.h"
#include "igameserverdata.h"

//-----------------------------------------------------------------------------
// Purpose: container class to handle network streams
//-----------------------------------------------------------------------------
class CSocketCreator
{
  public:
	CSocketCreator(void);
	~CSocketCreator(void);

	void RunFrame(void);
	void ProcessAccept(void);

	bool ConfigureListenSocket(int iSocket);
	bool ConfigureConnectSocket(SocketHandle_t hSocket);

	bool CreateListenSocket(const CNetAdr2& netAdr2, bool bListenOnAllInterfaces);
	void CloseListenSocket(void);

	int ConnectSocket(const CNetAdr2& netAdr2, bool bSingleSocket);
	void DisconnectSocket(void);

	int OnSocketAccepted(SocketHandle_t hSocket, CNetAdr2 netAdr2);

	void CloseAcceptedSocket(int nIndex);
	void CloseAllAcceptedSockets(void);

	bool IsListening(void) const;
	bool IsSocketBlocking(void) const;

	int GetAcceptedSocketCount(void) const;
	SocketHandle_t GetAcceptedSocketHandle(int nIndex) const;
	const CNetAdr2& GetAcceptedSocketAddress(int nIndex) const;
	CConnectedNetConsoleData* GetAcceptedSocketData(int nIndex) const;

  public:
	struct AcceptedSocket_t
	{
		SocketHandle_t m_hSocket{};
		CNetAdr2 m_Address{};
		CConnectedNetConsoleData* m_pData = nullptr;

		bool operator==(const AcceptedSocket_t& rhs) const { return (m_Address.CompareAdr(rhs.m_Address, false) == 0); }
	};

	std::vector<AcceptedSocket_t> m_hAcceptedSockets{};
	SocketHandle_t m_hListenSocket{}; // Used to accept connections.
	CNetAdr2 m_ListenAddress{};		  // Address used to listen on.

  private:
	enum
	{
		SOCKET_TCP_MAX_ACCEPTS = 2
	};
};