com.xmobitea.changx.gn-unity 2.0.2 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/Editor/GNServerSettingsEditor.cs +42 -322
  2. package/Runtime/Common/GNArray.cs +120 -35
  3. package/Runtime/Common/GNData.cs +19 -6
  4. package/Runtime/Common/GNHashtable.cs +174 -11
  5. package/Runtime/Common/IGNData.cs +11 -2
  6. package/Runtime/Config/GNServerSettings.cs +136 -92
  7. package/Runtime/Constant/Commands.cs +82 -19
  8. package/Runtime/Constant/EnumType/GoogleLoginType.cs +5 -5
  9. package/Runtime/Constant/EnumType/GroupStatus.cs +7 -7
  10. package/Runtime/Constant/EnumType/ItemType.cs +5 -5
  11. package/Runtime/Constant/EnumType/MatchmakingMemberStatus.cs +6 -6
  12. package/Runtime/Constant/EnumType/MatchmakingTicketStatus.cs +2 -2
  13. package/Runtime/Constant/EnumType/OwnerType.cs +3 -2
  14. package/Runtime/Constant/EnumType/PushPlatformType.cs +10 -0
  15. package/Runtime/Constant/EnumType/PushPlatformType.cs.meta +11 -0
  16. package/Runtime/Constant/EnumType/RequestType.cs +1 -0
  17. package/Runtime/Constant/EnumType/StoreReceiveType.cs +13 -0
  18. package/Runtime/Constant/EnumType/StoreReceiveType.cs.meta +11 -0
  19. package/Runtime/Constant/ErrorCode/GNErrorCode.cs +53 -48
  20. package/Runtime/Constant/EventCode.cs +29 -2
  21. package/Runtime/Constant/OperationCode.cs +249 -211
  22. package/Runtime/Constant/ParameterCode/GNParameterCode.cs +610 -466
  23. package/Runtime/Constant/ReturnCode.cs +50 -2
  24. package/Runtime/Entity/DataMember.cs +221 -3
  25. package/Runtime/Entity/GNMetadata.cs +46 -1
  26. package/Runtime/Entity/InvalidMember.cs +24 -5
  27. package/Runtime/Entity/Models/AuthenticateModels.cs +251 -207
  28. package/Runtime/Entity/Models/AuthenticateRequestModels.cs +153 -131
  29. package/Runtime/Entity/Models/AuthenticateResponseModels.cs +61 -51
  30. package/Runtime/Entity/Models/CharacterPlayerModels.cs +985 -849
  31. package/Runtime/Entity/Models/CharacterPlayerRequestModels.cs +957 -885
  32. package/Runtime/Entity/Models/CharacterPlayerResponseModels.cs +165 -150
  33. package/Runtime/Entity/Models/CloudScriptModels.cs +185 -0
  34. package/Runtime/Entity/Models/CloudScriptModels.cs.meta +11 -0
  35. package/Runtime/Entity/Models/CloudScriptRequestModels.cs +134 -0
  36. package/Runtime/Entity/Models/CloudScriptRequestModels.cs.meta +11 -0
  37. package/Runtime/Entity/Models/CloudScriptResponseModels.cs +34 -0
  38. package/Runtime/Entity/Models/CloudScriptResponseModels.cs.meta +11 -0
  39. package/Runtime/Entity/Models/ContentModels.cs +150 -153
  40. package/Runtime/Entity/Models/ContentRequestModels.cs +129 -129
  41. package/Runtime/Entity/Models/ContentResponseModels.cs +24 -24
  42. package/Runtime/Entity/Models/DashboardModels.cs +1684 -1156
  43. package/Runtime/Entity/Models/DashboardRequestModels.cs +220 -147
  44. package/Runtime/Entity/Models/DashboardResponseModels.cs +92 -57
  45. package/Runtime/Entity/Models/GamePlayerModels.cs +1073 -933
  46. package/Runtime/Entity/Models/GamePlayerRequestModels.cs +939 -867
  47. package/Runtime/Entity/Models/GamePlayerResponseModels.cs +162 -147
  48. package/Runtime/Entity/Models/GenericModels.cs +102 -102
  49. package/Runtime/Entity/Models/GroupModels.cs +812 -676
  50. package/Runtime/Entity/Models/GroupRequestModels.cs +741 -669
  51. package/Runtime/Entity/Models/GroupResponseModels.cs +129 -114
  52. package/Runtime/Entity/Models/InventoryModels.cs +667 -558
  53. package/Runtime/Entity/Models/InventoryRequestModels.cs +626 -579
  54. package/Runtime/Entity/Models/InventoryResponseModels.cs +109 -99
  55. package/Runtime/Entity/Models/MasterPlayerModels.cs +1882 -1340
  56. package/Runtime/Entity/Models/MasterPlayerRequestModels.cs +1643 -1253
  57. package/Runtime/Entity/Models/MasterPlayerResponseModels.cs +293 -213
  58. package/Runtime/Entity/Models/MultiplayerModels.cs +293 -223
  59. package/Runtime/Entity/Models/MultiplayerRequestModels.cs +199 -149
  60. package/Runtime/Entity/Models/MultiplayerResponseModels.cs +10 -0
  61. package/Runtime/Entity/Models/StoreInventoryModels.cs +514 -323
  62. package/Runtime/Entity/Models/StoreInventoryRequestModels.cs +236 -165
  63. package/Runtime/Entity/Models/StoreInventoryResponseModels.cs +45 -30
  64. package/Runtime/Entity/OperationEvent.cs +43 -7
  65. package/Runtime/Entity/OperationHelper.cs +18 -2
  66. package/Runtime/Entity/OperationRequest.cs +80 -37
  67. package/Runtime/Entity/OperationResponse.cs +111 -63
  68. package/Runtime/Entity/Request/CustomOperationRequest.cs +53 -2
  69. package/Runtime/Entity/Response/CustomOperationResponse.cs +25 -4
  70. package/Runtime/GNNetwork.cs +333 -21
  71. package/Runtime/GNNetworkApi.cs +25 -13
  72. package/Runtime/GNNetworkAuthenticateApi.cs +531 -98
  73. package/Runtime/GNNetworkCharacterPlayerApi.cs +1587 -762
  74. package/Runtime/GNNetworkCloudScriptApi.cs +181 -0
  75. package/Runtime/GNNetworkCloudScriptApi.cs.meta +11 -0
  76. package/Runtime/GNNetworkContentApi.cs +238 -132
  77. package/Runtime/GNNetworkDashboardApi.cs +278 -117
  78. package/Runtime/GNNetworkGamePlayerApi.cs +1558 -747
  79. package/Runtime/GNNetworkGroupApi.cs +1228 -582
  80. package/Runtime/GNNetworkInventoryApi.cs +1048 -507
  81. package/Runtime/GNNetworkMasterPlayerApi.cs +2586 -1067
  82. package/Runtime/GNNetworkMultiplayerApi.cs +328 -147
  83. package/Runtime/GNNetworkStoreInventoryApi.cs +388 -162
  84. package/Runtime/Helper/CodeHelper.cs +45 -3
  85. package/Runtime/Helper/ConverterService.cs +240 -85
  86. package/Runtime/Logger/GNDebug.cs +46 -3
  87. package/Runtime/Networking/AuthenticateStatus.cs +30 -8
  88. package/Runtime/Networking/Handler/IServerEventHandler.cs +8 -8
  89. package/Runtime/Networking/Handler/OnCharacterPlayerFriendUpdateEventHandler.cs +31 -2
  90. package/Runtime/Networking/Handler/OnCharacterPlayerGroupUpdateEventHandler.cs +24 -3
  91. package/Runtime/Networking/Handler/OnGamePlayerFriendUpdateEventHandler.cs +24 -2
  92. package/Runtime/Networking/Handler/OnGamePlayerGroupUpdateEventHandler.cs +24 -2
  93. package/Runtime/Networking/Handler/OnGroupMemberUpdateEventHandler.cs +21 -2
  94. package/Runtime/Networking/Handler/OnGroupMessageUpdateEventHandler.cs +20 -2
  95. package/Runtime/Networking/Http/HttpPeer.cs +68 -25
  96. package/Runtime/Networking/Http/NetworkingHttpPeerBase.cs +44 -4
  97. package/Runtime/Networking/Http/NetworkingPeerHttpClientRequest.cs +63 -40
  98. package/Runtime/Networking/Http/NetworkingPeerHttpRequest.cs +43 -13
  99. package/Runtime/Networking/Http/NetworkingPeerUnityWebRequest.cs +72 -39
  100. package/Runtime/Networking/IPeer.cs +24 -2
  101. package/Runtime/Networking/NetworkingPeer.cs +64 -10
  102. package/Runtime/Networking/NetworkingPeerAPI.cs +20 -21
  103. package/Runtime/Networking/OperationPending.cs +79 -15
  104. package/Runtime/Networking/PeerBase.cs +86 -23
  105. package/Runtime/Networking/Socket/NetworkingPeerSocketV2.cs +57 -18
  106. package/Runtime/Networking/Socket/NetworkingPeerSocketV3.cs +70 -10
  107. package/Runtime/Networking/Socket/NetworkingSocketPeerBase.cs +165 -23
  108. package/Runtime/Networking/Socket/SocketPeer.cs +63 -12
  109. package/Runtime/Unity/ServiceCoroutine.cs +10 -0
  110. package/Runtime/Unity/ServiceCoroutine.cs.meta +11 -0
  111. package/Runtime/Unity/ServiceUpdate.cs +14 -1
  112. package/package.json +1 -1
