-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
2,211 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Aki.Common.Http; | ||
using Fika.Core.Coop.Components; | ||
using System.Runtime.Serialization; | ||
|
||
namespace Fika.Core.Networking.Http.Models | ||
{ | ||
[DataContract] | ||
public struct GetHostStunRequest | ||
{ | ||
[DataMember(Name = "requestType")] | ||
public string RequestType; | ||
|
||
[DataMember(Name = "clientId")] | ||
public string ClientId; | ||
|
||
[DataMember(Name = "serverId")] | ||
public string ServerId; | ||
|
||
[DataMember(Name = "stunIp")] | ||
public string StunIp; | ||
|
||
[DataMember(Name = "stunPort")] | ||
public int StunPort; | ||
|
||
public GetHostStunRequest(string stunIp, int stunPort) | ||
{ | ||
RequestType = GetType().Name; | ||
ClientId = RequestHandler.SessionId; | ||
ServerId = CoopHandler.GetServerId(); | ||
StunIp = stunIp; | ||
StunPort = stunPort; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Runtime.Serialization; | ||
|
||
[DataContract] | ||
public struct GetHostStunResponse | ||
{ | ||
[DataMember(Name = "requestType")] | ||
public string RequestType; | ||
|
||
[DataMember(Name = "clientId")] | ||
public string ClientId; | ||
|
||
[DataMember(Name = "StunIp")] | ||
public string StunIp; | ||
|
||
[DataMember(Name = "StunPort")] | ||
public int StunPort; | ||
|
||
public GetHostStunResponse(string clientId, string stunIp, int stunPort) | ||
{ | ||
RequestType = GetType().Name; | ||
ClientId = clientId; | ||
StunIp = stunIp; | ||
StunPort = stunPort; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using LiteNetLib; | ||
using Aki.Common.Http; | ||
using Fika.Core.Networking.Websocket; | ||
using WebSocketSharp; | ||
using System.Security.Policy; | ||
using System; | ||
using Newtonsoft.Json; | ||
using System.Threading.Tasks; | ||
using Fika.Core.Networking.Http.Models; | ||
|
||
namespace Fika.Core.Networking.NatPunch | ||
{ | ||
public class FikaNatPunchClient | ||
{ | ||
public string Host { get; set; } | ||
public string Url { get; set; } | ||
public string SessionId { get; set; } | ||
|
||
private WebSocket _webSocket; | ||
private TaskCompletionSource<string> _receiveTaskCompletion; | ||
|
||
public StunIpEndPoint StunIpEndPoint { get; set; } | ||
|
||
public FikaNatPunchClient() | ||
{ | ||
// Assuming http protocol is always used | ||
Host = RequestHandler.Host.Replace("http", "ws"); | ||
SessionId = RequestHandler.SessionId; | ||
Url = $"{Host}/{SessionId}?"; | ||
|
||
_webSocket = new WebSocket(Url) | ||
{ | ||
WaitTime = TimeSpan.FromMinutes(1), | ||
EmitOnPing = true | ||
}; | ||
|
||
_webSocket.OnOpen += WebSocket_OnOpen; | ||
_webSocket.OnError += WebSocket_OnError; | ||
_webSocket.OnMessage += WebSocket_OnMessage; | ||
} | ||
|
||
public void Connect() | ||
{ | ||
_webSocket.Connect(); | ||
} | ||
|
||
public void Close() | ||
{ | ||
_webSocket.Close(); | ||
} | ||
|
||
private void WebSocket_OnOpen(object sender, EventArgs e) | ||
{ | ||
//Logger.LogInfo($"Connected to NAT Helper"); | ||
EFT.UI.ConsoleScreen.Log("Connected to FikaNatPunchService as client"); | ||
} | ||
|
||
private void WebSocket_OnMessage(object sender, MessageEventArgs e) | ||
{ | ||
if (_receiveTaskCompletion == null) | ||
return; | ||
|
||
if (_receiveTaskCompletion.Task.Status == TaskStatus.RanToCompletion) | ||
return; | ||
|
||
if (e == null) | ||
return; | ||
|
||
if (string.IsNullOrEmpty(e.Data)) | ||
return; | ||
|
||
_receiveTaskCompletion.SetResult(e.Data); | ||
} | ||
|
||
private void WebSocket_OnError(object sender, ErrorEventArgs e) | ||
{ | ||
EFT.UI.ConsoleScreen.LogError($"Websocket error {e}"); | ||
_webSocket.Close(); | ||
} | ||
|
||
private void Send<T1>(T1 o) | ||
{ | ||
var data = JsonConvert.SerializeObject(o); | ||
_webSocket.Send(data); | ||
} | ||
|
||
private async Task<T2> Receive<T2>() | ||
{ | ||
var data = await _receiveTaskCompletion.Task; | ||
var obj = JsonConvert.DeserializeObject<T2>(data); | ||
|
||
return obj; | ||
} | ||
|
||
private async Task<T2> SendAndReceiveAsync<T1, T2>(T1 o) | ||
{ | ||
_receiveTaskCompletion = new TaskCompletionSource<string>(); | ||
|
||
Send<T1>(o); | ||
|
||
var data = await Receive<T2>(); | ||
|
||
return data; | ||
} | ||
|
||
public async Task<GetHostStunResponse> GetHostStun(GetHostStunRequest getHostStunRequest) | ||
{ | ||
return await SendAndReceiveAsync<GetHostStunRequest, GetHostStunResponse>(getHostStunRequest); | ||
} | ||
} | ||
} |
Oops, something went wrong.