Skip to content

Commit 9a9cb56

Browse files
committed
+Version 0.1.0
0 parents  commit 9a9cb56

17 files changed

+909
-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: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
public enum AsyncNetworkResult
9+
{
10+
SUCCESS,
11+
E_HTTP,
12+
E_NETWORK,
13+
E_DATA_NULL,
14+
E_DATA_EMPTY,
15+
E_PARSER
16+
}
17+
18+
public enum AsyncNetworkEngineCloud
19+
{
20+
GCP = 0,
21+
AWS = 1
22+
}
23+
24+
/// <summary>
25+
/// Implements a UnityRequest for google protobuf web functions.
26+
/// </summary>
27+
/// <typeparam name="T">Request</typeparam>
28+
/// <typeparam name="TU">Response</typeparam>
29+
/// <typeparam name="TV">GenericError</typeparam>
30+
public static class AsyncNetworkEngine<T, TU, TV>
31+
where T : IMessage<T>, new()
32+
where TU : IMessage<TU>, new()
33+
where TV : IMessage<TV>, 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<TV> _tvParser = new MessageParser<TV>(() => new TV());
38+
private static EventService _eventService = null;
39+
40+
public static void Send(string uri, T request, Action<AsyncNetworkResult, TU, TV> 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, T request, Action<AsyncNetworkResult, TU, TV> 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+
if (rqt.result != UnityWebRequest.Result.Success) { Return(AsyncNetworkResult.E_NETWORK, result); yield break; }
56+
57+
while (!rqt.downloadHandler.isDone) yield return null;
58+
if (rqt.downloadHandler.data == null) { Return(AsyncNetworkResult.E_DATA_NULL, result); yield break; }
59+
if (rqt.downloadHandler.data.Length <= 0) { Return(AsyncNetworkResult.E_DATA_EMPTY, result); yield break; }
60+
if (rqt.responseCode != 200)
61+
{
62+
if (rqt.downloadHandler.data == null) { Return(AsyncNetworkResult.E_DATA_NULL, result); yield break; }
63+
if (rqt.downloadHandler.data.Length <= 0) { Return(AsyncNetworkResult.E_DATA_EMPTY, result); yield break; }
64+
try
65+
{
66+
if(Cloud == AsyncNetworkEngineCloud.AWS)
67+
{
68+
var data = Base64Url.Decode(Encoding.ASCII.GetString(rqt.downloadHandler.data));
69+
var error = _tvParser.ParseFrom(data);
70+
Return(AsyncNetworkResult.E_HTTP, default, error, result);
71+
}
72+
else
73+
{
74+
var error = _tvParser.ParseFrom(rqt.downloadHandler.data);
75+
Return(AsyncNetworkResult.E_HTTP, default, error, result);
76+
}
77+
}
78+
catch
79+
{
80+
Return(AsyncNetworkResult.E_HTTP, result);
81+
}
82+
yield break;
83+
}
84+
85+
try
86+
{
87+
if (Cloud == AsyncNetworkEngineCloud.AWS)
88+
{
89+
var data = Base64Url.Decode(Encoding.ASCII.GetString(rqt.downloadHandler.data));
90+
var packet = _tuParser.ParseFrom(data);
91+
Return(AsyncNetworkResult.SUCCESS, packet, default, result);
92+
}
93+
else
94+
{
95+
var packet = _tuParser.ParseFrom(rqt.downloadHandler.data);
96+
Return(AsyncNetworkResult.SUCCESS, packet, default, result);
97+
}
98+
}
99+
catch
100+
{
101+
Return(AsyncNetworkResult.E_PARSER, result);
102+
}
103+
}
104+
105+
private static void Return(AsyncNetworkResult result, Action<AsyncNetworkResult, TU, TV> callback)
106+
{
107+
Return(result, default, default, callback);
108+
}
109+
110+
private static void Return(AsyncNetworkResult result, TU data, TV error, Action<AsyncNetworkResult, TU, TV> callback)
111+
{
112+
if (callback == null) return;
113+
_eventService.QueueAction(() => callback.Invoke(result, data, error));
114+
}
115+
}

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

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)