@@ -3,17 +3,22 @@ namespace XmobiTea.GN.Networking.Http
3
3
  using System;
4
4
  using System.Collections;
5
5
  using System.Text;
6
- using UnityEngine;
7
6
  using UnityEngine.Networking;
8
7
  using XmobiTea.GN.Common;
9
8
  using XmobiTea.GN.Constant;
10
9
  using XmobiTea.GN.Helper;
10
+ using XmobiTea.GN.Unity;
11
11
  using XmobiTea.MsgPack;
12
12
 
13
- class UnityWebRequestSupportMono : MonoBehaviour { }
14
-
13
+ /// <summary>
14
+ /// Handles HTTP networking using UnityWebRequest.
15
+ /// </summary>
15
16
  internal class NetworkingPeerUnityWebRequest : NetworkingHttpPeerBase
16
17
  {
18
+ /// <summary>
19
+ /// A certificate handler to accept all certificates.
20
+ /// Used for environments where certificates cannot be validated.
21
+ /// </summary>
17
22
  class AcceptAllCertificatesSignedWithASpecificKeyPublicKey : CertificateHandler
18
23
  {
19
24
  protected override bool ValidateCertificate(byte[] certificateData)
@@ -22,24 +27,51 @@ namespace XmobiTea.GN.Networking.Http
22
27
  }
23
28
  }
