-
Notifications
You must be signed in to change notification settings - Fork 16
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
0 parents
commit adf155b
Showing
18 changed files
with
549 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Copyright 2019 Hourai Teahouse | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in the | ||
Software without restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the | ||
Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,5 @@ | ||
# Hourai Networking | ||
|
||
P2P networking with multiple backends. | ||
|
||
TODO(james7132): Document this further. |
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,17 @@ | ||
namespace HouraiTeahouse.Networking { | ||
|
||
public readonly struct AccountHandle { | ||
|
||
public readonly uint AccountID; | ||
|
||
public AccountHandle(uint accountId) { | ||
AccountID = AccountID; | ||
} | ||
|
||
public static implicit operator uint(AccountHandle handle) => handle.AccountID; | ||
public static implicit operator AccountHandle(uint id) => | ||
new AccountHandle(id); | ||
|
||
} | ||
|
||
} |
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,20 @@ | ||
using Discord; | ||
|
||
namespace HouraiTeahouse.Networking.Discord { | ||
|
||
public class DiscordIntegrationClient : IIntegrationClient { | ||
|
||
internal readonly Discord _discordClient; | ||
|
||
public IntegrationAccountHandle ActiveUser { get; private set; } | ||
public ILobbyManager LobbyManager { get; } | ||
|
||
public DiscordIntegrationClient(int clientId) { | ||
_discordClient = new Discord(clientId, | ||
DiscordCreateFlags.NoRequiredDiscord); | ||
LobbyManager = new DiscordLobbyManager(this); | ||
} | ||
|
||
} | ||
|
||
} |
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,116 @@ | ||
using System; | ||
using Discord; | ||
|
||
namespace HouraiTeahouse.Networking.Discord { | ||
|
||
public class DiscordLobby : LobbyBase, IDisposable { | ||
|
||
readonly Discord.LobbyManager _lobbyManager; | ||
public ulong Id { get; } | ||
|
||
public event Action<LobbyMember> OnMemberJoin; | ||
public event Action<LobbyMember> OnMemberLeave; | ||
public event Action<AccountHandle, byte[]> OnNetworkMessage; | ||
|
||
public DiscordLobby(DiscordLobbyManager manager, uint id) : base() { | ||
Id = id; | ||
_lobbyManager = manager._lobbyManager; | ||
_lobbyManager.OnMemberConnect += _OnMemberJoin; | ||
_lobbyManager.OnMemberDisconnect += _OnMemberLeave; | ||
_lobbyManager.OnNetworkMessage += _OnNetworkMessage; | ||
} | ||
|
||
public void Dispose() { | ||
if (_lobbyManager == null) return; | ||
_lobbyManager.OnMemberConnect -= _OnMemberJoin; | ||
_lobbyManager.OnMemberDisconnect -= _OnMemberLeave; | ||
_lobbyManager.OnNetworkMessage -= _OnNetworkMessage; | ||
} | ||
|
||
protected uint MemberCount => _lobbyManager.MemberCount(Id); | ||
protected ulong GetMemberId(int idx) => | ||
_lobbyManager.GetMemberUserId(Id, i); | ||
|
||
public override string GetMetadata(string key) => | ||
_lobbyManager.GetLobbyMetadataValue(Id, key); | ||
|
||
public override string GetMetadataByIndex(int idx) => | ||
_lobbyManager.GetLobbyMetadataKey(Id, idx); | ||
|
||
public override void SetMetadata(string key, string value) { | ||
var txn = _lobbyManager.GetLobbyUpdateTransaction(); | ||
txn.SetMetadata(key, value); | ||
_lobbyManager.UpdateLobby(Id, txn, (Discord.Result result, ref Discord.Lobby lobby) { | ||
if (result == Discord.Result.OK) return; | ||
// TODO(james7132): Implement | ||
}); | ||
} | ||
|
||
public override string DeleteMetadata(string key) { | ||
var txn = _lobbyManager.GetLobbyUpdateTransaction(); | ||
txn.DeleteMetadata(key); | ||
_lobbyManager.UpdateLobby(Id, txn, (Discord.Result result, ref Discord.Lobby lobby) { | ||
if (result == Discord.Result.OK) return; | ||
// TODO(james7132): Implement | ||
}); | ||
} | ||
|
||
public override int GetMetadataCount() => _lobbyManager.LobbyMetadataCount(Id); | ||
|
||
public override void Join() { | ||
_lobbyManager.ConnectLobby(Id, txn, (Discord.Result result, ref Discord.Lobby lobby) { | ||
if (result == Discord.Result.OK) return; | ||
// TODO(james7132): Implement | ||
_lobbyManager.ConnectNetwork(lobby.id); | ||
_lobbyManager.OpenNetworkChannel(lobby.id, (byte)Reliabilty.Reliable, true); | ||
_lobbyManager.OpenNetworkChannel(lobby.id, (byte)Reliabilty.Unreliable, false); | ||
}); | ||
} | ||
|
||
public override void Leave() { | ||
_lobbyManager.DisconectLobby(Id, txn, (Discord.Result result, ref Discord.Lobby lobby) { | ||
if (result == Discord.Result.OK) return; | ||
// TODO(james7132): Implement | ||
}); | ||
} | ||
|
||
public override void Delete() { | ||
_lobbyManager.DeleteLobby(Id, (result) => { | ||
if (result == Discord.Result.OK) { | ||
Dispose(); | ||
return; | ||
} | ||
// TODO(james7132): Implement | ||
}) | ||
} | ||
|
||
public override void SendNetworkMessage(AccountHandle target, byte[] msg, | ||
Reliabilty reliability = Reliabilty.Reliable) { | ||
_lobbyManager.SendNetworkMessage(Id, target.Id, (byte)reliability, msg); | ||
} | ||
|
||
// Event Handlers | ||
|
||
void _OnMemberJoin(ulong lobbyId, ulong userId) { | ||
if (lobbyId != Id) return; | ||
var member = Members.Get(new AccountHandle(userId)); | ||
OnMemberJoin?.Invoke(member); | ||
} | ||
|
||
void _OnMemberLeave(ulong lobbyId, ulong userId) { | ||
if (lobbyId != Id) return; | ||
var member = Members.Get(new AccountHandle(userId)); | ||
Members.Remove(member); | ||
OnMemberLeave?.Invoke(member); | ||
} | ||
|
||
void _OnNetworkMessage(ulong lobbyId, ulong userId, byte _, byte[] message) { | ||
if (lobbyId != Id) return; | ||
var account = new AccountHandle(userId); | ||
Members.Get(account).DispatchNetworkMessage( | ||
OnNetworkMessage?.Invoke(account, message); | ||
} | ||
|
||
} | ||
|
||
} |
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,84 @@ | ||
namespace HouraiTeahouse.Networking.Discord { | ||
|
||
public class DiscordLobbyManager : ILobbyManager { | ||
|
||
internal readonly Discord.LobbyManager _lobbyManager; | ||
internal readonly Discord.ActivtyManager _activityManager; | ||
|
||
public DiscordiIntegrationLobbyManager(DiscordIntegrationClient client) { | ||
_lobbyManager = _client._discordClient.GetLobbyManager(); | ||
_activityManager = _client._discordClient.GetActivityManager(); | ||
} | ||
|
||
public ILobby CreateLobby(LobbyCreateParams createParams) { | ||
var txn = _lobbyManager.GetLobbyCreateTransaction(); | ||
txn.SetCapacity(createParams.Capacity); | ||
txn.SetType((Discord.LobbyType)createParams.Type); | ||
ILobby outputLobby = null; | ||
_lobbyManager.CreateLobby(txn, (result, lobby) => { | ||
outputLobby = new DiscordLobby(this, lobby.id); | ||
|
||
var secret = _lobbyManager.GetLobbyActivitySecret(lobby.id); | ||
|
||
var activity = new Discord.Activity { | ||
Party = { | ||
Id = lobby.id, | ||
Size = { | ||
CurrentSize = 1, | ||
MaxSize = createParams.Capacity | ||
} | ||
}, | ||
Secrets = { Join = secret } | ||
}; | ||
|
||
_activityManager.UpdateActivity(activity, res => {}); | ||
}) | ||
return outputLobby; | ||
} | ||
|
||
public IList<LobbyBase> SearchLobbies(Action<ILobbySearchBuilder> builder) { | ||
var queryBuilder = new LobbySearchBuilder(_lobbyManager); | ||
builder?.Invoke(queryBuilder); | ||
var results = new List<LobbyBase>(); | ||
_lobbyManager.Search(queryBuilder.Build(), (result) => { | ||
if (result == Discord.Result.OK) { | ||
var count = _lobbyManager.LobbyCount(); | ||
if (results.Capacity < count) { | ||
results.Capacity = count; | ||
} | ||
for (var i = 0; i < count; i++) { | ||
var id = _lobbyManager.GetLobbyId(i); | ||
results.Add(new DiscordLobby(this, id)); | ||
} | ||
return; | ||
} | ||
}); | ||
return results; | ||
} | ||
|
||
class LobbySearchBuilder : ILobbySearchBuilder { | ||
|
||
readonly LobbySearchQuery _query; | ||
|
||
public LobbySearchBuilder(Discord.LobbyManager manager) { | ||
_query = manager.GetSearchQuery(); | ||
} | ||
|
||
public ILobbySearchBuilder Filter(string key, SearchComparison comparison, string value) { | ||
_query.Filterkey, (LobbySearchComparison)comparison, | ||
LobbySearchCast.String, value); | ||
return this; | ||
} | ||
|
||
public ILobbySearchBuilder Sort(string key, string value) { | ||
_query.Sort(key, LobbSearchCast.String, value); | ||
} | ||
|
||
public ILobbySearchBuilder Limit(int limit) => _query.Limit(limit); | ||
|
||
public LobbySearchQuery Build() => _query; | ||
} | ||
|
||
} | ||
|
||
} |
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,11 @@ | ||
|
||
namespace HouraiTeahouse.Networking { | ||
|
||
public interface IIntegrationClient { | ||
|
||
IntegrationAccountHandle ActiveUser { get; } | ||
IIntegrationLobbyManager LobbyManager { get; } | ||
|
||
} | ||
|
||
} |
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,74 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace HouraiTeahouse.Networking { | ||
|
||
public interface IMetadataContainer { | ||
|
||
string GetMetadata(string key); | ||
void SetMetadata(string key, string value); | ||
void DeleteMetadata(string key); | ||
|
||
// Returns -1 if the container does not support iteration. | ||
int GetMetadataCount(); | ||
// Returns null if the container does not support iteration. | ||
string GetKeyByIndex(int idx); | ||
|
||
} | ||
|
||
public abstract class LobbyBase : IMetadataContainer { | ||
|
||
public abstract ulong Id { get; } | ||
public abstract LobbyType Type { get; } | ||
public abstract ulong OwnerId { get; } | ||
public abstract uint Capacity { get; } | ||
public abstract bool IsLocked { get; } | ||
|
||
protected abstract int MemberCount { get; } | ||
protected abstract ulong GetMemberId(int idx); | ||
|
||
public LobbyMemberMap Members { get; } | ||
|
||
protected LobbyBase() { | ||
Members = new LobbyPlayerCollection(this); | ||
var count = MemberCount; | ||
for (int i = 0; i < memberCount; i++) { | ||
Members.Get(GetMemberId(i)); | ||
} | ||
} | ||
|
||
public bool IsJoinable => | ||
!lobby.IsLocked && lobby.MemberCount < lobby.Capacity; | ||
|
||
public abstract void Join(); | ||
public abstract void Leave(); | ||
public abstract void Delete(); | ||
|
||
public abstract string GetMetadata(string key); | ||
public abstract void SetMetadata(string key, string value); | ||
public abstract void DeleteMetadata(string key); | ||
|
||
// Returns -1 if the container does not support iteration. | ||
public abstract int GetMetadataCount(); | ||
// Returns null if the container does not support iteration. | ||
public abstract string GetKeyByIndex(int idx); | ||
|
||
} | ||
|
||
public static class IMetadataContainerExtensions { | ||
|
||
public static Dictionary<string, string> GetAllMetadata(this IMetadataContainer container) { | ||
var count = container.GetMetadataCount(); | ||
if (count < 0) { | ||
throw new ArgumentException("Metadata container does not support iteration"); | ||
} | ||
var output = new Dictionary<string, string>() | ||
for (var i = 0; i < count; i++) { | ||
string key = container.GetKeyByIndex(i); | ||
output[key] = container.GetMetadata(key); | ||
} | ||
return output; | ||
} | ||
|
||
} | ||
|
||
} |
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,12 @@ | ||
using System; | ||
|
||
namespace HouraiTeahouse.Networking { | ||
|
||
public interface ILobbyManager { | ||
|
||
ILobby CreateLobby(LobbyCreateParams createParams); | ||
IList<ILobby> SearchLobbies(Action<ILobbySearchBuilder> builder); | ||
|
||
} | ||
|
||
} |
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,20 @@ | ||
namespace HouraiTeahouse.Networking { | ||
|
||
public enum SearchComparison { | ||
LessThanOrEqual = -2, | ||
LessThan = -1, | ||
Equal = 0, | ||
GreaterThan = 1, | ||
GreaterThanOrEqual = 2, | ||
NotEqual = 3, | ||
} | ||
|
||
public interface ILobbySearchBuilder { | ||
|
||
ILobbySearchBuilder Filter(string key, SearchComparison comparison, string value); | ||
ILobbySearchBuilder Sort(string key, string value); | ||
ILobbySearchBuilder Limit(int limit); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.