Skip to content

Commit da5187b

Browse files
committed
+version 0.1.1
0 parents  commit da5187b

17 files changed

+920
-0
lines changed

.icon.png

6.3 KB
Loading

LICENSE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
MIT License
2+
3+
Copyright (c) 2015, Unity Technologies
4+
5+
Copyright (c) 2020 Game Workstore
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.

LICENSE.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Async Network Engine
2+
3+
Unity http requests made easy to interoperability with Go Lang + Google Protobuf functions!
4+
5+
Supported Cloud Functions:
6+
- Google Cloud Functions
7+
- Amazon Web Services Lambdas
8+
9+
Use at your own risk!
10+
11+
# How to install
12+
13+
At package.json, add these 3 lines of code:
14+
```json
15+
"com.gameworkstore.asyncnetworkengine": "git://github.com/GameWorkstore/async-network-engine.git",
16+
"com.gameworkstore.googleprotobufunity": "git://github.com/GameWorkstore/google-protobuf-unity.git",
17+
"com.gameworkstore.patterns": "git://github.com/GameWorkstore/patterns.git"
18+
```
19+
20+
And wait for unity to download and compile the package.
21+
22+
for update package for a newer version, install UpmGitExtension and update on [ Window > Package Manager ]!
23+
> https://github.com/mob-sakai/UpmGitExtension
24+
25+
# Contributions
26+
27+
If you are using this library and want to submit a change, go ahead! Overall, this project accepts contributions if:
28+
- Is a bug fix;
29+
- Or, is a generalization of a well-known issue;
30+
- Or is performance improvement;
31+
- Or is an improvement to already supported feature.
32+
33+
Also, you can donate to allow us to drink coffee while we improve it for you!