24
29
 
25
- private MonoBehaviour unityMonoBehaviour;
26
-
27
- public override void getRequest(string subUri, Action<HttpAppResponse> onHttpAppResponse, float timeout, string authToken, string secretKey)
30
+ private UnityEngine.MonoBehaviour unityMonoBehaviour;
31
+
32
+ /// <summary>
33
+ /// Sends a GET request using UnityWebRequest.
34
+ /// </summary>
35
+ /// <param name="subUri">The sub-URI for the request.</param>
36
+ /// <param name="onHttpAppResponse">Callback to handle the HTTP response.</param>
37
+ /// <param name="timeout">Timeout for the request, in seconds.</param>
38
+ /// <param name="authToken">Optional authentication token.</param>
39
+ /// <param name="secretKey">Optional secret key.</param>
40
+ public override void getRequest(string subUri, Action<HttpAppResponse> onHttpAppResponse, float timeout, string authToken, string secretKey, string gameId)
28
41
  {
29
- this.unityMonoBehaviour.StartCoroutine(this.ieGetRequestViaUnityWebRequest(subUri, onHttpAppResponse, timeout, authToken, secretKey));
42
+ this.unityMonoBehaviour.StartCoroutine(this.ieGetRequestViaUnityWebRequest(subUri, onHttpAppResponse, timeout, authToken, secretKey, gameId));
30
43
  }
31
44
 
32
- public override void postRequest(string subUri, GNHashtable param, PostType postType, Action<HttpAppResponse> onHttpAppResponse, float timeout, string authToken, string secretKey, GNHashtable customTags)
45
+ /// <summary>
46
+ /// Sends a POST request with specified parameters and content type using UnityWebRequest.
47
+ /// </summary>
48
+ /// <param name="subUri">The sub-URI for the request.</param>
49
+ /// <param name="param">The parameters to include in the request.</param>
50
+ /// <param name="postType">The type of POST request (e.g., JSON, MsgPack).</param>
51
+ /// <param name="onHttpAppResponse">Callback to handle the HTTP response.</param>
52
+ /// <param name="timeout">Timeout for the request, in seconds.</param>
53
+ /// <param name="authToken">Optional authentication token.</param>
54
+ /// <param name="secretKey">Optional secret key.</param>
55
+ /// <param name="customTags">Optional custom tags.</param>
56
+ public override void postRequest(string subUri, GNHashtable param, PostType postType, Action<HttpAppResponse> onHttpAppResponse, float timeout, string authToken, string secretKey, GNHashtable customTags, string gameId)
33
57
  {
34
- this.unityMonoBehaviour.StartCoroutine(this.iePostRequestViaUnityWebRequest(subUri, param, postType, onHttpAppResponse, timeout, authToken, secretKey, customTags));
58
+ this.unityMonoBehaviour.StartCoroutine(this.iePostRequestViaUnityWebRequest(subUri, param, postType, onHttpAppResponse, timeout, authToken, secretKey, customTags, gameId));
35
59
  }
