summaryrefslogtreecommitdiff
path: root/Runtime/Network/PlayerCommunicator/PlayerConnection.h
blob: 07e2159fbab1eb657f933b742570c6846ee2fd6d (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma once

#if ENABLE_PLAYERCONNECTION
#define ENABLE_LISTEN_SOCKET (!UNITY_FLASH)

#include "GeneralConnection.h"
#include "Runtime/Serialize/SwapEndianBytes.h"


#if UNITY_WIN
#include <winsock2.h>
#include <ws2tcpip.h>
#elif UNITY_XENON
#include <xtl.h>
#else 
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <errno.h>
#endif
#include "Runtime/Profiler/TimeHelper.h"
#include "Runtime/Threads/Mutex.h"
#if UNITY_ANDROID
#include <sys/un.h>
#endif

// flags are bits denoting capabilities of the broadcaster
enum PlayerConnectionInitiateMode
	{
	kPlayerConnectionInitiateByListening,
	kPlayerConnectionInitiateByConnecting
	};

class PlayerConnection : public GeneralConnection 
{
public:
	PlayerConnection (const std::string& dataPath = "", unsigned short multicastPort = PLAYER_MULTICAST_PORT, bool enableDebugging=false);

	static void Initialize (const std::string& dataPath, bool enableDebugging=false);
	static void Cleanup ();

	// Singleton accessor for playerconnection
	static PlayerConnection& Get ();
	static PlayerConnection* ms_Instance;

	void Poll ();
	inline bool AllowDebugging () { return (0 != m_AllowDebugging); }
	bool ShouldEnableProfiler() { return m_EnableProfiler != 0; }

	// ugly hack to fix gfx tests
	inline bool IsTestrigMode() { return (m_InitiateMode == kPlayerConnectionInitiateByConnecting); }
	inline bool HasBytesToSend() { return GeneralConnection::HasBytesToSend(); }

private:
	virtual bool IsServer() { return true; }
	bool ReadConfigFile (const std::string& dataPath);
	void CreateListenSocket ();
	void CreateUnixSocket();
	void InitializeMulticastAddress (UInt16 multicastPort);
	void PollListenMode ();
	void PollConnectMode ();
	void CreateAndReportConnection(TSocketHandle socketHandle);
	std::string ConstructWhoamiString ();

	bool ImmediateConnect () const
	{
		return ms_RunningUnitTests
		       || m_WaitingForPlayerConnectionBeforeStartingPlayback;
	}

#if ENABLE_LISTEN_SOCKET
	static void InitializeListenSocket(ServerSocket& socket, const std::string& localIP, int listenPort);
#endif
#if UNITY_ANDROID
	static void InitializeUnixSocket(ServerSocket& socket, const std::string& socketname);
#endif


private:
	bool    m_IsPlayerConnectionEnabled;
	PlayerConnectionInitiateMode		m_InitiateMode;
	bool	m_WaitingForPlayerConnectionBeforeStartingPlayback;

	// player specific
	unsigned short m_ListenPort;
	std::string m_HostName;
	std::string m_WhoAmI;
#if ENABLE_LISTEN_SOCKET
	ServerSocket m_ListenSocket; // player only
#endif
#if UNITY_ANDROID
	ServerSocket m_UnixSocket; // local
#endif
	
	UInt32 m_EditorGuid;
	int m_AllowDebugging;
	int m_EnableProfiler;
	int m_NumIPs;
	std::string m_ConnectToIP;
	char m_ConnectToIPList[10][16];
	
	ABSOLUTE_TIME m_LastMulticast;
};

void InstallPlayerConnectionLogging (bool install);
void TransferFileOverPlayerConnection(const std::string& fname, void* body, unsigned int length, void* header = 0, unsigned int headerLength = 0);
void NotifyFileReadyOverPlayerConnection(const std::string& fname);

#endif // ENABLE_PLAYERCONNECTION