Seven is the number.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2153 lines
106 KiB

4 years ago
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="LoadBalancingPeer.cs" company="Exit Games GmbH">
  3. // Loadbalancing Framework for Photon - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Provides operations to use the LoadBalancing and Cloud photon servers.
  7. // No logic is implemented here.
  8. // </summary>
  9. // <author>developer@photonengine.com</author>
  10. // ----------------------------------------------------------------------------
  11. #if UNITY_4_7 || UNITY_5 || UNITY_5_3_OR_NEWER
  12. #define SUPPORTED_UNITY
  13. #endif
  14. namespace Photon.Realtime
  15. {
  16. using System;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. using ExitGames.Client.Photon;
  20. #if SUPPORTED_UNITY
  21. using UnityEngine;
  22. using Debug = UnityEngine.Debug;
  23. #endif
  24. #if SUPPORTED_UNITY || NETFX_CORE
  25. using Hashtable = ExitGames.Client.Photon.Hashtable;
  26. using SupportClass = ExitGames.Client.Photon.SupportClass;
  27. #endif
  28. /// <summary>
  29. /// A LoadBalancingPeer provides the operations and enum definitions needed to use the LoadBalancing server application which is also used in Photon Cloud.
  30. /// </summary>
  31. /// <remarks>
  32. /// This class is internally used.
  33. /// The LoadBalancingPeer does not keep a state, instead this is done by a LoadBalancingClient.
  34. /// </remarks>
  35. public class LoadBalancingPeer : PhotonPeer
  36. {
  37. protected internal static Type PingImplementation = null;
  38. private readonly Dictionary<byte, object> opRaiseEventParameters = new Dictionary<byte, object>(); // used in OpRaiseEvent() (avoids lots of new Dictionary() calls)
  39. /// <summary>
  40. /// Creates a Peer with specified connection protocol. You need to set the Listener before using the peer.
  41. /// </summary>
  42. /// <remarks>Each connection protocol has it's own default networking ports for Photon.</remarks>
  43. /// <param name="protocolType">The preferred option is UDP.</param>
  44. public LoadBalancingPeer(ConnectionProtocol protocolType) : base(protocolType)
  45. {
  46. // this does not require a Listener, so:
  47. // make sure to set this.Listener before using a peer!
  48. this.ConfigUnitySockets();
  49. }
  50. /// <summary>
  51. /// Creates a Peer with specified connection protocol and a Listener for callbacks.
  52. /// </summary>
  53. public LoadBalancingPeer(IPhotonPeerListener listener, ConnectionProtocol protocolType) : this(protocolType)
  54. {
  55. this.Listener = listener;
  56. }
  57. // Sets up the socket implementations to use, depending on platform
  58. [System.Diagnostics.Conditional("SUPPORTED_UNITY")]
  59. private void ConfigUnitySockets()
  60. {
  61. #if !NETFX_CORE && !NO_SOCKET
  62. PingImplementation = typeof(PingMono);
  63. #endif
  64. #if UNITY_WEBGL
  65. PingImplementation = typeof(PingHttp);
  66. #endif
  67. #if !UNITY_EDITOR && NETFX_CORE
  68. PingImplementation = typeof(PingWindowsStore);
  69. #endif
  70. Type websocketType = null;
  71. #if UNITY_XBOXONE && !UNITY_EDITOR
  72. websocketType = Type.GetType("ExitGames.Client.Photon.SocketNativeSource, PhotonRealtime", false);
  73. if (websocketType == null)
  74. {
  75. websocketType = Type.GetType("ExitGames.Client.Photon.SocketNativeSource, Assembly-CSharp-firstpass", false);
  76. }
  77. if (websocketType == null)
  78. {
  79. websocketType = Type.GetType("ExitGames.Client.Photon.SocketNativeSource, Assembly-CSharp", false);
  80. }
  81. if (websocketType == null)
  82. {
  83. Debug.LogError("UNITY_XBOXONE is defined but peer could not find SocketNativeSource. Check your project files to make sure the native WSS implementation is available. Won't connect.");
  84. }
  85. #else
  86. // to support WebGL export in Unity, we find and assign the SocketWebTcp class (if it's in the project).
  87. // alternatively class SocketWebTcp might be in the Photon3Unity3D.dll
  88. websocketType = Type.GetType("ExitGames.Client.Photon.SocketWebTcp, PhotonWebSocket", false);
  89. if (websocketType == null)
  90. {
  91. websocketType = Type.GetType("ExitGames.Client.Photon.SocketWebTcp, Assembly-CSharp-firstpass", false);
  92. }
  93. if (websocketType == null)
  94. {
  95. websocketType = Type.GetType("ExitGames.Client.Photon.SocketWebTcp, Assembly-CSharp", false);
  96. }
  97. #endif
  98. if (websocketType != null)
  99. {
  100. this.SocketImplementationConfig[ConnectionProtocol.WebSocket] = websocketType;
  101. this.SocketImplementationConfig[ConnectionProtocol.WebSocketSecure] = websocketType;
  102. }
  103. #if NET_4_6 && (UNITY_EDITOR || !ENABLE_IL2CPP)
  104. this.SocketImplementationConfig[ConnectionProtocol.Udp] = typeof(SocketUdpAsync);
  105. this.SocketImplementationConfig[ConnectionProtocol.Tcp] = typeof(SocketTcpAsync);
  106. #endif
  107. }
  108. public virtual bool OpGetRegions(string appId)
  109. {
  110. Dictionary<byte, object> parameters = new Dictionary<byte, object>(1);
  111. parameters[(byte)ParameterCode.ApplicationId] = appId;
  112. return this.SendOperation(OperationCode.GetRegions, parameters, new SendOptions() { Reliability = true, Encrypt = true });
  113. }
  114. /// <summary>
  115. /// Joins the lobby on the Master Server, where you get a list of RoomInfos of currently open rooms.
  116. /// This is an async request which triggers a OnOperationResponse() call.
  117. /// </summary>
  118. /// <param name="lobby">The lobby join to.</param>
  119. /// <returns>If the operation could be sent (has to be connected).</returns>
  120. public virtual bool OpJoinLobby(TypedLobby lobby = null)
  121. {
  122. if (this.DebugOut >= DebugLevel.INFO)
  123. {
  124. this.Listener.DebugReturn(DebugLevel.INFO, "OpJoinLobby()");
  125. }
  126. Dictionary<byte, object> parameters = null;
  127. if (lobby != null && !lobby.IsDefault)
  128. {
  129. parameters = new Dictionary<byte, object>();
  130. parameters[(byte)ParameterCode.LobbyName] = lobby.Name;
  131. parameters[(byte)ParameterCode.LobbyType] = (byte)lobby.Type;
  132. }
  133. return this.SendOperation(OperationCode.JoinLobby, parameters, SendOptions.SendReliable);
  134. }
  135. /// <summary>
  136. /// Leaves the lobby on the Master Server.
  137. /// This is an async request which triggers a OnOperationResponse() call.
  138. /// </summary>
  139. /// <returns>If the operation could be sent (requires connection).</returns>
  140. public virtual bool OpLeaveLobby()
  141. {
  142. if (this.DebugOut >= DebugLevel.INFO)
  143. {
  144. this.Listener.DebugReturn(DebugLevel.INFO, "OpLeaveLobby()");
  145. }
  146. return this.SendOperation(OperationCode.LeaveLobby, null, SendOptions.SendReliable);
  147. }
  148. /// <summary>Used in the RoomOptionFlags parameter, this bitmask toggles options in the room.</summary>
  149. enum RoomOptionBit : int
  150. {
  151. CheckUserOnJoin = 0x01, // toggles a check of the UserId when joining (enabling returning to a game)
  152. DeleteCacheOnLeave = 0x02, // deletes cache on leave
  153. SuppressRoomEvents = 0x04, // suppresses all room events
  154. PublishUserId = 0x08, // signals that we should publish userId
  155. DeleteNullProps = 0x10, // signals that we should remove property if its value was set to null. see RoomOption to Delete Null Properties
  156. BroadcastPropsChangeToAll = 0x20, // signals that we should send PropertyChanged event to all room players including initiator
  157. }
  158. /// <summary>Used by OpJoinRoom and by OpCreateRoom alike.</summary>
  159. private void RoomOptionsToOpParameters(Dictionary<byte, object> op, RoomOptions roomOptions, bool usePropertiesKey = false)
  160. {
  161. if (roomOptions == null)
  162. {
  163. roomOptions = new RoomOptions();
  164. }
  165. Hashtable gameProperties = new Hashtable();
  166. gameProperties[GamePropertyKey.IsOpen] = roomOptions.IsOpen;
  167. gameProperties[GamePropertyKey.IsVisible] = roomOptions.IsVisible;
  168. gameProperties[GamePropertyKey.PropsListedInLobby] = (roomOptions.CustomRoomPropertiesForLobby == null) ? new string[0] : roomOptions.CustomRoomPropertiesForLobby;
  169. gameProperties.MergeStringKeys(roomOptions.CustomRoomProperties);
  170. if (roomOptions.MaxPlayers > 0)
  171. {
  172. gameProperties[GamePropertyKey.MaxPlayers] = roomOptions.MaxPlayers;
  173. }
  174. if (!usePropertiesKey)
  175. {
  176. op[ParameterCode.GameProperties] = gameProperties; // typically, the key for game props is 248
  177. }
  178. else
  179. {
  180. op[ParameterCode.Properties] = gameProperties; // when an op uses 248 as filter, the "create room" props can be set as 251
  181. }
  182. int flags = 0; // a new way to send the room options as bitwise-flags
  183. if (roomOptions.CleanupCacheOnLeave)
  184. {
  185. op[ParameterCode.CleanupCacheOnLeave] = true; // this defines the server's room settings and logic
  186. flags = flags | (int)RoomOptionBit.DeleteCacheOnLeave; // this defines the server's room settings and logic (for servers that support flags)
  187. }
  188. else
  189. {
  190. op[ParameterCode.CleanupCacheOnLeave] = false; // this defines the server's room settings and logic
  191. gameProperties[GamePropertyKey.CleanupCacheOnLeave] = false; // this is only informational for the clients which join
  192. }
  193. #if SERVERSDK
  194. op[ParameterCode.CheckUserOnJoin] = roomOptions.CheckUserOnJoin;
  195. if (roomOptions.CheckUserOnJoin)
  196. {
  197. flags = flags | (int) RoomOptionBit.CheckUserOnJoin;
  198. }
  199. #else
  200. // in PUN v1.88 and PUN 2, CheckUserOnJoin is set by default:
  201. flags = flags | (int) RoomOptionBit.CheckUserOnJoin;
  202. op[ParameterCode.CheckUserOnJoin] = true;
  203. #endif
  204. if (roomOptions.PlayerTtl > 0 || roomOptions.PlayerTtl == -1)
  205. {
  206. op[ParameterCode.PlayerTTL] = roomOptions.PlayerTtl; // TURNBASED
  207. }
  208. if (roomOptions.EmptyRoomTtl > 0)
  209. {
  210. op[ParameterCode.EmptyRoomTTL] = roomOptions.EmptyRoomTtl; //TURNBASED
  211. }
  212. if (roomOptions.SuppressRoomEvents)
  213. {
  214. flags = flags | (int)RoomOptionBit.SuppressRoomEvents;
  215. op[ParameterCode.SuppressRoomEvents] = true;
  216. }
  217. if (roomOptions.Plugins != null)
  218. {
  219. op[ParameterCode.Plugins] = roomOptions.Plugins;
  220. }
  221. if (roomOptions.PublishUserId)
  222. {
  223. flags = flags | (int)RoomOptionBit.PublishUserId;
  224. op[ParameterCode.PublishUserId] = true;
  225. }
  226. if (roomOptions.DeleteNullProperties)
  227. {
  228. flags = flags | (int)RoomOptionBit.DeleteNullProps; // this is only settable as flag
  229. }
  230. if (roomOptions.BroadcastPropsChangeToAll)
  231. {
  232. flags = flags | (int)RoomOptionBit.BroadcastPropsChangeToAll; // this is only settable as flag
  233. }
  234. op[ParameterCode.RoomOptionFlags] = flags;
  235. }
  236. /// <summary>
  237. /// Creates a room (on either Master or Game Server).
  238. /// The OperationResponse depends on the server the peer is connected to:
  239. /// Master will return a Game Server to connect to.
  240. /// Game Server will return the joined Room's data.
  241. /// This is an async request which triggers a OnOperationResponse() call.
  242. /// </summary>
  243. /// <remarks>
  244. /// If the room is already existing, the OperationResponse will have a returnCode of ErrorCode.GameAlreadyExists.
  245. /// </remarks>
  246. public virtual bool OpCreateRoom(EnterRoomParams opParams)
  247. {
  248. if (this.DebugOut >= DebugLevel.INFO)
  249. {
  250. this.Listener.DebugReturn(DebugLevel.INFO, "OpCreateRoom()");
  251. }
  252. Dictionary<byte, object> op = new Dictionary<byte, object>();
  253. if (!string.IsNullOrEmpty(opParams.RoomName))
  254. {
  255. op[ParameterCode.RoomName] = opParams.RoomName;
  256. }
  257. if (opParams.Lobby != null && !opParams.Lobby.IsDefault)
  258. {
  259. op[ParameterCode.LobbyName] = opParams.Lobby.Name;
  260. op[ParameterCode.LobbyType] = (byte)opParams.Lobby.Type;
  261. }
  262. if (opParams.ExpectedUsers != null && opParams.ExpectedUsers.Length > 0)
  263. {
  264. op[ParameterCode.Add] = opParams.ExpectedUsers;
  265. }
  266. if (opParams.OnGameServer)
  267. {
  268. if (opParams.PlayerProperties != null && opParams.PlayerProperties.Count > 0)
  269. {
  270. op[ParameterCode.PlayerProperties] = opParams.PlayerProperties;
  271. op[ParameterCode.Broadcast] = true; // TODO: check if this also makes sense when creating a room?! // broadcast actor properties
  272. }
  273. this.RoomOptionsToOpParameters(op, opParams.RoomOptions);
  274. }
  275. //this.Listener.DebugReturn(DebugLevel.INFO, "OpCreateRoom: " + SupportClass.DictionaryToString(op));
  276. return this.SendOperation(OperationCode.CreateGame, op, SendOptions.SendReliable);
  277. }
  278. /// <summary>
  279. /// Joins a room by name or creates new room if room with given name not exists.
  280. /// The OperationResponse depends on the server the peer is connected to:
  281. /// Master will return a Game Server to connect to.
  282. /// Game Server will return the joined Room's data.
  283. /// This is an async request which triggers a OnOperationResponse() call.
  284. /// </summary>
  285. /// <remarks>
  286. /// If the room is not existing (anymore), the OperationResponse will have a returnCode of ErrorCode.GameDoesNotExist.
  287. /// Other possible ErrorCodes are: GameClosed, GameFull.
  288. /// </remarks>
  289. /// <returns>If the operation could be sent (requires connection).</returns>
  290. public virtual bool OpJoinRoom(EnterRoomParams opParams)
  291. {
  292. if (this.DebugOut >= DebugLevel.INFO)
  293. {
  294. this.Listener.DebugReturn(DebugLevel.INFO, "OpJoinRoom()");
  295. }
  296. Dictionary<byte, object> op = new Dictionary<byte, object>();
  297. if (!string.IsNullOrEmpty(opParams.RoomName))
  298. {
  299. op[ParameterCode.RoomName] = opParams.RoomName;
  300. }
  301. if (opParams.CreateIfNotExists)
  302. {
  303. op[ParameterCode.JoinMode] = (byte)JoinMode.CreateIfNotExists;
  304. if (opParams.Lobby != null && !opParams.Lobby.IsDefault)
  305. {
  306. op[ParameterCode.LobbyName] = opParams.Lobby.Name;
  307. op[ParameterCode.LobbyType] = (byte)opParams.Lobby.Type;
  308. }
  309. }
  310. if (opParams.RejoinOnly)
  311. {
  312. op[ParameterCode.JoinMode] = (byte)JoinMode.RejoinOnly; // changed from JoinMode.JoinOrRejoin
  313. }
  314. if (opParams.ExpectedUsers != null && opParams.ExpectedUsers.Length > 0)
  315. {
  316. op[ParameterCode.Add] = opParams.ExpectedUsers;
  317. }
  318. if (opParams.OnGameServer)
  319. {
  320. if (opParams.PlayerProperties != null && opParams.PlayerProperties.Count > 0)
  321. {
  322. op[ParameterCode.PlayerProperties] = opParams.PlayerProperties;
  323. op[ParameterCode.Broadcast] = true; // broadcast actor properties
  324. }
  325. if (opParams.CreateIfNotExists)
  326. {
  327. this.RoomOptionsToOpParameters(op, opParams.RoomOptions);
  328. }
  329. }
  330. //this.Listener.DebugReturn(DebugLevel.INFO, "OpJoinRoom: " + SupportClass.DictionaryToString(op));
  331. return this.SendOperation(OperationCode.JoinGame, op, SendOptions.SendReliable);
  332. }
  333. /// <summary>
  334. /// Operation to join a random, available room. Overloads take additional player properties.
  335. /// This is an async request which triggers a OnOperationResponse() call.
  336. /// If all rooms are closed or full, the OperationResponse will have a returnCode of ErrorCode.NoRandomMatchFound.
  337. /// If successful, the OperationResponse contains a gameserver address and the name of some room.
  338. /// </summary>
  339. /// <returns>If the operation could be sent currently (requires connection).</returns>
  340. public virtual bool OpJoinRandomRoom(OpJoinRandomRoomParams opJoinRandomRoomParams)
  341. {
  342. if (this.DebugOut >= DebugLevel.INFO)
  343. {
  344. this.Listener.DebugReturn(DebugLevel.INFO, "OpJoinRandomRoom()");
  345. }
  346. Hashtable expectedRoomProperties = new Hashtable();
  347. expectedRoomProperties.MergeStringKeys(opJoinRandomRoomParams.ExpectedCustomRoomProperties);
  348. if (opJoinRandomRoomParams.ExpectedMaxPlayers > 0)
  349. {
  350. expectedRoomProperties[GamePropertyKey.MaxPlayers] = opJoinRandomRoomParams.ExpectedMaxPlayers;
  351. }
  352. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  353. if (expectedRoomProperties.Count > 0)
  354. {
  355. opParameters[ParameterCode.GameProperties] = expectedRoomProperties;
  356. }
  357. if (opJoinRandomRoomParams.MatchingType != MatchmakingMode.FillRoom)
  358. {
  359. opParameters[ParameterCode.MatchMakingType] = (byte)opJoinRandomRoomParams.MatchingType;
  360. }
  361. if (opJoinRandomRoomParams.TypedLobby != null && !opJoinRandomRoomParams.TypedLobby.IsDefault)
  362. {
  363. opParameters[ParameterCode.LobbyName] = opJoinRandomRoomParams.TypedLobby.Name;
  364. opParameters[ParameterCode.LobbyType] = (byte)opJoinRandomRoomParams.TypedLobby.Type;
  365. }
  366. if (!string.IsNullOrEmpty(opJoinRandomRoomParams.SqlLobbyFilter))
  367. {
  368. opParameters[ParameterCode.Data] = opJoinRandomRoomParams.SqlLobbyFilter;
  369. }
  370. if (opJoinRandomRoomParams.ExpectedUsers != null && opJoinRandomRoomParams.ExpectedUsers.Length > 0)
  371. {
  372. opParameters[ParameterCode.Add] = opJoinRandomRoomParams.ExpectedUsers;
  373. }
  374. //this.Listener.DebugReturn(DebugLevel.INFO, "OpJoinRandomRoom: " + SupportClass.DictionaryToString(opParameters));
  375. return this.SendOperation(OperationCode.JoinRandomGame, opParameters, SendOptions.SendReliable);
  376. }
  377. /// <summary>
  378. /// Only used on the Master Server. It will assign a game server and room to join-or-create.
  379. /// On the Game Server, the OpJoin is used with option "create if not exists".
  380. /// </summary>
  381. public virtual bool OpJoinRandomOrCreateRoom(OpJoinRandomRoomParams opJoinRandomRoomParams, EnterRoomParams createRoomParams)
  382. {
  383. if (this.DebugOut >= DebugLevel.INFO)
  384. {
  385. this.Listener.DebugReturn(DebugLevel.INFO, "OpJoinRandomOrCreateRoom()");
  386. }
  387. // join random room parameters:
  388. Hashtable expectedRoomProperties = new Hashtable();
  389. expectedRoomProperties.MergeStringKeys(opJoinRandomRoomParams.ExpectedCustomRoomProperties);
  390. if (opJoinRandomRoomParams.ExpectedMaxPlayers > 0)
  391. {
  392. expectedRoomProperties[GamePropertyKey.MaxPlayers] = opJoinRandomRoomParams.ExpectedMaxPlayers;
  393. }
  394. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  395. if (expectedRoomProperties.Count > 0)
  396. {
  397. opParameters[ParameterCode.GameProperties] = expectedRoomProperties; // used as filter. below, RoomOptionsToOpParameters has usePropertiesKey = true
  398. }
  399. if (opJoinRandomRoomParams.MatchingType != MatchmakingMode.FillRoom)
  400. {
  401. opParameters[ParameterCode.MatchMakingType] = (byte)opJoinRandomRoomParams.MatchingType;
  402. }
  403. if (opJoinRandomRoomParams.TypedLobby != null && !opJoinRandomRoomParams.TypedLobby.IsDefault)
  404. {
  405. opParameters[ParameterCode.LobbyName] = opJoinRandomRoomParams.TypedLobby.Name;
  406. opParameters[ParameterCode.LobbyType] = (byte)opJoinRandomRoomParams.TypedLobby.Type;
  407. }
  408. if (!string.IsNullOrEmpty(opJoinRandomRoomParams.SqlLobbyFilter))
  409. {
  410. opParameters[ParameterCode.Data] = opJoinRandomRoomParams.SqlLobbyFilter;
  411. }
  412. if (opJoinRandomRoomParams.ExpectedUsers != null && opJoinRandomRoomParams.ExpectedUsers.Length > 0)
  413. {
  414. opParameters[ParameterCode.Add] = opJoinRandomRoomParams.ExpectedUsers;
  415. }
  416. // parameters for creating a room if needed ("or create" part of the operation)
  417. // partial copy of OpCreateRoom
  418. opParameters[ParameterCode.JoinMode] = (byte)JoinMode.CreateIfNotExists;
  419. if (createRoomParams != null)
  420. {
  421. if (!string.IsNullOrEmpty(createRoomParams.RoomName))
  422. {
  423. opParameters[ParameterCode.RoomName] = createRoomParams.RoomName;
  424. }
  425. this.RoomOptionsToOpParameters(opParameters, createRoomParams.RoomOptions, true);
  426. }
  427. //this.Listener.DebugReturn(DebugLevel.INFO, "OpJoinRandomOrCreateRoom: " + SupportClass.DictionaryToString(opParameters, false));
  428. return this.SendOperation(OperationCode.JoinRandomGame, opParameters, SendOptions.SendReliable);
  429. }
  430. /// <summary>
  431. /// Leaves a room with option to come back later or "for good".
  432. /// </summary>
  433. /// <param name="becomeInactive">Async games can be re-joined (loaded) later on. Set to false, if you want to abandon a game entirely.</param>
  434. /// <param name="sendAuthCookie">WebFlag: Securely transmit the encrypted object AuthCookie to the web service in PathLeave webhook when available</param>
  435. /// <returns>If the opteration can be send currently.</returns>
  436. public virtual bool OpLeaveRoom(bool becomeInactive, bool sendAuthCookie = false)
  437. {
  438. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  439. if (becomeInactive)
  440. {
  441. opParameters[ParameterCode.IsInactive] = true;
  442. }
  443. if (sendAuthCookie)
  444. {
  445. opParameters[ParameterCode.EventForward] = WebFlags.SendAuthCookieConst;
  446. }
  447. return this.SendOperation(OperationCode.Leave, opParameters, SendOptions.SendReliable);
  448. }
  449. /// <summary>Gets a list of games matching a SQL-like where clause.</summary>
  450. /// <remarks>
  451. /// Operation is only available in lobbies of type SqlLobby.
  452. /// This is an async request which triggers a OnOperationResponse() call.
  453. /// Returned game list is stored in RoomInfoList.
  454. /// </remarks>
  455. /// <see cref="https://doc.photonengine.com/en-us/realtime/current/reference/matchmaking-and-lobby#sql_lobby_type"/>
  456. /// <param name="lobby">The lobby to query. Has to be of type SqlLobby.</param>
  457. /// <param name="queryData">The sql query statement.</param>
  458. /// <returns>If the operation could be sent (has to be connected).</returns>
  459. public virtual bool OpGetGameList(TypedLobby lobby, string queryData)
  460. {
  461. if (this.DebugOut >= DebugLevel.INFO)
  462. {
  463. this.Listener.DebugReturn(DebugLevel.INFO, "OpGetGameList()");
  464. }
  465. if (lobby == null)
  466. {
  467. if (this.DebugOut >= DebugLevel.INFO)
  468. {
  469. this.Listener.DebugReturn(DebugLevel.INFO, "OpGetGameList not sent. Lobby cannot be null.");
  470. }
  471. return false;
  472. }
  473. if (lobby.Type != LobbyType.SqlLobby)
  474. {
  475. if (this.DebugOut >= DebugLevel.INFO)
  476. {
  477. this.Listener.DebugReturn(DebugLevel.INFO, "OpGetGameList not sent. LobbyType must be SqlLobby.");
  478. }
  479. return false;
  480. }
  481. if (lobby.IsDefault)
  482. {
  483. if (this.DebugOut >= DebugLevel.INFO)
  484. {
  485. this.Listener.DebugReturn(DebugLevel.INFO, "OpGetGameList not sent. LobbyName must be not null and not empty.");
  486. }
  487. return false;
  488. }
  489. if (string.IsNullOrEmpty(queryData))
  490. {
  491. if (this.DebugOut >= DebugLevel.INFO)
  492. {
  493. this.Listener.DebugReturn(DebugLevel.INFO, "OpGetGameList not sent. queryData must be not null and not empty.");
  494. }
  495. return false;
  496. }
  497. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  498. opParameters[(byte)ParameterCode.LobbyName] = lobby.Name;
  499. opParameters[(byte)ParameterCode.LobbyType] = (byte)lobby.Type;
  500. opParameters[(byte)ParameterCode.Data] = queryData;
  501. return this.SendOperation(OperationCode.GetGameList, opParameters, SendOptions.SendReliable);
  502. }
  503. /// <summary>
  504. /// Request the rooms and online status for a list of friends (each client must set a unique username via OpAuthenticate).
  505. /// </summary>
  506. /// <remarks>
  507. /// Used on Master Server to find the rooms played by a selected list of users.
  508. /// Users identify themselves by using OpAuthenticate with a unique user ID.
  509. /// The list of user IDs must be fetched from some other source (not provided by Photon).
  510. ///
  511. /// The server response includes 2 arrays of info (each index matching a friend from the request):<br/>
  512. /// ParameterCode.FindFriendsResponseOnlineList = bool[] of online states<br/>
  513. /// ParameterCode.FindFriendsResponseRoomIdList = string[] of room names (empty string if not in a room)<br/>
  514. /// <br/>
  515. /// The options may be used to define which state a room must match to be returned.
  516. /// </remarks>
  517. /// <param name="friendsToFind">Array of friend's names (make sure they are unique).</param>
  518. /// <param name="options">Options that affect the result of the FindFriends operation.</param>
  519. /// <returns>If the operation could be sent (requires connection).</returns>
  520. public virtual bool OpFindFriends(string[] friendsToFind, FindFriendsOptions options = null)
  521. {
  522. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  523. if (friendsToFind != null && friendsToFind.Length > 0)
  524. {
  525. opParameters[ParameterCode.FindFriendsRequestList] = friendsToFind;
  526. }
  527. if (options != null)
  528. {
  529. opParameters[ParameterCode.FindFriendsOptions] = options.ToIntFlags();
  530. }
  531. return this.SendOperation(OperationCode.FindFriends, opParameters, SendOptions.SendReliable);
  532. }
  533. public bool OpSetCustomPropertiesOfActor(int actorNr, Hashtable actorProperties)
  534. {
  535. return this.OpSetPropertiesOfActor(actorNr, actorProperties.StripToStringKeys(), null);
  536. }
  537. /// <summary>
  538. /// Sets properties of a player / actor.
  539. /// Internally this uses OpSetProperties, which can be used to either set room or player properties.
  540. /// </summary>
  541. /// <param name="actorNr">The payer ID (a.k.a. actorNumber) of the player to attach these properties to.</param>
  542. /// <param name="actorProperties">The properties to add or update.</param>
  543. /// <param name="expectedProperties">If set, these must be in the current properties-set (on the server) to set actorProperties: CAS.</param>
  544. /// <param name="webflags">Set these to forward the properties to a WebHook as defined for this app (in Dashboard).</param>
  545. /// <returns>If the operation could be sent (requires connection).</returns>
  546. protected internal bool OpSetPropertiesOfActor(int actorNr, Hashtable actorProperties, Hashtable expectedProperties = null, WebFlags webflags = null)
  547. {
  548. if (this.DebugOut >= DebugLevel.INFO)
  549. {
  550. this.Listener.DebugReturn(DebugLevel.INFO, "OpSetPropertiesOfActor()");
  551. }
  552. if (actorNr <= 0 || actorProperties == null)
  553. {
  554. if (this.DebugOut >= DebugLevel.INFO)
  555. {
  556. this.Listener.DebugReturn(DebugLevel.INFO, "OpSetPropertiesOfActor not sent. ActorNr must be > 0 and actorProperties != null.");
  557. }
  558. return false;
  559. }
  560. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  561. opParameters.Add(ParameterCode.Properties, actorProperties);
  562. opParameters.Add(ParameterCode.ActorNr, actorNr);
  563. opParameters.Add(ParameterCode.Broadcast, true);
  564. if (expectedProperties != null && expectedProperties.Count != 0)
  565. {
  566. opParameters.Add(ParameterCode.ExpectedValues, expectedProperties);
  567. }
  568. if (webflags != null && webflags.HttpForward)
  569. {
  570. opParameters[ParameterCode.EventForward] = webflags.WebhookFlags;
  571. }
  572. return this.SendOperation(OperationCode.SetProperties, opParameters, SendOptions.SendReliable);
  573. }
  574. protected void OpSetPropertyOfRoom(byte propCode, object value)
  575. {
  576. Hashtable properties = new Hashtable();
  577. properties[propCode] = value;
  578. this.OpSetPropertiesOfRoom(properties);
  579. }
  580. public bool OpSetCustomPropertiesOfRoom(Hashtable gameProperties)
  581. {
  582. return this.OpSetPropertiesOfRoom(gameProperties.StripToStringKeys());
  583. }
  584. /// <summary>
  585. /// Sets properties of a room.
  586. /// Internally this uses OpSetProperties, which can be used to either set room or player properties.
  587. /// </summary>
  588. /// <param name="gameProperties">The properties to add or update.</param>
  589. /// <param name="expectedProperties">The properties expected when update occurs. (CAS : "Check And Swap")</param>
  590. /// <param name="webflags">WebFlag to indicate if request should be forwarded as "PathProperties" webhook or not.</param>
  591. /// <returns>If the operation could be sent (has to be connected).</returns>
  592. protected internal bool OpSetPropertiesOfRoom(Hashtable gameProperties, Hashtable expectedProperties = null, WebFlags webflags = null)
  593. {
  594. if (this.DebugOut >= DebugLevel.INFO)
  595. {
  596. this.Listener.DebugReturn(DebugLevel.INFO, "OpSetPropertiesOfRoom()");
  597. }
  598. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  599. opParameters.Add(ParameterCode.Properties, gameProperties);
  600. opParameters.Add(ParameterCode.Broadcast, true);
  601. if (expectedProperties != null && expectedProperties.Count != 0)
  602. {
  603. opParameters.Add(ParameterCode.ExpectedValues, expectedProperties);
  604. }
  605. if (webflags!=null && webflags.HttpForward)
  606. {
  607. opParameters[ParameterCode.EventForward] = webflags.WebhookFlags;
  608. }
  609. return this.SendOperation(OperationCode.SetProperties, opParameters, SendOptions.SendReliable);
  610. }
  611. /// <summary>
  612. /// Sends this app's appId and appVersion to identify this application server side.
  613. /// This is an async request which triggers a OnOperationResponse() call.
  614. /// </summary>
  615. /// <remarks>
  616. /// This operation makes use of encryption, if that is established before.
  617. /// See: EstablishEncryption(). Check encryption with IsEncryptionAvailable.
  618. /// This operation is allowed only once per connection (multiple calls will have ErrorCode != Ok).
  619. /// </remarks>
  620. /// <param name="appId">Your application's name or ID to authenticate. This is assigned by Photon Cloud (webpage).</param>
  621. /// <param name="appVersion">The client's version (clients with differing client appVersions are separated and players don't meet).</param>
  622. /// <param name="authValues">Contains all values relevant for authentication. Even without account system (external Custom Auth), the clients are allowed to identify themselves.</param>
  623. /// <param name="regionCode">Optional region code, if the client should connect to a specific Photon Cloud Region.</param>
  624. /// <param name="getLobbyStatistics">Set to true on Master Server to receive "Lobby Statistics" events.</param>
  625. /// <returns>If the operation could be sent (has to be connected).</returns>
  626. public virtual bool OpAuthenticate(string appId, string appVersion, AuthenticationValues authValues, string regionCode, bool getLobbyStatistics)
  627. {
  628. if (this.DebugOut >= DebugLevel.INFO)
  629. {
  630. this.Listener.DebugReturn(DebugLevel.INFO, "OpAuthenticate()");
  631. }
  632. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  633. if (getLobbyStatistics)
  634. {
  635. // must be sent in operation, even if a Token is available
  636. opParameters[ParameterCode.LobbyStats] = true;
  637. }
  638. // shortcut, if we have a Token
  639. if (authValues != null && authValues.Token != null)
  640. {
  641. opParameters[ParameterCode.Secret] = authValues.Token;
  642. return this.SendOperation(OperationCode.Authenticate, opParameters, SendOptions.SendReliable); // we don't have to encrypt, when we have a token (which is encrypted)
  643. }
  644. // without a token, we send a complete op auth
  645. opParameters[ParameterCode.AppVersion] = appVersion;
  646. opParameters[ParameterCode.ApplicationId] = appId;
  647. if (!string.IsNullOrEmpty(regionCode))
  648. {
  649. opParameters[ParameterCode.Region] = regionCode;
  650. }
  651. if (authValues != null)
  652. {
  653. if (!string.IsNullOrEmpty(authValues.UserId))
  654. {
  655. opParameters[ParameterCode.UserId] = authValues.UserId;
  656. }
  657. if (authValues.AuthType != CustomAuthenticationType.None)
  658. {
  659. opParameters[ParameterCode.ClientAuthenticationType] = (byte)authValues.AuthType;
  660. // if we had a token, the code above would use it. here, we send parameters:
  661. if (!string.IsNullOrEmpty(authValues.AuthGetParameters))
  662. {
  663. opParameters[ParameterCode.ClientAuthenticationParams] = authValues.AuthGetParameters;
  664. }
  665. if (authValues.AuthPostData != null)
  666. {
  667. opParameters[ParameterCode.ClientAuthenticationData] = authValues.AuthPostData;
  668. }
  669. }
  670. }
  671. return this.SendOperation(OperationCode.Authenticate, opParameters, new SendOptions() { Reliability = true, Encrypt = this.IsEncryptionAvailable });
  672. }
  673. /// <summary>
  674. /// Sends this app's appId and appVersion to identify this application server side.
  675. /// This is an async request which triggers a OnOperationResponse() call.
  676. /// </summary>
  677. /// <remarks>
  678. /// This operation makes use of encryption, if that is established before.
  679. /// See: EstablishEncryption(). Check encryption with IsEncryptionAvailable.
  680. /// This operation is allowed only once per connection (multiple calls will have ErrorCode != Ok).
  681. /// </remarks>
  682. /// <param name="appId">Your application's name or ID to authenticate. This is assigned by Photon Cloud (webpage).</param>
  683. /// <param name="appVersion">The client's version (clients with differing client appVersions are separated and players don't meet).</param>
  684. /// <param name="authValues">Optional authentication values. The client can set no values or a UserId or some parameters for Custom Authentication by a server.</param>
  685. /// <param name="regionCode">Optional region code, if the client should connect to a specific Photon Cloud Region.</param>
  686. /// <param name="encryptionMode"></param>
  687. /// <param name="expectedProtocol"></param>
  688. /// <returns>If the operation could be sent (has to be connected).</returns>
  689. public virtual bool OpAuthenticateOnce(string appId, string appVersion, AuthenticationValues authValues, string regionCode, EncryptionMode encryptionMode, ConnectionProtocol expectedProtocol)
  690. {
  691. if (this.DebugOut >= DebugLevel.INFO)
  692. {
  693. this.Listener.DebugReturn(DebugLevel.INFO, "OpAuthenticateOnce()");
  694. }
  695. var opParameters = new Dictionary<byte, object>();
  696. // shortcut, if we have a Token
  697. if (authValues != null && authValues.Token != null)
  698. {
  699. opParameters[ParameterCode.Secret] = authValues.Token;
  700. return this.SendOperation(OperationCode.AuthenticateOnce, opParameters, SendOptions.SendReliable); // we don't have to encrypt, when we have a token (which is encrypted)
  701. }
  702. if (encryptionMode == EncryptionMode.DatagramEncryption && expectedProtocol != ConnectionProtocol.Udp)
  703. {
  704. // TODO disconnect?!
  705. throw new NotSupportedException("Expected protocol set to UDP, due to encryption mode DatagramEncryption."); // TODO use some other form of callback?!
  706. }
  707. opParameters[ParameterCode.ExpectedProtocol] = (byte)expectedProtocol;
  708. opParameters[ParameterCode.EncryptionMode] = (byte)encryptionMode;
  709. opParameters[ParameterCode.AppVersion] = appVersion;
  710. opParameters[ParameterCode.ApplicationId] = appId;
  711. if (!string.IsNullOrEmpty(regionCode))
  712. {
  713. opParameters[ParameterCode.Region] = regionCode;
  714. }
  715. if (authValues != null)
  716. {
  717. if (!string.IsNullOrEmpty(authValues.UserId))
  718. {
  719. opParameters[ParameterCode.UserId] = authValues.UserId;
  720. }
  721. if (authValues.AuthType != CustomAuthenticationType.None)
  722. {
  723. opParameters[ParameterCode.ClientAuthenticationType] = (byte)authValues.AuthType;
  724. if (!string.IsNullOrEmpty(authValues.Token))
  725. {
  726. opParameters[ParameterCode.Secret] = authValues.Token;
  727. }
  728. else
  729. {
  730. if (!string.IsNullOrEmpty(authValues.AuthGetParameters))
  731. {
  732. opParameters[ParameterCode.ClientAuthenticationParams] = authValues.AuthGetParameters;
  733. }
  734. if (authValues.AuthPostData != null)
  735. {
  736. opParameters[ParameterCode.ClientAuthenticationData] = authValues.AuthPostData;
  737. }
  738. }
  739. }
  740. }
  741. return this.SendOperation(OperationCode.AuthenticateOnce, opParameters, new SendOptions() { Reliability = true, Encrypt = this.IsEncryptionAvailable });
  742. }
  743. /// <summary>
  744. /// Operation to handle this client's interest groups (for events in room).
  745. /// </summary>
  746. /// <remarks>
  747. /// Note the difference between passing null and byte[0]:
  748. /// null won't add/remove any groups.
  749. /// byte[0] will add/remove all (existing) groups.
  750. /// First, removing groups is executed. This way, you could leave all groups and join only the ones provided.
  751. ///
  752. /// Changes become active not immediately but when the server executes this operation (approximately RTT/2).
  753. /// </remarks>
  754. /// <param name="groupsToRemove">Groups to remove from interest. Null will not remove any. A byte[0] will remove all.</param>
  755. /// <param name="groupsToAdd">Groups to add to interest. Null will not add any. A byte[0] will add all current.</param>
  756. /// <returns>If operation could be enqueued for sending. Sent when calling: Service or SendOutgoingCommands.</returns>
  757. public virtual bool OpChangeGroups(byte[] groupsToRemove, byte[] groupsToAdd)
  758. {
  759. if (this.DebugOut >= DebugLevel.ALL)
  760. {
  761. this.Listener.DebugReturn(DebugLevel.ALL, "OpChangeGroups()");
  762. }
  763. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  764. if (groupsToRemove != null)
  765. {
  766. opParameters[(byte)ParameterCode.Remove] = groupsToRemove;
  767. }
  768. if (groupsToAdd != null)
  769. {
  770. opParameters[(byte)ParameterCode.Add] = groupsToAdd;
  771. }
  772. return this.SendOperation(OperationCode.ChangeGroups, opParameters, SendOptions.SendReliable);
  773. }
  774. /// <summary>
  775. /// Send an event with custom code/type and any content to the other players in the same room.
  776. /// </summary>
  777. /// <remarks>This override explicitly uses another parameter order to not mix it up with the implementation for Hashtable only.</remarks>
  778. /// <param name="eventCode">Identifies this type of event (and the content). Your game's event codes can start with 0.</param>
  779. /// <param name="customEventContent">Any serializable datatype (including Hashtable like the other OpRaiseEvent overloads).</param>
  780. /// <param name="raiseEventOptions">Contains (slightly) less often used options. If you pass null, the default options will be used.</param>
  781. /// <param name="sendOptions">Send options for reliable, encryption etc</param>
  782. /// <returns>If operation could be enqueued for sending. Sent when calling: Service or SendOutgoingCommands.</returns>
  783. public virtual bool OpRaiseEvent(byte eventCode, object customEventContent, RaiseEventOptions raiseEventOptions, SendOptions sendOptions)
  784. {
  785. this.opRaiseEventParameters.Clear(); // re-used private variable to avoid many new Dictionary() calls (garbage collection)
  786. if (raiseEventOptions != null)
  787. {
  788. if (raiseEventOptions.CachingOption != EventCaching.DoNotCache)
  789. {
  790. this.opRaiseEventParameters[(byte)ParameterCode.Cache] = (byte)raiseEventOptions.CachingOption;
  791. }
  792. switch (raiseEventOptions.CachingOption)
  793. {
  794. case EventCaching.SliceSetIndex:
  795. case EventCaching.SlicePurgeIndex:
  796. case EventCaching.SlicePurgeUpToIndex:
  797. //this.opParameters[(byte) ParameterCode.CacheSliceIndex] =
  798. // (byte) raiseEventOptions.CacheSliceIndex;
  799. return this.SendOperation(OperationCode.RaiseEvent, this.opRaiseEventParameters, sendOptions);
  800. case EventCaching.SliceIncreaseIndex:
  801. case EventCaching.RemoveFromRoomCacheForActorsLeft:
  802. return this.SendOperation(OperationCode.RaiseEvent, this.opRaiseEventParameters, sendOptions);
  803. case EventCaching.RemoveFromRoomCache:
  804. if (raiseEventOptions.TargetActors != null)
  805. {
  806. this.opRaiseEventParameters[(byte)ParameterCode.ActorList] = raiseEventOptions.TargetActors;
  807. }
  808. break;
  809. default:
  810. if (raiseEventOptions.TargetActors != null)
  811. {
  812. this.opRaiseEventParameters[(byte)ParameterCode.ActorList] = raiseEventOptions.TargetActors;
  813. }
  814. else if (raiseEventOptions.InterestGroup != 0)
  815. {
  816. this.opRaiseEventParameters[(byte)ParameterCode.Group] = raiseEventOptions.InterestGroup;
  817. }
  818. else if (raiseEventOptions.Receivers != ReceiverGroup.Others)
  819. {
  820. this.opRaiseEventParameters[(byte)ParameterCode.ReceiverGroup] = (byte)raiseEventOptions.Receivers;
  821. }
  822. if (raiseEventOptions.Flags.HttpForward)
  823. {
  824. this.opRaiseEventParameters[(byte)ParameterCode.EventForward] = raiseEventOptions.Flags.WebhookFlags;
  825. }
  826. break;
  827. }
  828. }
  829. this.opRaiseEventParameters[(byte)ParameterCode.Code] = (byte)eventCode;
  830. if (customEventContent != null)
  831. {
  832. this.opRaiseEventParameters[(byte)ParameterCode.Data] = customEventContent;
  833. }
  834. return this.SendOperation(OperationCode.RaiseEvent, this.opRaiseEventParameters, sendOptions);
  835. }
  836. /// <summary>
  837. /// Internally used operation to set some "per server" settings. This is for the Master Server.
  838. /// </summary>
  839. /// <param name="receiveLobbyStats">Set to true, to get Lobby Statistics (lists of existing lobbies).</param>
  840. /// <returns>False if the operation could not be sent.</returns>
  841. public virtual bool OpSettings(bool receiveLobbyStats)
  842. {
  843. if (this.DebugOut >= DebugLevel.ALL)
  844. {
  845. this.Listener.DebugReturn(DebugLevel.ALL, "OpSettings()");
  846. }
  847. Dictionary<byte, object> opParameters = new Dictionary<byte, object>();
  848. // implementation for Master Server:
  849. if (receiveLobbyStats)
  850. {
  851. opParameters[(byte)0] = receiveLobbyStats;
  852. }
  853. if (opParameters.Count == 0)
  854. {
  855. // no need to send op in case we set the default values
  856. return true;
  857. }
  858. return this.SendOperation(OperationCode.ServerSettings, opParameters, SendOptions.SendReliable);
  859. }
  860. }
  861. /// <summary>
  862. /// Options for OpFindFriends can be combined to filter which rooms of friends are returned.
  863. /// </summary>
  864. public class FindFriendsOptions
  865. {
  866. /// <summary>Include a friend's room only if it is created and confirmed by the game server.</summary>
  867. public bool CreatedOnGs = false; //flag: 0x01
  868. /// <summary>Include a friend's room only if it is visible (using Room.IsVisible).</summary>
  869. public bool Visible = false; //flag: 0x02
  870. /// <summary>Include a friend's room only if it is open (using Room.IsOpen).</summary>
  871. public bool Open = false; //flag: 0x04
  872. /// <summary>Turns the bool options into an integer, which is sent as option flags for Op FindFriends.</summary>
  873. /// <returns>The options applied to bits of an integer.</returns>
  874. internal int ToIntFlags()
  875. {
  876. int optionFlags = 0;
  877. if (this.CreatedOnGs)
  878. {
  879. optionFlags = optionFlags | 0x1;
  880. }
  881. if (this.Visible)
  882. {
  883. optionFlags = optionFlags | 0x2;
  884. }
  885. if (this.Open)
  886. {
  887. optionFlags = optionFlags | 0x4;
  888. }
  889. return optionFlags;
  890. }
  891. }
  892. public class OpJoinRandomRoomParams
  893. {
  894. public Hashtable ExpectedCustomRoomProperties;
  895. public byte ExpectedMaxPlayers;
  896. public MatchmakingMode MatchingType;
  897. public TypedLobby TypedLobby;
  898. public string SqlLobbyFilter;
  899. public string[] ExpectedUsers;
  900. }
  901. public class EnterRoomParams
  902. {
  903. public string RoomName;
  904. public RoomOptions RoomOptions;
  905. public TypedLobby Lobby;
  906. public Hashtable PlayerProperties;
  907. protected internal bool OnGameServer = true; // defaults to true! better send more parameter than too few (GS needs all)
  908. public bool CreateIfNotExists;
  909. public bool RejoinOnly;
  910. public string[] ExpectedUsers;
  911. }
  912. /// <summary>
  913. /// ErrorCode defines the default codes associated with Photon client/server communication.
  914. /// </summary>
  915. public class ErrorCode
  916. {
  917. /// <summary>(0) is always "OK", anything else an error or specific situation.</summary>
  918. public const int Ok = 0;
  919. // server - Photon low(er) level: <= 0
  920. /// <summary>
  921. /// (-3) Operation can't be executed yet (e.g. OpJoin can't be called before being authenticated, RaiseEvent cant be used before getting into a room).
  922. /// </summary>
  923. /// <remarks>
  924. /// Before you call any operations on the Cloud servers, the automated client workflow must complete its authorization.
  925. /// Wait until State is: JoinedLobby or ConnectedToMasterServer
  926. /// </remarks>
  927. public const int OperationNotAllowedInCurrentState = -3;
  928. /// <summary>(-2) The operation you called is not implemented on the server (application) you connect to. Make sure you run the fitting applications.</summary>
  929. [Obsolete("Use InvalidOperation.")]
  930. public const int InvalidOperationCode = -2;
  931. /// <summary>(-2) The operation you called could not be executed on the server.</summary>
  932. /// <remarks>
  933. /// Make sure you are connected to the server you expect.
  934. ///
  935. /// This code is used in several cases:
  936. /// The arguments/parameters of the operation might be out of range, missing entirely or conflicting.
  937. /// The operation you called is not implemented on the server (application). Server-side plugins affect the available operations.
  938. /// </remarks>
  939. public const int InvalidOperation = -2;
  940. /// <summary>(-1) Something went wrong in the server. Try to reproduce and contact Exit Games.</summary>
  941. public const int InternalServerError = -1;
  942. // server - client: 0x7FFF and down
  943. // logic-level error codes start with short.max
  944. /// <summary>(32767) Authentication failed. Possible cause: AppId is unknown to Photon (in cloud service).</summary>
  945. public const int InvalidAuthentication = 0x7FFF;
  946. /// <summary>(32766) GameId (name) already in use (can't create another). Change name.</summary>
  947. public const int GameIdAlreadyExists = 0x7FFF - 1;
  948. /// <summary>(32765) Game is full. This rarely happens when some player joined the room before your join completed.</summary>
  949. public const int GameFull = 0x7FFF - 2;
  950. /// <summary>(32764) Game is closed and can't be joined. Join another game.</summary>
  951. public const int GameClosed = 0x7FFF - 3;
  952. [Obsolete("No longer used, cause random matchmaking is no longer a process.")]
  953. public const int AlreadyMatched = 0x7FFF - 4;
  954. /// <summary>(32762) All servers are busy. This is a temporary issue and the game logic should try again after a brief wait time.</summary>
  955. /// <remarks>
  956. /// This error may happen for all operations that create rooms. The operation response will contain this error code.
  957. ///
  958. /// This error is very unlikely to happen as we monitor load on all servers and add them on demand.
  959. /// However, it's good to be prepared for a shortage of machines or surge in CCUs.
  960. /// </remarks>
  961. public const int ServerFull = 0x7FFF - 5;
  962. /// <summary>(32761) Not in use currently.</summary>
  963. public const int UserBlocked = 0x7FFF - 6;
  964. /// <summary>(32760) Random matchmaking only succeeds if a room exists thats neither closed nor full. Repeat in a few seconds or create a new room.</summary>
  965. public const int NoRandomMatchFound = 0x7FFF - 7;
  966. /// <summary>(32758) Join can fail if the room (name) is not existing (anymore). This can happen when players leave while you join.</summary>
  967. public const int GameDoesNotExist = 0x7FFF - 9;
  968. /// <summary>(32757) Authorization on the Photon Cloud failed becaus the concurrent users (CCU) limit of the app's subscription is reached.</summary>
  969. /// <remarks>
  970. /// Unless you have a plan with "CCU Burst", clients might fail the authentication step during connect.
  971. /// Affected client are unable to call operations. Please note that players who end a game and return
  972. /// to the master server will disconnect and re-connect, which means that they just played and are rejected
  973. /// in the next minute / re-connect.
  974. /// This is a temporary measure. Once the CCU is below the limit, players will be able to connect an play again.
  975. ///
  976. /// OpAuthorize is part of connection workflow but only on the Photon Cloud, this error can happen.
  977. /// Self-hosted Photon servers with a CCU limited license won't let a client connect at all.
  978. /// </remarks>
  979. public const int MaxCcuReached = 0x7FFF - 10;
  980. /// <summary>(32756) Authorization on the Photon Cloud failed because the app's subscription does not allow to use a particular region's server.</summary>
  981. /// <remarks>
  982. /// Some subscription plans for the Photon Cloud are region-bound. Servers of other regions can't be used then.
  983. /// Check your master server address and compare it with your Photon Cloud Dashboard's info.
  984. /// https://dashboard.photonengine.com
  985. ///
  986. /// OpAuthorize is part of connection workflow but only on the Photon Cloud, this error can happen.
  987. /// Self-hosted Photon servers with a CCU limited license won't let a client connect at all.
  988. /// </remarks>
  989. public const int InvalidRegion = 0x7FFF - 11;
  990. /// <summary>
  991. /// (32755) Custom Authentication of the user failed due to setup reasons (see Cloud Dashboard) or the provided user data (like username or token). Check error message for details.
  992. /// </summary>
  993. public const int CustomAuthenticationFailed = 0x7FFF - 12;
  994. /// <summary>(32753) The Authentication ticket expired. Usually, this is refreshed behind the scenes. Connect (and authorize) again.</summary>
  995. public const int AuthenticationTicketExpired = 0x7FF1;
  996. /// <summary>
  997. /// (32752) A server-side plugin (or webhook) failed to execute and reported an error. Check the OperationResponse.DebugMessage.
  998. /// </summary>
  999. public const int PluginReportedError = 0x7FFF - 15;
  1000. /// <summary>
  1001. /// (32751) CreateGame/JoinGame/Join operation fails if expected plugin does not correspond to loaded one.
  1002. /// </summary>
  1003. public const int PluginMismatch = 0x7FFF - 16;
  1004. /// <summary>
  1005. /// (32750) for join requests. Indicates the current peer already called join and is joined to the room.
  1006. /// </summary>
  1007. public const int JoinFailedPeerAlreadyJoined = 32750; // 0x7FFF - 17,
  1008. /// <summary>
  1009. /// (32749) for join requests. Indicates the list of InactiveActors already contains an actor with the requested ActorNr or UserId.
  1010. /// </summary>
  1011. public const int JoinFailedFoundInactiveJoiner = 32749; // 0x7FFF - 18,
  1012. /// <summary>
  1013. /// (32748) for join requests. Indicates the list of Actors (active and inactive) did not contain an actor with the requested ActorNr or UserId.
  1014. /// </summary>
  1015. public const int JoinFailedWithRejoinerNotFound = 32748; // 0x7FFF - 19,
  1016. /// <summary>
  1017. /// (32747) for join requests. Note: for future use - Indicates the requested UserId was found in the ExcludedList.
  1018. /// </summary>
  1019. public const int JoinFailedFoundExcludedUserId = 32747; // 0x7FFF - 20,
  1020. /// <summary>
  1021. /// (32746) for join requests. Indicates the list of ActiveActors already contains an actor with the requested ActorNr or UserId.
  1022. /// </summary>
  1023. public const int JoinFailedFoundActiveJoiner = 32746; // 0x7FFF - 21,
  1024. /// <summary>
  1025. /// (32745) for SetProerties and Raisevent (if flag HttpForward is true) requests. Indicates the maximum allowd http requests per minute was reached.
  1026. /// </summary>
  1027. public const int HttpLimitReached = 32745; // 0x7FFF - 22,
  1028. /// <summary>
  1029. /// (32744) for WebRpc requests. Indicates the the call to the external service failed.
  1030. /// </summary>
  1031. public const int ExternalHttpCallFailed = 32744; // 0x7FFF - 23,
  1032. /// <summary>
  1033. /// (32742) Server error during matchmaking with slot reservation. E.g. the reserved slots can not exceed MaxPlayers.
  1034. /// </summary>
  1035. public const int SlotError = 32742; // 0x7FFF - 25,
  1036. /// <summary>
  1037. /// (32741) Server will react with this error if invalid encryption parameters provided by token
  1038. /// </summary>
  1039. public const int InvalidEncryptionParameters = 32741; // 0x7FFF - 24,
  1040. }
  1041. /// <summary>
  1042. /// Class for constants. These (byte) values define "well known" properties for an Actor / Player.
  1043. /// </summary>
  1044. /// <remarks>
  1045. /// These constants are used internally.
  1046. /// "Custom properties" have to use a string-type as key. They can be assigned at will.
  1047. /// </remarks>
  1048. public class ActorProperties
  1049. {
  1050. /// <summary>(255) Name of a player/actor.</summary>
  1051. public const byte PlayerName = 255; // was: 1
  1052. /// <summary>(254) Tells you if the player is currently in this game (getting events live).</summary>
  1053. /// <remarks>A server-set value for async games, where players can leave the game and return later.</remarks>
  1054. public const byte IsInactive = 254;
  1055. /// <summary>(253) UserId of the player. Sent when room gets created with RoomOptions.PublishUserId = true.</summary>
  1056. public const byte UserId = 253;
  1057. }
  1058. /// <summary>
  1059. /// Class for constants. These (byte) values are for "well known" room/game properties used in Photon LoadBalancing.
  1060. /// </summary>
  1061. /// <remarks>
  1062. /// These constants are used internally.
  1063. /// "Custom properties" have to use a string-type as key. They can be assigned at will.
  1064. /// </remarks>
  1065. public class GamePropertyKey
  1066. {
  1067. /// <summary>(255) Max number of players that "fit" into this room. 0 is for "unlimited".</summary>
  1068. public const byte MaxPlayers = 255;
  1069. /// <summary>(254) Makes this room listed or not in the lobby on master.</summary>
  1070. public const byte IsVisible = 254;
  1071. /// <summary>(253) Allows more players to join a room (or not).</summary>
  1072. public const byte IsOpen = 253;
  1073. /// <summary>(252) Current count of players in the room. Used only in the lobby on master.</summary>
  1074. public const byte PlayerCount = 252;
  1075. /// <summary>(251) True if the room is to be removed from room listing (used in update to room list in lobby on master)</summary>
  1076. public const byte Removed = 251;
  1077. /// <summary>(250) A list of the room properties to pass to the RoomInfo list in a lobby. This is used in CreateRoom, which defines this list once per room.</summary>
  1078. public const byte PropsListedInLobby = 250;
  1079. /// <summary>(249) Equivalent of Operation Join parameter CleanupCacheOnLeave.</summary>
  1080. public const byte CleanupCacheOnLeave = 249;
  1081. /// <summary>(248) Code for MasterClientId, which is synced by server. When sent as op-parameter this is (byte)203. As room property this is (byte)248.</summary>
  1082. /// <remarks>Tightly related to ParameterCode.MasterClientId.</remarks>
  1083. public const byte MasterClientId = (byte)248;
  1084. /// <summary>(247) Code for ExpectedUsers in a room. Matchmaking keeps a slot open for the players with these userIDs.</summary>
  1085. public const byte ExpectedUsers = (byte)247;
  1086. /// <summary>(246) Player Time To Live. How long any player can be inactive (due to disconnect or leave) before the user gets removed from the playerlist (freeing a slot).</summary>
  1087. public const byte PlayerTtl = (byte)246;
  1088. /// <summary>(245) Room Time To Live. How long a room stays available (and in server-memory), after the last player becomes inactive. After this time, the room gets persisted or destroyed.</summary>
  1089. public const byte EmptyRoomTtl = (byte)245;
  1090. }
  1091. /// <summary>
  1092. /// Class for constants. These values are for events defined by Photon LoadBalancing.
  1093. /// </summary>
  1094. /// <remarks>They start at 255 and go DOWN. Your own in-game events can start at 0. These constants are used internally.</remarks>
  1095. public class EventCode
  1096. {
  1097. /// <summary>(230) Initial list of RoomInfos (in lobby on Master)</summary>
  1098. public const byte GameList = 230;
  1099. /// <summary>(229) Update of RoomInfos to be merged into "initial" list (in lobby on Master)</summary>
  1100. public const byte GameListUpdate = 229;
  1101. /// <summary>(228) Currently not used. State of queueing in case of server-full</summary>
  1102. public const byte QueueState = 228;
  1103. /// <summary>(227) Currently not used. Event for matchmaking</summary>
  1104. public const byte Match = 227;
  1105. /// <summary>(226) Event with stats about this application (players, rooms, etc)</summary>
  1106. public const byte AppStats = 226;
  1107. /// <summary>(224) This event provides a list of lobbies with their player and game counts.</summary>
  1108. public const byte LobbyStats = 224;
  1109. /// <summary>(210) Internally used in case of hosting by Azure</summary>
  1110. [Obsolete("TCP routing was removed after becoming obsolete.")]
  1111. public const byte AzureNodeInfo = 210;
  1112. /// <summary>(255) Event Join: someone joined the game. The new actorNumber is provided as well as the properties of that actor (if set in OpJoin).</summary>
  1113. public const byte Join = (byte)255;
  1114. /// <summary>(254) Event Leave: The player who left the game can be identified by the actorNumber.</summary>
  1115. public const byte Leave = (byte)254;
  1116. /// <summary>(253) When you call OpSetProperties with the broadcast option "on", this event is fired. It contains the properties being set.</summary>
  1117. public const byte PropertiesChanged = (byte)253;
  1118. /// <summary>(253) When you call OpSetProperties with the broadcast option "on", this event is fired. It contains the properties being set.</summary>
  1119. [Obsolete("Use PropertiesChanged now.")]
  1120. public const byte SetProperties = (byte)253;
  1121. /// <summary>(252) When player left game unexpected and the room has a playerTtl != 0, this event is fired to let everyone know about the timeout.</summary>
  1122. /// Obsolete. Replaced by Leave. public const byte Disconnect = LiteEventCode.Disconnect;
  1123. /// <summary>(251) Sent by Photon Cloud when a plugin-call or webhook-call failed. Usually, the execution on the server continues, despite the issue. Contains: ParameterCode.Info.</summary>
  1124. /// <seealso cref="https://doc.photonengine.com/en-us/realtime/current/reference/webhooks#options"/>
  1125. public const byte ErrorInfo = 251;
  1126. /// <summary>(250) Sent by Photon whent he event cache slice was changed. Done by OpRaiseEvent.</summary>
  1127. public const byte CacheSliceChanged = 250;
  1128. /// <summary>(223) Sent by Photon to update a token before it times out.</summary>
  1129. public const byte AuthEvent = 223;
  1130. }
  1131. /// <summary>Class for constants. Codes for parameters of Operations and Events.</summary>
  1132. /// <remarks>These constants are used internally.</remarks>
  1133. public class ParameterCode
  1134. {
  1135. /// <summary>(237) A bool parameter for creating games. If set to true, no room events are sent to the clients on join and leave. Default: false (and not sent).</summary>
  1136. public const byte SuppressRoomEvents = 237;
  1137. /// <summary>(236) Time To Live (TTL) for a room when the last player leaves. Keeps room in memory for case a player re-joins soon. In milliseconds.</summary>
  1138. public const byte EmptyRoomTTL = 236;
  1139. /// <summary>(235) Time To Live (TTL) for an 'actor' in a room. If a client disconnects, this actor is inactive first and removed after this timeout. In milliseconds.</summary>
  1140. public const byte PlayerTTL = 235;
  1141. /// <summary>(234) Optional parameter of OpRaiseEvent and OpSetCustomProperties to forward the event/operation to a web-service.</summary>
  1142. public const byte EventForward = 234;
  1143. /// <summary>(233) Optional parameter of OpLeave in async games. If false, the player does abandons the game (forever). By default players become inactive and can re-join.</summary>
  1144. [Obsolete("Use: IsInactive")]
  1145. public const byte IsComingBack = (byte)233;
  1146. /// <summary>(233) Used in EvLeave to describe if a user is inactive (and might come back) or not. In rooms with PlayerTTL, becoming inactive is the default case.</summary>
  1147. public const byte IsInactive = (byte)233;
  1148. /// <summary>(232) Used when creating rooms to define if any userid can join the room only once.</summary>
  1149. public const byte CheckUserOnJoin = (byte)232;
  1150. /// <summary>(231) Code for "Check And Swap" (CAS) when changing properties.</summary>
  1151. public const byte ExpectedValues = (byte)231;
  1152. /// <summary>(230) Address of a (game) server to use.</summary>
  1153. public const byte Address = 230;
  1154. /// <summary>(229) Count of players in this application in a rooms (used in stats event)</summary>
  1155. public const byte PeerCount = 229;
  1156. /// <summary>(228) Count of games in this application (used in stats event)</summary>
  1157. public const byte GameCount = 228;
  1158. /// <summary>(227) Count of players on the master server (in this app, looking for rooms)</summary>
  1159. public const byte MasterPeerCount = 227;
  1160. /// <summary>(225) User's ID</summary>
  1161. public const byte UserId = 225;
  1162. /// <summary>(224) Your application's ID: a name on your own Photon or a GUID on the Photon Cloud</summary>
  1163. public const byte ApplicationId = 224;
  1164. /// <summary>(223) Not used currently (as "Position"). If you get queued before connect, this is your position</summary>
  1165. public const byte Position = 223;
  1166. /// <summary>(223) Modifies the matchmaking algorithm used for OpJoinRandom. Allowed parameter values are defined in enum MatchmakingMode.</summary>
  1167. public const byte MatchMakingType = 223;
  1168. /// <summary>(222) List of RoomInfos about open / listed rooms</summary>
  1169. public const byte GameList = 222;
  1170. /// <summary>(221) Internally used to establish encryption</summary>
  1171. public const byte Secret = 221;
  1172. /// <summary>(220) Version of your application</summary>
  1173. public const byte AppVersion = 220;
  1174. /// <summary>(210) Internally used in case of hosting by Azure</summary>
  1175. [Obsolete("TCP routing was removed after becoming obsolete.")]
  1176. public const byte AzureNodeInfo = 210; // only used within events, so use: EventCode.AzureNodeInfo
  1177. /// <summary>(209) Internally used in case of hosting by Azure</summary>
  1178. [Obsolete("TCP routing was removed after becoming obsolete.")]
  1179. public const byte AzureLocalNodeId = 209;
  1180. /// <summary>(208) Internally used in case of hosting by Azure</summary>
  1181. [Obsolete("TCP routing was removed after becoming obsolete.")]
  1182. public const byte AzureMasterNodeId = 208;
  1183. /// <summary>(255) Code for the gameId/roomName (a unique name per room). Used in OpJoin and similar.</summary>
  1184. public const byte RoomName = (byte)255;
  1185. /// <summary>(250) Code for broadcast parameter of OpSetProperties method.</summary>
  1186. public const byte Broadcast = (byte)250;
  1187. /// <summary>(252) Code for list of players in a room. Currently not used.</summary>
  1188. public const byte ActorList = (byte)252;
  1189. /// <summary>(254) Code of the Actor of an operation. Used for property get and set.</summary>
  1190. public const byte ActorNr = (byte)254;
  1191. /// <summary>(249) Code for property set (Hashtable).</summary>
  1192. public const byte PlayerProperties = (byte)249;
  1193. /// <summary>(245) Code of data/custom content of an event. Used in OpRaiseEvent.</summary>
  1194. public const byte CustomEventContent = (byte)245;
  1195. /// <summary>(245) Code of data of an event. Used in OpRaiseEvent.</summary>
  1196. public const byte Data = (byte)245;
  1197. /// <summary>(244) Code used when sending some code-related parameter, like OpRaiseEvent's event-code.</summary>
  1198. /// <remarks>This is not the same as the Operation's code, which is no longer sent as part of the parameter Dictionary in Photon 3.</remarks>
  1199. public const byte Code = (byte)244;
  1200. /// <summary>(248) Code for property set (Hashtable).</summary>
  1201. public const byte GameProperties = (byte)248;
  1202. /// <summary>
  1203. /// (251) Code for property-set (Hashtable). This key is used when sending only one set of properties.
  1204. /// If either ActorProperties or GameProperties are used (or both), check those keys.
  1205. /// </summary>
  1206. public const byte Properties = (byte)251;
  1207. /// <summary>(253) Code of the target Actor of an operation. Used for property set. Is 0 for game</summary>
  1208. public const byte TargetActorNr = (byte)253;
  1209. /// <summary>(246) Code to select the receivers of events (used in Lite, Operation RaiseEvent).</summary>
  1210. public const byte ReceiverGroup = (byte)246;
  1211. /// <summary>(247) Code for caching events while raising them.</summary>
  1212. public const byte Cache = (byte)247;
  1213. /// <summary>(241) Bool parameter of CreateGame Operation. If true, server cleans up roomcache of leaving players (their cached events get removed).</summary>
  1214. public const byte CleanupCacheOnLeave = (byte)241;
  1215. /// <summary>(240) Code for "group" operation-parameter (as used in Op RaiseEvent).</summary>
  1216. public const byte Group = 240;
  1217. /// <summary>(239) The "Remove" operation-parameter can be used to remove something from a list. E.g. remove groups from player's interest groups.</summary>
  1218. public const byte Remove = 239;
  1219. /// <summary>(239) Used in Op Join to define if UserIds of the players are broadcast in the room. Useful for FindFriends and reserving slots for expected users.</summary>
  1220. public const byte PublishUserId = 239;
  1221. /// <summary>(238) The "Add" operation-parameter can be used to add something to some list or set. E.g. add groups to player's interest groups.</summary>
  1222. public const byte Add = 238;
  1223. /// <summary>(218) Content for EventCode.ErrorInfo and internal debug operations.</summary>
  1224. public const byte Info = 218;
  1225. /// <summary>(217) This key's (byte) value defines the target custom authentication type/service the client connects with. Used in OpAuthenticate</summary>
  1226. public const byte ClientAuthenticationType = 217;
  1227. /// <summary>(216) This key's (string) value provides parameters sent to the custom authentication type/service the client connects with. Used in OpAuthenticate</summary>
  1228. public const byte ClientAuthenticationParams = 216;
  1229. /// <summary>(215) Makes the server create a room if it doesn't exist. OpJoin uses this to always enter a room, unless it exists and is full/closed.</summary>
  1230. // public const byte CreateIfNotExists = 215;
  1231. /// <summary>(215) The JoinMode enum defines which variant of joining a room will be executed: Join only if available, create if not exists or re-join.</summary>
  1232. /// <remarks>Replaces CreateIfNotExists which was only a bool-value.</remarks>
  1233. public const byte JoinMode = 215;
  1234. /// <summary>(214) This key's (string or byte[]) value provides parameters sent to the custom authentication service setup in Photon Dashboard. Used in OpAuthenticate</summary>
  1235. public const byte ClientAuthenticationData = 214;
  1236. /// <summary>(203) Code for MasterClientId, which is synced by server. When sent as op-parameter this is code 203.</summary>
  1237. /// <remarks>Tightly related to GamePropertyKey.MasterClientId.</remarks>
  1238. public const byte MasterClientId = (byte)203;
  1239. /// <summary>(1) Used in Op FindFriends request. Value must be string[] of friends to look up.</summary>
  1240. public const byte FindFriendsRequestList = (byte)1;
  1241. /// <summary>(2) Used in Op FindFriends request. An integer containing option-flags to filter the results.</summary>
  1242. public const byte FindFriendsOptions = (byte)2;
  1243. /// <summary>(1) Used in Op FindFriends response. Contains bool[] list of online states (false if not online).</summary>
  1244. public const byte FindFriendsResponseOnlineList = (byte)1;
  1245. /// <summary>(2) Used in Op FindFriends response. Contains string[] of room names ("" where not known or no room joined).</summary>
  1246. public const byte FindFriendsResponseRoomIdList = (byte)2;
  1247. /// <summary>(213) Used in matchmaking-related methods and when creating a room to name a lobby (to join or to attach a room to).</summary>
  1248. public const byte LobbyName = (byte)213;
  1249. /// <summary>(212) Used in matchmaking-related methods and when creating a room to define the type of a lobby. Combined with the lobby name this identifies the lobby.</summary>
  1250. public const byte LobbyType = (byte)212;
  1251. /// <summary>(211) This (optional) parameter can be sent in Op Authenticate to turn on Lobby Stats (info about lobby names and their user- and game-counts).</summary>
  1252. public const byte LobbyStats = (byte)211;
  1253. /// <summary>(210) Used for region values in OpAuth and OpGetRegions.</summary>
  1254. public const byte Region = (byte)210;
  1255. /// <summary>(209) Path of the WebRPC that got called. Also known as "WebRpc Name". Type: string.</summary>
  1256. public const byte UriPath = 209;
  1257. /// <summary>(208) Parameters for a WebRPC as: Dictionary&lt;string, object&gt;. This will get serialized to JSon.</summary>
  1258. public const byte WebRpcParameters = 208;
  1259. /// <summary>(207) ReturnCode for the WebRPC, as sent by the web service (not by Photon, which uses ErrorCode). Type: byte.</summary>
  1260. public const byte WebRpcReturnCode = 207;
  1261. /// <summary>(206) Message returned by WebRPC server. Analog to Photon's debug message. Type: string.</summary>
  1262. public const byte WebRpcReturnMessage = 206;
  1263. /// <summary>(205) Used to define a "slice" for cached events. Slices can easily be removed from cache. Type: int.</summary>
  1264. public const byte CacheSliceIndex = 205;
  1265. /// <summary>(204) Informs the server of the expected plugin setup.</summary>
  1266. /// <remarks>
  1267. /// The operation will fail in case of a plugin mismatch returning error code PluginMismatch 32751(0x7FFF - 16).
  1268. /// Setting string[]{} means the client expects no plugin to be setup.
  1269. /// Note: for backwards compatibility null omits any check.
  1270. /// </remarks>
  1271. public const byte Plugins = 204;
  1272. /// <summary>(202) Used by the server in Operation Responses, when it sends the nickname of the client (the user's nickname).</summary>
  1273. public const byte NickName = 202;
  1274. /// <summary>(201) Informs user about name of plugin load to game</summary>
  1275. public const byte PluginName = 201;
  1276. /// <summary>(200) Informs user about version of plugin load to game</summary>
  1277. public const byte PluginVersion = 200;
  1278. /// <summary>(196) Cluster info provided in OpAuthenticate/OpAuthenticateOnce responses.</summary>
  1279. public const byte Cluster = 196;
  1280. /// <summary>(195) Protocol which will be used by client to connect master/game servers. Used for nameserver.</summary>
  1281. public const byte ExpectedProtocol = 195;
  1282. /// <summary>(194) Set of custom parameters which are sent in auth request.</summary>
  1283. public const byte CustomInitData = 194;
  1284. /// <summary>(193) How are we going to encrypt data.</summary>
  1285. public const byte EncryptionMode = 193;
  1286. /// <summary>(192) Parameter of Authentication, which contains encryption keys (depends on AuthMode and EncryptionMode).</summary>
  1287. public const byte EncryptionData = 192;
  1288. /// <summary>(191) An int parameter summarizing several boolean room-options with bit-flags.</summary>
  1289. public const byte RoomOptionFlags = 191;
  1290. }
  1291. /// <summary>
  1292. /// Class for constants. Contains operation codes.
  1293. /// </summary>
  1294. /// <remarks>These constants are used internally.</remarks>
  1295. public class OperationCode
  1296. {
  1297. [Obsolete("Exchanging encrpytion keys is done internally in the lib now. Don't expect this operation-result.")]
  1298. public const byte ExchangeKeysForEncryption = 250;
  1299. /// <summary>(255) Code for OpJoin, to get into a room.</summary>
  1300. [Obsolete]
  1301. public const byte Join = 255;
  1302. /// <summary>(231) Authenticates this peer and connects to a virtual application</summary>
  1303. public const byte AuthenticateOnce = 231;
  1304. /// <summary>(230) Authenticates this peer and connects to a virtual application</summary>
  1305. public const byte Authenticate = 230;
  1306. /// <summary>(229) Joins lobby (on master)</summary>
  1307. public const byte JoinLobby = 229;
  1308. /// <summary>(228) Leaves lobby (on master)</summary>
  1309. public const byte LeaveLobby = 228;
  1310. /// <summary>(227) Creates a game (or fails if name exists)</summary>
  1311. public const byte CreateGame = 227;
  1312. /// <summary>(226) Join game (by name)</summary>
  1313. public const byte JoinGame = 226;
  1314. /// <summary>(225) Joins random game (on master)</summary>
  1315. public const byte JoinRandomGame = 225;
  1316. // public const byte CancelJoinRandom = 224; // obsolete, cause JoinRandom no longer is a "process". now provides result immediately
  1317. /// <summary>(254) Code for OpLeave, to get out of a room.</summary>
  1318. public const byte Leave = (byte)254;
  1319. /// <summary>(253) Raise event (in a room, for other actors/players)</summary>
  1320. public const byte RaiseEvent = (byte)253;
  1321. /// <summary>(252) Set Properties (of room or actor/player)</summary>
  1322. public const byte SetProperties = (byte)252;
  1323. /// <summary>(251) Get Properties</summary>
  1324. public const byte GetProperties = (byte)251;
  1325. /// <summary>(248) Operation code to change interest groups in Rooms (Lite application and extending ones).</summary>
  1326. public const byte ChangeGroups = (byte)248;
  1327. /// <summary>(222) Request the rooms and online status for a list of friends (by name, which should be unique).</summary>
  1328. public const byte FindFriends = 222;
  1329. /// <summary>(221) Request statistics about a specific list of lobbies (their user and game count).</summary>
  1330. public const byte GetLobbyStats = 221;
  1331. /// <summary>(220) Get list of regional servers from a NameServer.</summary>
  1332. public const byte GetRegions = 220;
  1333. /// <summary>(219) WebRpc Operation.</summary>
  1334. public const byte WebRpc = 219;
  1335. /// <summary>(218) Operation to set some server settings. Used with different parameters on various servers.</summary>
  1336. public const byte ServerSettings = 218;
  1337. /// <summary>(217) Get the game list matching a supplied sql filter (SqlListLobby only) </summary>
  1338. public const byte GetGameList = 217;
  1339. }
  1340. /// <summary>Defines possible values for OpJoinRoom and OpJoinOrCreate. It tells the server if the room can be only be joined normally, created implicitly or found on a web-service for Turnbased games.</summary>
  1341. /// <remarks>These values are not directly used by a game but implicitly set.</remarks>
  1342. public enum JoinMode : byte
  1343. {
  1344. /// <summary>Regular join. The room must exist.</summary>
  1345. Default = 0,
  1346. /// <summary>Join or create the room if it's not existing. Used for OpJoinOrCreate for example.</summary>
  1347. CreateIfNotExists = 1,
  1348. /// <summary>The room might be out of memory and should be loaded (if possible) from a Turnbased web-service.</summary>
  1349. JoinOrRejoin = 2,
  1350. /// <summary>Only re-join will be allowed. If the user is not yet in the room, this will fail.</summary>
  1351. RejoinOnly = 3,
  1352. }
  1353. /// <summary>
  1354. /// Options for matchmaking rules for OpJoinRandom.
  1355. /// </summary>
  1356. public enum MatchmakingMode : byte
  1357. {
  1358. /// <summary>Fills up rooms (oldest first) to get players together as fast as possible. Default.</summary>
  1359. /// <remarks>Makes most sense with MaxPlayers > 0 and games that can only start with more players.</remarks>
  1360. FillRoom = 0,
  1361. /// <summary>Distributes players across available rooms sequentially but takes filter into account. Without filter, rooms get players evenly distributed.</summary>
  1362. SerialMatching = 1,
  1363. /// <summary>Joins a (fully) random room. Expected properties must match but aside from this, any available room might be selected.</summary>
  1364. RandomMatching = 2
  1365. }
  1366. /// <summary>
  1367. /// Lite - OpRaiseEvent lets you chose which actors in the room should receive events.
  1368. /// By default, events are sent to "Others" but you can overrule this.
  1369. /// </summary>
  1370. public enum ReceiverGroup : byte
  1371. {
  1372. /// <summary>Default value (not sent). Anyone else gets my event.</summary>
  1373. Others = 0,
  1374. /// <summary>Everyone in the current room (including this peer) will get this event.</summary>
  1375. All = 1,
  1376. /// <summary>The server sends this event only to the actor with the lowest actorNumber.</summary>
  1377. /// <remarks>The "master client" does not have special rights but is the one who is in this room the longest time.</remarks>
  1378. MasterClient = 2,
  1379. }
  1380. /// <summary>
  1381. /// Lite - OpRaiseEvent allows you to cache events and automatically send them to joining players in a room.
  1382. /// Events are cached per event code and player: Event 100 (example!) can be stored once per player.
  1383. /// Cached events can be modified, replaced and removed.
  1384. /// </summary>
  1385. /// <remarks>
  1386. /// Caching works only combination with ReceiverGroup options Others and All.
  1387. /// </remarks>
  1388. public enum EventCaching : byte
  1389. {
  1390. /// <summary>Default value (not sent).</summary>
  1391. DoNotCache = 0,
  1392. /// <summary>Will merge this event's keys with those already cached.</summary>
  1393. [Obsolete]
  1394. MergeCache = 1,
  1395. /// <summary>Replaces the event cache for this eventCode with this event's content.</summary>
  1396. [Obsolete]
  1397. ReplaceCache = 2,
  1398. /// <summary>Removes this event (by eventCode) from the cache.</summary>
  1399. [Obsolete]
  1400. RemoveCache = 3,
  1401. /// <summary>Adds an event to the room's cache</summary>
  1402. AddToRoomCache = 4,
  1403. /// <summary>Adds this event to the cache for actor 0 (becoming a "globally owned" event in the cache).</summary>
  1404. AddToRoomCacheGlobal = 5,
  1405. /// <summary>Remove fitting event from the room's cache.</summary>
  1406. RemoveFromRoomCache = 6,
  1407. /// <summary>Removes events of players who already left the room (cleaning up).</summary>
  1408. RemoveFromRoomCacheForActorsLeft = 7,
  1409. /// <summary>Increase the index of the sliced cache.</summary>
  1410. SliceIncreaseIndex = 10,
  1411. /// <summary>Set the index of the sliced cache. You must set RaiseEventOptions.CacheSliceIndex for this.</summary>
  1412. SliceSetIndex = 11,
  1413. /// <summary>Purge cache slice with index. Exactly one slice is removed from cache. You must set RaiseEventOptions.CacheSliceIndex for this.</summary>
  1414. SlicePurgeIndex = 12,
  1415. /// <summary>Purge cache slices with specified index and anything lower than that. You must set RaiseEventOptions.CacheSliceIndex for this.</summary>
  1416. SlicePurgeUpToIndex = 13,
  1417. }
  1418. /// <summary>
  1419. /// Flags for "types of properties", being used as filter in OpGetProperties.
  1420. /// </summary>
  1421. [Flags]
  1422. public enum PropertyTypeFlag : byte
  1423. {
  1424. /// <summary>(0x00) Flag type for no property type.</summary>
  1425. None = 0x00,
  1426. /// <summary>(0x01) Flag type for game-attached properties.</summary>
  1427. Game = 0x01,
  1428. /// <summary>(0x02) Flag type for actor related propeties.</summary>
  1429. Actor = 0x02,
  1430. /// <summary>(0x01) Flag type for game AND actor properties. Equal to 'Game'</summary>
  1431. GameAndActor = Game | Actor
  1432. }
  1433. /// <summary>Wraps up common room properties needed when you create rooms. Read the individual entries for more details.</summary>
  1434. /// <remarks>This directly maps to the fields in the Room class.</remarks>
  1435. public class RoomOptions
  1436. {
  1437. /// <summary>Defines if this room is listed in the lobby. If not, it also is not joined randomly.</summary>
  1438. /// <remarks>
  1439. /// A room that is not visible will be excluded from the room lists that are sent to the clients in lobbies.
  1440. /// An invisible room can be joined by name but is excluded from random matchmaking.
  1441. ///
  1442. /// Use this to "hide" a room and simulate "private rooms". Players can exchange a roomname and create it
  1443. /// invisble to avoid anyone else joining it.
  1444. /// </remarks>
  1445. public bool IsVisible { get { return this.isVisible; } set { this.isVisible = value; } }
  1446. private bool isVisible = true;
  1447. /// <summary>Defines if this room can be joined at all.</summary>
  1448. /// <remarks>
  1449. /// If a room is closed, no player can join this. As example this makes sense when 3 of 4 possible players
  1450. /// start their gameplay early and don't want anyone to join during the game.
  1451. /// The room can still be listed in the lobby (set isVisible to control lobby-visibility).
  1452. /// </remarks>
  1453. public bool IsOpen { get { return this.isOpen; } set { this.isOpen = value; } }
  1454. private bool isOpen = true;
  1455. /// <summary>Max number of players that can be in the room at any time. 0 means "no limit".</summary>
  1456. public byte MaxPlayers;
  1457. /// <summary>Time To Live (TTL) for an 'actor' in a room. If a client disconnects, this actor is inactive first and removed after this timeout. In milliseconds.</summary>
  1458. public int PlayerTtl;
  1459. /// <summary>Time To Live (TTL) for a room when the last player leaves. Keeps room in memory for case a player re-joins soon. In milliseconds.</summary>
  1460. public int EmptyRoomTtl;
  1461. /// <summary>Removes a user's events and properties from the room when a user leaves.</summary>
  1462. /// <remarks>
  1463. /// This makes sense when in rooms where players can't place items in the room and just vanish entirely.
  1464. /// When you disable this, the event history can become too long to load if the room stays in use indefinitely.
  1465. /// Default: true. Cleans up the cache and props of leaving users.
  1466. /// </remarks>
  1467. public bool CleanupCacheOnLeave { get { return this.cleanupCacheOnLeave; } set { this.cleanupCacheOnLeave = value; } }
  1468. private bool cleanupCacheOnLeave = true;
  1469. /// <summary>The room's custom properties to set. Use string keys!</summary>
  1470. /// <remarks>
  1471. /// Custom room properties are any key-values you need to define the game's setup.
  1472. /// The shorter your keys are, the better.
  1473. /// Example: Map, Mode (could be "m" when used with "Map"), TileSet (could be "t").
  1474. /// </remarks>
  1475. public Hashtable CustomRoomProperties;
  1476. /// <summary>Defines the custom room properties that get listed in the lobby.</summary>
  1477. /// <remarks>
  1478. /// Name the custom room properties that should be available to clients that are in a lobby.
  1479. /// Use with care. Unless a custom property is essential for matchmaking or user info, it should
  1480. /// not be sent to the lobby, which causes traffic and delays for clients in the lobby.
  1481. ///
  1482. /// Default: No custom properties are sent to the lobby.
  1483. /// </remarks>
  1484. public string[] CustomRoomPropertiesForLobby = new string[0];
  1485. /// <summary>Informs the server of the expected plugin setup.</summary>
  1486. /// <remarks>
  1487. /// The operation will fail in case of a plugin missmatch returning error code PluginMismatch 32757(0x7FFF - 10).
  1488. /// Setting string[]{} means the client expects no plugin to be setup.
  1489. /// Note: for backwards compatibility null omits any check.
  1490. /// </remarks>
  1491. public string[] Plugins;
  1492. /// <summary>
  1493. /// Tells the server to skip room events for joining and leaving players.
  1494. /// </summary>
  1495. /// <remarks>
  1496. /// Using this makes the client unaware of the other players in a room.
  1497. /// That can save some traffic if you have some server logic that updates players
  1498. /// but it can also limit the client's usability.
  1499. /// </remarks>
  1500. public bool SuppressRoomEvents { get; set; }
  1501. /// <summary>
  1502. /// Defines if the UserIds of players get "published" in the room. Useful for FindFriends, if players want to play another game together.
  1503. /// </summary>
  1504. /// <remarks>
  1505. /// When you set this to true, Photon will publish the UserIds of the players in that room.
  1506. /// In that case, you can use PhotonPlayer.userId, to access any player's userID.
  1507. /// This is useful for FindFriends and to set "expected users" to reserve slots in a room.
  1508. /// </remarks>
  1509. public bool PublishUserId { get; set; }
  1510. /// <summary>Optionally, properties get deleted, when null gets assigned as value. Defaults to off / false.</summary>
  1511. /// <remarks>
  1512. /// When Op SetProperties is setting a key's value to null, the server and clients should remove the key/value from the Custom Properties.
  1513. /// By default, the server keeps the keys (and null values) and sends them to joining players.
  1514. ///
  1515. /// Important: Only when SetProperties does a "broadcast", the change (key, value = null) is sent to clients to update accordingly.
  1516. /// This applies to Custom Properties for rooms and actors/players.
  1517. /// </remarks>
  1518. public bool DeleteNullProperties { get; set; }
  1519. /// <summary>By default, property changes are sent back to the client that's setting them to avoid de-sync when properties are set concurrently.</summary>
  1520. /// <remarks>
  1521. /// This option is enables by default to fix this scenario:
  1522. ///
  1523. /// 1) On server, room property ABC is set to value FOO, which triggers notifications to all the clients telling them that the property changed.
  1524. /// 2) While that notification is in flight, a client sets the ABC property to value BAR.
  1525. /// 3) Client receives notification from the server and changes it�s local copy of ABC to FOO.
  1526. /// 4) Server receives the set operation and changes the official value of ABC to BAR, but never notifies the client that sent the set operation that the value is now BAR.
  1527. ///
  1528. /// Without this option, the client that set the value to BAR never hears from the server that the official copy has been updated to BAR, and thus gets stuck with a value of FOO.
  1529. /// </remarks>
  1530. public bool BroadcastPropsChangeToAll { get { return this.broadcastPropsChangeToAll; } set { this.broadcastPropsChangeToAll = value; } }
  1531. private bool broadcastPropsChangeToAll = true;
  1532. #if SERVERSDK
  1533. public bool CheckUserOnJoin { get; set; }
  1534. #endif
  1535. }
  1536. /// <summary>Aggregates several less-often used options for operation RaiseEvent. See field descriptions for usage details.</summary>
  1537. public class RaiseEventOptions
  1538. {
  1539. /// <summary>Default options: CachingOption: DoNotCache, InterestGroup: 0, targetActors: null, receivers: Others, sequenceChannel: 0.</summary>
  1540. public readonly static RaiseEventOptions Default = new RaiseEventOptions();
  1541. /// <summary>Defines if the server should simply send the event, put it in the cache or remove events that are like this one.</summary>
  1542. /// <remarks>
  1543. /// When using option: SliceSetIndex, SlicePurgeIndex or SlicePurgeUpToIndex, set a CacheSliceIndex. All other options except SequenceChannel get ignored.
  1544. /// </remarks>
  1545. public EventCaching CachingOption;
  1546. /// <summary>The number of the Interest Group to send this to. 0 goes to all users but to get 1 and up, clients must subscribe to the group first.</summary>
  1547. public byte InterestGroup;
  1548. /// <summary>A list of Player.ActorNumbers to send this event to. You can implement events that just go to specific users this way.</summary>
  1549. public int[] TargetActors;
  1550. /// <summary>Sends the event to All, MasterClient or Others (default). Be careful with MasterClient, as the client might disconnect before it got the event and it gets lost.</summary>
  1551. public ReceiverGroup Receivers;
  1552. /// <summary>Events are ordered per "channel". If you have events that are independent of others, they can go into another sequence or channel.</summary>
  1553. [Obsolete("Not used where SendOptions are a parameter too. Use SendOptions.Channel instead.")]
  1554. public byte SequenceChannel;
  1555. /// <summary> Optional flags to be used in Photon client SDKs with Op RaiseEvent and Op SetProperties.</summary>
  1556. /// <remarks>Introduced mainly for webhooks 1.2 to control behavior of forwarded HTTP requests.</remarks>
  1557. public WebFlags Flags = WebFlags.Default;
  1558. ///// <summary>Used along with CachingOption SliceSetIndex, SlicePurgeIndex or SlicePurgeUpToIndex if you want to set or purge a specific cache-slice.</summary>
  1559. //public int CacheSliceIndex;
  1560. }
  1561. /// <summary>Types of lobbies define their behaviour and capabilities. Check each value for details.</summary>
  1562. /// <remarks>Values of this enum must be matched by the server.</remarks>
  1563. public enum LobbyType :byte
  1564. {
  1565. /// <summary>Standard type and behaviour: While joined to this lobby clients get room-lists and JoinRandomRoom can use a simple filter to match properties (perfectly).</summary>
  1566. Default = 0,
  1567. /// <summary>This lobby type lists rooms like Default but JoinRandom has a parameter for SQL-like "where" clauses for filtering. This allows bigger, less, or and and combinations.</summary>
  1568. SqlLobby = 2,
  1569. /// <summary>This lobby does not send lists of games. It is only used for OpJoinRandomRoom. It keeps rooms available for a while when there are only inactive users left.</summary>
  1570. AsyncRandomLobby = 3
  1571. }
  1572. /// <summary>Refers to a specific lobby on the server.</summary>
  1573. /// <remarks>
  1574. /// Name and Type combined are the unique identifier for a lobby.<br/>
  1575. /// The server will create lobbies "on demand", so no registration or setup is required.<br/>
  1576. /// An empty or null Name always points to the "default lobby" as special case.
  1577. /// </remarks>
  1578. public class TypedLobby
  1579. {
  1580. /// <summary>
  1581. /// Name of the lobby. Default: null, pointing to the "default lobby".
  1582. /// </summary>
  1583. /// <remarks>
  1584. /// If Name is null or empty, a TypedLobby will point to the "default lobby". This ignores the Type value and always acts as <see cref="LobbyType.Default"/>.
  1585. /// </remarks>
  1586. public string Name;
  1587. /// <summary>
  1588. /// Type (and behaviour) of the lobby.
  1589. /// </summary>
  1590. /// <remarks>
  1591. /// An empty or null Name always points to the "default lobby" as special case.
  1592. /// </remarks>
  1593. public LobbyType Type;
  1594. /// <summary>
  1595. /// A reference to the default lobby which is the unique lobby that uses null as name and is of type <see cref="LobbyType.Default"/>.
  1596. /// </summary>
  1597. /// <remarks>
  1598. /// There is only a single lobby with an empty name on the server. It is always of type <see cref="LobbyType.Default"/>.<br/>
  1599. /// On the other hand, this is a shortcut and reusable reference to the default lobby.<br/>
  1600. /// Do not change Name or Type.<br/>
  1601. /// </remarks>
  1602. public static readonly TypedLobby Default = new TypedLobby();
  1603. /// <summary>
  1604. /// Returns whether or not this instance points to the "default lobby" (<see cref="TypedLobby.Default"/>).
  1605. /// </summary>
  1606. /// <remarks>
  1607. /// This comes up to checking if the Name is null or empty.
  1608. /// <see cref="LobbyType.Default"/> is not the same thing as the "default lobby" (<see cref="TypedLobby.Default"/>).
  1609. /// </remarks>
  1610. public bool IsDefault { get { return string.IsNullOrEmpty(this.Name); } }
  1611. /// <summary>
  1612. /// Creates a TypedLobby instance. Unless Name is changed, this points to the "default lobby" (<see cref="TypedLobby.Default"/>).
  1613. /// </summary>
  1614. internal TypedLobby()
  1615. {
  1616. }
  1617. /// <summary>
  1618. /// Sets Name and Type of the new instance. Make sure name is not empty or null, as that always points to the "default lobby" (<see cref="TypedLobby.Default"/>).
  1619. /// </summary>
  1620. /// <param name="name">Some string to identify a lobby.</param>
  1621. /// <param name="type">The type of a lobby defines it's capabilities and behaviour.</param>
  1622. public TypedLobby(string name, LobbyType type)
  1623. {
  1624. this.Name = name;
  1625. this.Type = type;
  1626. }
  1627. public override string ToString()
  1628. {
  1629. return string.Format("lobby '{0}'[{1}]", this.Name, this.Type);
  1630. }
  1631. }
  1632. /// <summary>
  1633. /// Info for a lobby on the server. Used when <see cref="LoadBalancingClient.EnableLobbyStatistics"/> is true.
  1634. /// </summary>
  1635. public class TypedLobbyInfo : TypedLobby
  1636. {
  1637. /// <summary>Count of players that currently joined this lobby.</summary>
  1638. public int PlayerCount;
  1639. /// <summary>Count of rooms currently associated with this lobby.</summary>
  1640. public int RoomCount;
  1641. public override string ToString()
  1642. {
  1643. return string.Format("TypedLobbyInfo '{0}'[{1}] rooms: {2} players: {3}", this.Name, this.Type, this.RoomCount, this.PlayerCount);
  1644. }
  1645. }
  1646. /// <summary>
  1647. /// Options for authentication modes. From "classic" auth on each server to AuthOnce (on NameServer).
  1648. /// </summary>
  1649. public enum AuthModeOption { Auth, AuthOnce, AuthOnceWss }
  1650. /// <summary>
  1651. /// Options for optional "Custom Authentication" services used with Photon. Used by OpAuthenticate after connecting to Photon.
  1652. /// </summary>
  1653. public enum CustomAuthenticationType : byte
  1654. {
  1655. /// <summary>Use a custom authentification service. Currently the only implemented option.</summary>
  1656. Custom = 0,
  1657. /// <summary>Authenticates users by their Steam Account. Set auth values accordingly!</summary>
  1658. Steam = 1,
  1659. /// <summary>Authenticates users by their Facebook Account. Set auth values accordingly!</summary>
  1660. Facebook = 2,
  1661. /// <summary>Authenticates users by their Oculus Account and token.</summary>
  1662. Oculus = 3,
  1663. /// <summary>Authenticates users by their PSN Account and token.</summary>
  1664. PlayStation = 4,
  1665. /// <summary>Authenticates users by their Xbox Account and XSTS token.</summary>
  1666. Xbox = 5,
  1667. /// <summary>Authenticates users by their HTC Viveport Account and user token. Set AuthGetParameters to "userToken=[userToken]"</summary>
  1668. Viveport = 10,
  1669. /// <summary>Authenticates users by their NSA ID.</summary>
  1670. NintendoSwitch = 11,
  1671. /// <summary>Disables custom authentification. Same as not providing any AuthenticationValues for connect (more precisely for: OpAuthenticate).</summary>
  1672. None = byte.MaxValue
  1673. }
  1674. /// <summary>
  1675. /// Container for user authentication in Photon. Set AuthValues before you connect - all else is handled.
  1676. /// </summary>
  1677. /// <remarks>
  1678. /// On Photon, user authentication is optional but can be useful in many cases.
  1679. /// If you want to FindFriends, a unique ID per user is very practical.
  1680. ///
  1681. /// There are basically three options for user authentication: None at all, the client sets some UserId
  1682. /// or you can use some account web-service to authenticate a user (and set the UserId server-side).
  1683. ///
  1684. /// Custom Authentication lets you verify end-users by some kind of login or token. It sends those
  1685. /// values to Photon which will verify them before granting access or disconnecting the client.
  1686. ///
  1687. /// The AuthValues are sent in OpAuthenticate when you connect, so they must be set before you connect.
  1688. /// If the AuthValues.UserId is null or empty when it's sent to the server, then the Photon Server assigns a UserId!
  1689. ///
  1690. /// The Photon Cloud Dashboard will let you enable this feature and set important server values for it.
  1691. /// https://dashboard.photonengine.com
  1692. /// </remarks>
  1693. public class AuthenticationValues
  1694. {
  1695. /// <summary>See AuthType.</summary>
  1696. private CustomAuthenticationType authType = CustomAuthenticationType.None;
  1697. /// <summary>The type of custom authentication provider that should be used. Currently only "Custom" or "None" (turns this off).</summary>
  1698. public CustomAuthenticationType AuthType
  1699. {
  1700. get { return authType; }
  1701. set { authType = value; }
  1702. }
  1703. /// <summary>This string must contain any (http get) parameters expected by the used authentication service. By default, username and token.</summary>
  1704. /// <remarks>
  1705. /// Maps to operation parameter 216.
  1706. /// Standard http get parameters are used here and passed on to the service that's defined in the server (Photon Cloud Dashboard).
  1707. /// </remarks>
  1708. public string AuthGetParameters { get; set; }
  1709. /// <summary>Data to be passed-on to the auth service via POST. Default: null (not sent). Either string or byte[] (see setters).</summary>
  1710. /// <remarks>Maps to operation parameter 214.</remarks>
  1711. public object AuthPostData { get; private set; }
  1712. /// <summary>After initial authentication, Photon provides a token for this client / user, which is subsequently used as (cached) validation.</summary>
  1713. public string Token { get; set; }
  1714. /// <summary>The UserId should be a unique identifier per user. This is for finding friends, etc..</summary>
  1715. /// <remarks>See remarks of AuthValues for info about how this is set and used.</remarks>
  1716. public string UserId { get; set; }
  1717. /// <summary>Creates empty auth values without any info.</summary>
  1718. public AuthenticationValues()
  1719. {
  1720. }
  1721. /// <summary>Creates minimal info about the user. If this is authenticated or not, depends on the set AuthType.</summary>
  1722. /// <param name="userId">Some UserId to set in Photon.</param>
  1723. public AuthenticationValues(string userId)
  1724. {
  1725. this.UserId = userId;
  1726. }
  1727. /// <summary>Sets the data to be passed-on to the auth service via POST.</summary>
  1728. /// <remarks>AuthPostData is just one value. Each SetAuthPostData replaces any previous value. It can be either a string, a byte[] or a dictionary. Each SetAuthPostData replaces any previous value.</remarks>
  1729. /// <param name="stringData">String data to be used in the body of the POST request. Null or empty string will set AuthPostData to null.</param>
  1730. public virtual void SetAuthPostData(string stringData)
  1731. {
  1732. this.AuthPostData = (string.IsNullOrEmpty(stringData)) ? null : stringData;
  1733. }
  1734. /// <summary>Sets the data to be passed-on to the auth service via POST.</summary>
  1735. /// <remarks>AuthPostData is just one value. Each SetAuthPostData replaces any previous value. It can be either a string, a byte[] or a dictionary. Each SetAuthPostData replaces any previous value.</remarks>
  1736. /// <param name="byteData">Binary token / auth-data to pass on.</param>
  1737. public virtual void SetAuthPostData(byte[] byteData)
  1738. {
  1739. this.AuthPostData = byteData;
  1740. }
  1741. /// <summary>Sets data to be passed-on to the auth service as Json (Content-Type: "application/json") via Post.</summary>
  1742. /// <remarks>AuthPostData is just one value. Each SetAuthPostData replaces any previous value. It can be either a string, a byte[] or a dictionary. Each SetAuthPostData replaces any previous value.</remarks>
  1743. /// <param name="dictData">A authentication-data dictionary will be converted to Json and passed to the Auth webservice via HTTP Post.</param>
  1744. public virtual void SetAuthPostData(Dictionary<string, object> dictData)
  1745. {
  1746. this.AuthPostData = dictData;
  1747. }
  1748. /// <summary>Adds a key-value pair to the get-parameters used for Custom Auth (AuthGetParameters).</summary>
  1749. /// <remarks>This method does uri-encoding for you.</remarks>
  1750. /// <param name="key">Key for the value to set.</param>
  1751. /// <param name="value">Some value relevant for Custom Authentication.</param>
  1752. public virtual void AddAuthParameter(string key, string value)
  1753. {
  1754. string ampersand = string.IsNullOrEmpty(this.AuthGetParameters) ? "" : "&";
  1755. this.AuthGetParameters = string.Format("{0}{1}{2}={3}", this.AuthGetParameters, ampersand, System.Uri.EscapeDataString(key), System.Uri.EscapeDataString(value));
  1756. }
  1757. public override string ToString()
  1758. {
  1759. return string.Format("AuthenticationValues Type: {3} UserId: {0}, GetParameters: {1} Token available: {2}", this.UserId, this.AuthGetParameters, !string.IsNullOrEmpty(this.Token), this.AuthType);
  1760. }
  1761. }
  1762. }