36
60
 
61
+ /// <summary>
62
+ /// Sends a file upload request using UnityWebRequest.
63
+ /// </summary>
64
+ /// <param name="subUri">The sub-URI for the request.</param>
65
+ /// <param name="content">The file content to upload.</param>
66
+ /// <param name="mimetype">The MIME type of the file.</param>
67
+ /// <param name="onHttpAppResponse">Callback to handle the HTTP response.</param>
68
+ /// <param name="timeout">Timeout for the request, in seconds.</param>
37
69
  public override void postRequestUpload(string subUri, byte[] content, string mimetype, Action<HttpAppResponse> onHttpAppResponse, float timeout)
38
70
  {
39
71
  this.unityMonoBehaviour.StartCoroutine(this.iePostRequestUploadViaUnityWebRequest(subUri, content, mimetype, onHttpAppResponse, timeout));
40
72
  }
41
73
 
42
- private IEnumerator ieGetRequestViaUnityWebRequest(string subUri, Action<HttpAppResponse> onHttpAppResponse, float timeout, string authToken, string secretKey)
74
+ private IEnumerator ieGetRequestViaUnityWebRequest(string subUri, Action<HttpAppResponse> onHttpAppResponse, float timeout, string authToken, string secretKey, string gameId)
43
75
  {
44
76
  using (var unityWebRequest = UnityWebRequest.Get(new Uri(this.httpUrl + "/" + subUri)))
45
77
  {
@@ -50,18 +82,20 @@ namespace XmobiTea.GN.Networking.Http
50
82
 
51
83
  if (!string.IsNullOrEmpty(authToken)) unityWebRequest.SetRequestHeader(Commands.RequestAuthTokenCmd, authToken);
52
84
  if (!string.IsNullOrEmpty(secretKey)) unityWebRequest.SetRequestHeader(Commands.RequestSecretCmd, secretKey);
53
- //unityWebRequest.SetRequestHeader("User-Agent", userAgent);
85
+ if (!string.IsNullOrEmpty(gameId)) unityWebRequest.SetRequestHeader(Commands.RequestGameIdCmd, gameId);
54
86
 
55
87
  yield return unityWebRequest.SendWebRequest();
56
88
 
57
89
  certificateHandler.Dispose();
58
90
 
59
- var httpAppResponse = new HttpAppResponse();
60
- httpAppResponse.statusCode = unityWebRequest.responseCode;
91
+ var httpAppResponse = new HttpAppResponse
92
+ {
93
+ statusCode = unityWebRequest.responseCode
94
+ };
61
95
 
62
- if (unityWebRequest.result == UnityWebRequest.Result.ConnectionError
63
- || unityWebRequest.result == UnityWebRequest.Result.ProtocolError
64
- || unityWebRequest.result == UnityWebRequest.Result.DataProcessingError)
96
+ if (unityWebRequest.result == UnityWebRequest.Result.ConnectionError ||
97
+ unityWebRequest.result == UnityWebRequest.Result.ProtocolError ||
98
+ unityWebRequest.result == UnityWebRequest.Result.DataProcessingError)
65
99
  {
66
100
  httpAppResponse.error = unityWebRequest.error;
67
101
  }
@@ -76,7 +110,7 @@ namespace XmobiTea.GN.Networking.Http
76
110
  }
77
111
  }
78
112
 