README.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/AsyncNetworkEngine.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using Google.Protobuf;
2+
using GameWorkstore.Patterns;
3+
using System;
4+
using System.Collections;
5+
using System.Text;
6+
using UnityEngine.Networking;
7+
8+
namespace GameWorkstore.AsyncNetworkEngine
9+
{
10+
public enum AsyncNetworkResult
11+
{
12+
SUCCESS,
13+
E_NETWORK,
14+
E_PROCESS,
15+
E_DATA_NULL,
16+
E_DATA_EMPTY,
17+
E_PARSER
18+
}
19+
20+
public enum AsyncNetworkEngineCloud
21+
{
22+
GCP = 0,
23+
AWS = 1
24+
}
25+
26+
/// <summary>
27+
/// Implements a UnityRequest for google protobuf web functions.
28+
/// </summary>
29+
/// <typeparam name="TR">Request</typeparam>
30+
/// <typeparam name="TU">Response</typeparam>
31+
public static class AsyncNetworkEngine<TR, TU>
32+
where TR : IMessage<TR>, new()
33+
where TU : IMessage<TU>, new()
34+
{
35+
public static AsyncNetworkEngineCloud Cloud = AsyncNetworkEngineCloud.AWS;
36+
private static readonly MessageParser<TU> _tuParser = new MessageParser<TU>(() => new TU());
37+
private static readonly MessageParser<GenericErrorResponse> _tvParser = new MessageParser<GenericErrorResponse>(() => new GenericErrorResponse());
38+
private static EventService _eventService = null;
39+
40+
public static void Send(string uri, TR request, Action<AsyncNetworkResult, TU, GenericErrorResponse> result)
41+
{
42+
if (_eventService == null) _eventService = ServiceProvider.GetService<EventService>();
43+
_eventService.StartCoroutine(SendRequest(uri, request, result));
44+
}
45+
46+
public static IEnumerator SendRequest(string uri, TR request, Action<AsyncNetworkResult, TU, GenericErrorResponse> result)
47+
{
48+
//Notice: APIGateway automatically converts binary data into base64 strings
49+
using var rqt = new UnityWebRequest(uri, "POST")
50+
{
51+
uploadHandler = new UploadHandlerRaw(request.ToByteArray()),
52+
downloadHandler = new DownloadHandlerBuffer()
53+
};
54+
yield return rqt.SendWebRequest();
55+
56+
switch (rqt.result)
57+
{
58+
case UnityWebRequest.Result.ConnectionError:
59+
Return(AsyncNetworkResult.E_NETWORK, result);
60+
break;
61+
case UnityWebRequest.Result.ProtocolError:
62+
while (!rqt.downloadHandler.isDone) yield return null;
63+
if (rqt.downloadHandler.data == null) { Return(AsyncNetworkResult.E_DATA_NULL, result); yield break; }
64+
if (rqt.downloadHandler.data.Length <= 0) { Return(AsyncNetworkResult.E_DATA_EMPTY, result); yield break; }
65+
try
66+
{
67+
if (Cloud == AsyncNetworkEngineCloud.AWS)
68+
{
69+
var data = Base64Url.Decode(Encoding.ASCII.GetString(rqt.downloadHandler.data));
70+
var error = _tvParser.ParseFrom(data);
71+
Return(AsyncNetworkResult.E_PROCESS, default, error, result);
72+
}
73+
else
74+
{
75+
var error = _tvParser.ParseFrom(rqt.downloadHandler.data);
76+
Return(AsyncNetworkResult.E_PROCESS, default, error, result);
77+
}
78+
}
79+
catch
80+
{
81+
Return(AsyncNetworkResult.E_PROCESS, result);
82+
}
83+
break;
84+
case UnityWebRequest.Result.Success:
85+
while (!rqt.downloadHandler.isDone) yield return null;
86+
if (rqt.downloadHandler.data == null) { Return(AsyncNetworkResult.E_DATA_NULL, result); yield break; }
87+
if (rqt.downloadHandler.data.Length <= 0) { Return(AsyncNetworkResult.E_DATA_EMPTY, result); yield break; }
88+
try
89+
{
90+
if (Cloud == AsyncNetworkEngineCloud.AWS)
91+
{
92+
var data = Base64Url.Decode(Encoding.ASCII.GetString(rqt.downloadHandler.data));
93+
var packet = _tuParser.ParseFrom(data);
94+
Return(AsyncNetworkResult.SUCCESS, packet, default, result);
95+
}
96+
else
97+
{
98+
var packet = _tuParser.ParseFrom(rqt.downloadHandler.data);
99+
Return(AsyncNetworkResult.SUCCESS, packet, default, result);
100+
}
101+
}
102+
catch
103+
{
104+
Return(AsyncNetworkResult.E_PARSER, result);
105+
}
106+
break;
107+
}
108+
}
109+
110+
private static void Return(AsyncNetworkResult result, Action<AsyncNetworkResult, TU, GenericErrorResponse> callback)
111+
{
112+
Return(result, default, default, callback);
113+
}
114+
115+
private static void Return(AsyncNetworkResult result, TU data, GenericErrorResponse error, Action<AsyncNetworkResult, TU, GenericErrorResponse> callback)
116+
{
117+
if (callback == null) return;
118+
_eventService.QueueAction(() => callback.Invoke(result, data, error));
119+
}
120+
}
121+
}

Runtime/AsyncNetworkEngine.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Base64Url.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
3+
namespace GameWorkstore.AsyncNetworkEngine
4+
{
5+
6+
public static class Base64Url
7+
{
8+
public static string Encode(byte[] input)
9+
{
10+
var output = Convert.ToBase64String(input);
11+
output = output.Replace('+', '-'); // 62nd char of encoding
12+
output = output.Replace('/', '_'); // 63rd char of encoding
13+
output = output.Split('=')[0]; // Remove any trailing '='s
14+
15+
return output;
16+
}
17+
18+
public static byte[] Decode(string input)
19+
{
20+
var output = input;
21+
22+
output = output.Replace('-', '+'); // 62nd char of encoding
23+
output = output.Replace('_', '/'); // 63rd char of encoding
24+
25+
switch (output.Length % 4) // Pad with trailing '='s
26+
{
27+
case 0:
28+
break; // No pad chars in this case
29+
case 2:
30+
output += "==";
31+
break; // Two pad chars
32+
case 3:
33+
output += "=";
34+
break; // One pad char
35+
default:
36+
throw new ArgumentOutOfRangeException(nameof(input), "Illegal base64url string!");
37+
}
38+
39+
return Convert.FromBase64String(output);
40+
}
41+
}
42+
}

Runtime/Base64Url.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)