79
- private IEnumerator iePostRequestViaUnityWebRequest(string subUri, GNHashtable param, PostType postType, Action<HttpAppResponse> onHttpAppResponse, float timeout, string authToken, string secretKey, GNHashtable customTags)
113
+ private IEnumerator iePostRequestViaUnityWebRequest(string subUri, GNHashtable param, PostType postType, Action<HttpAppResponse> onHttpAppResponse, float timeout, string authToken, string secretKey, GNHashtable customTags, string gameId)
80
114
  {
81
115
  using (var unityWebRequest = new UnityWebRequest(new Uri(this.httpUrl + "/" + subUri)))
82
116
  {
@@ -90,24 +124,16 @@ namespace XmobiTea.GN.Networking.Http
90
124
  if (param == null) param = new GNHashtable();
91
125
  if (customTags != null) param.add(GNParameterCode.CustomTags, customTags);
92
126
 
93
- //unityWebRequest.SetRequestHeader("User-Agent", userAgent);
94
-
95
127
  if (postType == PostType.Json)
96
128
  {
97
129
  unityWebRequest.SetRequestHeader(Commands.CONTENT_TYPE, Commands.APPLICATION_JSON);
98
-
99
130
  var content = Encoding.UTF8.GetBytes(Serializer.Serialize(param.toData()));
100
-
101
131
  unityWebRequest.uploadHandler = new UploadHandlerRaw(content);
102
132
  }
103
133
  else if (postType == PostType.MsgPack)
104
134
  {
105
135
  unityWebRequest.SetRequestHeader(Commands.CONTENT_TYPE, Commands.APPLICATION_MSGPACK);
106
-
107
- var dataSend = param.toData();
108
-
109
- var content = MsgPackSerializer.Serialize(dataSend);
110
-
136
+ var content = MsgPackSerializer.Serialize(param.toData());
111
137
  unityWebRequest.uploadHandler = new UploadHandlerRaw(content);
112
138
  }
113
139
 
@@ -115,17 +141,20 @@ namespace XmobiTea.GN.Networking.Http
115
141
 
116
142
  if (!string.IsNullOrEmpty(authToken)) unityWebRequest.SetRequestHeader(Commands.RequestAuthTokenCmd, authToken);
117
143
  if (!string.IsNullOrEmpty(secretKey)) unityWebRequest.SetRequestHeader(Commands.RequestSecretCmd, secretKey);
144
+ if (!string.IsNullOrEmpty(gameId)) unityWebRequest.SetRequestHeader(Commands.RequestGameIdCmd, gameId);
118
145
 
119
146
  yield return unityWebRequest.SendWebRequest();
120
147
 
121
148
  certificateHandler.Dispose();
122
149
 
123
- var httpAppResponse = new HttpAppResponse();
124
- httpAppResponse.statusCode = unityWebRequest.responseCode;
150
+ var httpAppResponse = new HttpAppResponse
151
+ {
152
+ statusCode = unityWebRequest.responseCode
153
+ };
125
154
 
126
- if (unityWebRequest.result == UnityWebRequest.Result.ConnectionError
127
- || unityWebRequest.result == UnityWebRequest.Result.ProtocolError
128
- || unityWebRequest.result == UnityWebRequest.Result.DataProcessingError)
155
+ if (unityWebRequest.result == UnityWebRequest.Result.ConnectionError ||
156
+ unityWebRequest.result == UnityWebRequest.Result.ProtocolError ||
157
+ unityWebRequest.result == UnityWebRequest.Result.DataProcessingError)
129
158
  {
130
159
  httpAppResponse.error = unityWebRequest.error;
131
160
  }
@@ -161,12 +190,14 @@ namespace XmobiTea.GN.Networking.Http
161
190
 
162
191
  certificateHandler.Dispose();
163
192
 
164
- var httpAppResponse = new HttpAppResponse();
165
- httpAppResponse.statusCode = unityWebRequest.responseCode;
193
+ var httpAppResponse = new HttpAppResponse
194
+ {
195
+ statusCode = unityWebRequest.responseCode
196
+ };
166
197
 
167
- if (unityWebRequest.result == UnityWebRequest.Result.ConnectionError
168
- || unityWebRequest.result == UnityWebRequest.Result.ProtocolError
169
- || unityWebRequest.result == UnityWebRequest.Result.DataProcessingError)
198
+ if (unityWebRequest.result == UnityWebRequest.Result.ConnectionError ||
199
+ unityWebRequest.result == UnityWebRequest.Result.ProtocolError ||
200
+ unityWebRequest.result == UnityWebRequest.Result.DataProcessingError)
170
201
  {
171
202
  httpAppResponse.error = unityWebRequest.error;
172
203
  }
@@ -181,12 +212,14 @@ namespace XmobiTea.GN.Networking.Http
181
212
  }
182
213
  }
183
214
 
215
+ /// <summary>
216
+ /// Constructor initializes Unity MonoBehaviour for coroutines.
217
+ /// </summary>
184
218
  public NetworkingPeerUnityWebRequest()
185
219
  {
186
- var unityWebRequestSupport = new GameObject("[GN] UnityWebRequestSupport");
220
+ this.unityMonoBehaviour = new UnityEngine.GameObject("[GN] UnityWebRequestSupport").AddComponent<ServiceCoroutine>();
187
221
 
188
- this.unityMonoBehaviour = unityWebRequestSupport.AddComponent<UnityWebRequestSupportMono>();
222
+ UnityEngine.GameObject.DontDestroyOnLoad(this.unityMonoBehaviour.gameObject);
189
223
  }
190
224
  }
191
-
192
225
  }
@@ -5,15 +5,37 @@
5
5
  using XmobiTea.GN.Constant;
6
6
  using XmobiTea.GN.Entity;
7
7
 
8
+ /// <summary>
9
+ /// Defines the interface for managing peer communication.
10
+ /// </summary>
8
11
  internal interface IPeer
9
12
  {
13
+ /// <summary>
14
+ /// Initializes the peer connection.
15
+ /// </summary>
10
16
  void initPeer();
11
17
 
12
- void enqueue(RequestType requestType, RequestRole requestRole, OperationRequest operationRequest, Action<OperationResponse> onOperationResponse, string authToken, string secretKey, GNHashtable customTags);
18
+ /// <summary>
19
+ /// Enqueues a request to be sent to the server.
20
+ /// </summary>
21
+ /// <param name="requestType">The type of request to send.</param>
22
+ /// <param name="requestRole">The role associated with the request.</param>
23
+ /// <param name="operationRequest">The operation request object containing the request data.</param>
24
+ /// <param name="onOperationResponse">The callback invoked upon receiving a response.</param>
25
+ /// <param name="authToken">The authentication token to include with the request.</param>
26
+ /// <param name="secretKey">The secret key to include with the request, if applicable.</param>
27
+ /// <param name="customTags">Custom tags to be attached to the request.</param>
28
+ void enqueue(RequestType requestType, RequestRole requestRole, OperationRequest operationRequest, Action<OperationResponse> onOperationResponse, string authToken, string secretKey, GNHashtable customTags, string gameId);
13
29
 
30
+ /// <summary>
31
+ /// Checks whether the peer is currently in use.
32
+ /// </summary>
33
+ /// <returns>True if the peer is in use, otherwise false.</returns>
14
34
  bool isUsing();
15
35
 
36
+ /// <summary>
37
+ /// Processes any pending requests or responses.
38
+ /// </summary>
16
39
  void service();
17
40
  }
18
-
19
41
  }
@@ -5,13 +5,21 @@
5
5
  using XmobiTea.GN.Constant;
6
6
  using XmobiTea.GN.Entity;
7
7
 
8
+ /// <summary>
9
+ /// Manages networking peers for HTTP and socket communication.
10
+ /// </summary>
8
11
  public partial class NetworkingPeer
9
12
  {
10
- private IPeer socketPeer;
11
- private IPeer httpPeer;
13
+ private IPeer socketPeer; // Socket peer instance.
14
+ private IPeer httpPeer; // HTTP peer instance.
12
15
 
13
- internal AuthenticateStatus authenticateStatus;
16
+ internal AuthenticateStatus authenticateStatus; // Stores authentication status.
17
+ internal string gameId;
14
18
 
19
+ /// <summary>
20
+ /// Calculates and returns the average ping between HTTP and socket peers.
21
+ /// </summary>
22
+ /// <returns>The average ping in milliseconds.</returns>
15
23
  internal int getPing()
16
24
  {
17
25
  var count = 0;
@@ -29,28 +37,43 @@
29
37
  totalPing += ((PeerBase)this.socketPeer).ping;
30
38
  }
31
39
 
32
- return totalPing / count;
40
+ return count > 0 ? totalPing / count : -1;
33
41
  }
34
42
 
43
+ /// <summary>
44
+ /// Gets the client ID of the socket connection.
45
+ /// </summary>
46
+ /// <returns>The client ID as a string.</returns>
35
47
  internal string getClientId()
36
48
  {
37
49
  return (this.socketPeer as Socket.SocketPeer).getClientId();
38
50
  }
39
51
 
40
- private long lastCurrentServerMilliseconds;
41
- private float lastGetCurrentMilliseconds;
52
+ private long lastCurrentServerMilliseconds; // The last known server time in milliseconds.
53
+ private float lastGetCurrentMilliseconds; // The local time in seconds when the server time was last updated.
42
54
 
55
+ /// <summary>
56
+ /// Sets the current server time in milliseconds.
57
+ /// </summary>
58
+ /// <param name="currentServerMilliseconds">The server time in milliseconds.</param>
43
59
  internal void setServerTimeMilliseconds(long currentServerMilliseconds)
44
60
  {
45
61
  this.lastCurrentServerMilliseconds = currentServerMilliseconds;
46
62
  this.lastGetCurrentMilliseconds = UnityEngine.Time.realtimeSinceStartup;
47
63
  }
48
64
 
65
+ /// <summary>
66
+ /// Gets the current server timestamp in milliseconds.
67
+ /// </summary>
68
+ /// <returns>The current server timestamp.</returns>
49
69
  internal long getServerTimestamp()
50
70
  {
51
71
  return this.lastCurrentServerMilliseconds + (int)((UnityEngine.Time.realtimeSinceStartup - this.lastGetCurrentMilliseconds) * 1000);
52
72
  }
53
73
 
74
+ /// <summary>
75
+ /// Initializes the HTTP and socket peers.
76
+ /// </summary>
54
77
  internal void initPeer()
55
78
  {
56
79
  this.setServerTimeMilliseconds(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
@@ -67,12 +90,18 @@
67
90
  this.socketPeer.initPeer();
68
91
  }
69
92
 
93
+ /// <summary>
94
+ /// Processes pending operations for both HTTP and socket peers.
95
+ /// </summary>
70
96
  internal void service()
71
97
  {
72
98
  this.httpPeer?.service();
73
99
  this.socketPeer?.service();
74
100
  }
75
101
 
102
+ /// <summary>
103
+ /// Sends a request via socket peer.
104
+ /// </summary>
76
105
  internal void sendViaSocket(RequestType requestType, RequestRole requestRole, OperationRequest request, Action<OperationResponse> onResponse = null, string authToken = null, string secretKey = null, GNHashtable customTags = null)
77
106
  {
78
107
  if (authToken == null) authToken = GNNetwork.getAuthenticateStatus().getAuthToken();
@@ -83,9 +112,12 @@
83
112
  else if (requestRole == RequestRole.Client) secretKey = GNNetwork.gnServerSettings.getClientSecretKey();
84
113
  }
85
114
 
86
- this.socketPeer.enqueue(requestType, requestRole, request, onResponse, authToken, secretKey, customTags);
115
+ this.socketPeer.enqueue(requestType, requestRole, request, onResponse, authToken, secretKey, customTags, this.gameId);
87
116
  }
88
117
 
118
+ /// <summary>
119
+ /// Sends a request via HTTP peer.
120
+ /// </summary>
89
121
  internal void sendViaHttp(RequestType requestType, RequestRole requestRole, OperationRequest request, Action<OperationResponse> onResponse = null, string authToken = null, string secretKey = null, GNHashtable customTags = null)
90
122
  {
91
123
  if (authToken == null) authToken = this.authenticateStatus.getAuthToken();
@@ -96,49 +128,71 @@
96
128
  else if (requestRole == RequestRole.Client) secretKey = GNNetwork.gnServerSettings.getClientSecretKey();
97
129
  }
98
130
 
99
- this.httpPeer.enqueue(requestType, requestRole, request, onResponse, authToken, secretKey, customTags);
131
+ this.httpPeer.enqueue(requestType, requestRole, request, onResponse, authToken, secretKey, customTags, this.gameId);
100
132
  }
101
133
 
134
+ /// <summary>
135
+ /// Sends an authentication request via socket.
136
+ /// </summary>
102
137
  internal void sendRequestAuthSocket()
103
138
  {
104
139
  (this.socketPeer as Socket.SocketPeer).sendRequestAuthSocket();
105
140
  }
106
141
 
142
+ /// <summary>
143
+ /// Connects to the socket with a specified callback.
144
+ /// </summary>
107
145
  internal void connectSocket(Action _onSocketConnect)
108
146
  {
109
147
  (this.socketPeer as Socket.SocketPeer).connect(_onSocketConnect);
110
148
  }
111
149
 
150
+ /// <summary>
151
+ /// Disconnects the socket with a specified callback.
152
+ /// </summary>
112
153
  internal void disconnectSocket(Action _onSocketDisconnect)
113
154
  {
114
155
  (this.socketPeer as Socket.SocketPeer).disconnect(_onSocketDisconnect);
115
156
  }
116
157
 
158
+ /// <summary>
159
+ /// Sets the handler for processing socket events.
160
+ /// </summary>
117
161
  internal void setOnEventHandler(Action<OperationEvent> _onEventHandler)
118
162
  {
119
163
  (this.socketPeer as Socket.SocketPeer).setOnEventHandler(_onEventHandler);
120
164
  }
121
165
 
166
+ /// <summary>
167
+ /// Sets a handler for socket connection events.
168
+ /// </summary>
122
169
  internal void setOnConnectHandler(Action _onConnectHandler)
123
170
  {
124
171
  (this.socketPeer as Socket.SocketPeer).setOnConnectHandler(_onConnectHandler);
125
172
  }
126
173
 
174
+ /// <summary>
175
+ /// Sets a handler for socket disconnection events.
176
+ /// </summary>
127
177
  internal void setOnDisconnectHandler(Action _onDisconnectHandler)
128
178
  {
129
179
  (this.socketPeer as Socket.SocketPeer).setOnDisconnectHandler(_onDisconnectHandler);
130
180
  }
131
181
 
182
+ /// <summary>
183
+ /// Removes the handler for socket connection events.
184
+ /// </summary>
132
185
  internal void removeOnConnectHandler(Action _onConnectHandler)
133
186
  {
134
187
  (this.socketPeer as Socket.SocketPeer).removeOnConnectHandler(_onConnectHandler);
135
188
  }
136
189
 
190
+ /// <summary>
191
+ /// Removes the handler for socket disconnection events.
192
+ /// </summary>
137
193
  internal void removeOnDisconnectHandler(Action _onDisconnectHandler)
138
194
  {
139
195
  (this.socketPeer as Socket.SocketPeer).removeOnDisconnectHandler(_onDisconnectHandler);
140
196
  }
141
-
142
197
  }
143
-
144
198
  }
@@ -4,33 +4,44 @@
4
4
  using System.Collections;
5
5
  using XmobiTea.GN.Common;
6
6
  using XmobiTea.GN.Constant;
7
- using XmobiTea.GN.Entity.Response;
8
7
  using XmobiTea.GN.Helper;
9
8
 
9
+ /// <summary>
10
+ /// Provides additional functionality for managing network peers.
11
+ /// </summary>
10
12
  public partial class NetworkingPeer
11
13
  {
12
- internal const string SYNC_TS = "ts";
13
- internal const string UPLOAD_FILE = "upload";
14
- internal const string DOWNLOAD_FILE = "download";
15
- internal const string API_JSON = "api/" + Commands.RequestCmd_Json;
16
- internal const string API_MSG_PACK = "api/" + Commands.RequestCmd_MsgPack;
17
-
14
+ internal const string SYNC_TS = "ts"; // API endpoint for synchronizing timestamps.
15
+ internal const string UPLOAD_FILE = "upload"; // API endpoint for file uploads.
16
+ internal const string DOWNLOAD_FILE = "download"; // API endpoint for file downloads.
17
+ internal const string API_JSON = "api/" + Commands.RequestCmd_Json; // Base path for JSON API requests.
18
+ internal const string API_MSG_PACK = "api/" + Commands.RequestCmd_MsgPack; // Base path for MsgPack API requests.
19
+
20
+ /// <summary>
21
+ /// Synchronizes the local time with the server time by retrieving the latest server timestamp.
22
+ /// </summary>
23
+ /// <param name="onResponse">An optional callback to handle the synchronized server timestamp.</param>
18
24
  internal void syncTs(Action<long> onResponse = null)
19
25
  {
26
+ // Sends a request to the SYNC_TS endpoint using the HTTP peer.
20
27
  (this.httpPeer as Http.HttpPeer).getRequest(NetworkingPeer.SYNC_TS, httpAppResponse =>
21
28
  {
29
+ // Processes the response if there is no error.
22
30
  if (!httpAppResponse.hasError())
23
31
  {
24
- if (httpAppResponse.statusCode == 200)
32
+ if (httpAppResponse.statusCode == 200) // HTTP success status.
25
33
  {
34
+ // Parses the response text into a list and converts it to a GNArray.
26
35
  var returnDatas = (IList)Parser.Parse(httpAppResponse.text);
27
36
  var gnArray = new GNArray.Builder()
28
37
  .addAll(returnDatas)
29
38
  .build();
30
39
 
40
+ // Extracts the return code and checks if the operation was successful.
31
41
  var returnCode = gnArray.getInt(0);
32
42
  if (returnCode == ReturnCode.Ok)
33
43
  {
44
+ // Retrieves the server timestamp from the response and updates the local timestamp.
34
45
  var gnHashtable = gnArray.getGNHashtable(1);
35
46
  var newServerTimestamp = gnHashtable.getLong("1", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
36
47
 
@@ -39,21 +50,9 @@
39
50
  }
40
51
  }
41
52
 
53
+ // Invokes the callback with the current server timestamp.
42
54
  onResponse?.Invoke(this.getServerTimestamp());
43
55
  });
44
56
  }
45
-
46
- //internal void accountLogin(string username, string password, Action<LoginOperationResponse> onResponse = null)
47
- //{
48
- // //var request = new AccountLoginOperationRequest(username, password).Build();
49
-
50
- // //SendViaHttp(request, response =>
51
- // //{
52
- // // var loginOperationResponse = new LoginOperationResponse();
53
- // // loginOperationResponse.SetupOperationResponse(response);
54
-
55
- // // onResponse?.Invoke(loginOperationResponse);
56
- // //});
57
- //}
58
57
  }
59
58
  }