This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
|
||||
namespace ShrinkNetwork
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
||||
public sealed class ShrinkNetworkMessageAttribute : Attribute
|
||||
{
|
||||
public int Opcode { get; }
|
||||
public string? Route { get; }
|
||||
|
||||
public ShrinkNetworkMessageAttribute(int opcode, string? route = null)
|
||||
{
|
||||
Opcode = opcode;
|
||||
Route = string.IsNullOrWhiteSpace(route) ? null : route.Trim();
|
||||
}
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
||||
public sealed class ShrinkNetworkSubscriberAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
public enum ShrinkNetworkStateSyncRole
|
||||
{
|
||||
JoinRequest = 0,
|
||||
JoinResponse = 1,
|
||||
Command = 2,
|
||||
StateDelta = 3,
|
||||
LeaveNotice = 4,
|
||||
Heartbeat = 5
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
|
||||
public sealed class ShrinkNetworkStateSyncAttribute : Attribute
|
||||
{
|
||||
public string Group { get; }
|
||||
public ShrinkNetworkStateSyncRole Role { get; }
|
||||
|
||||
public ShrinkNetworkStateSyncAttribute(string group, ShrinkNetworkStateSyncRole role)
|
||||
{
|
||||
Group = string.IsNullOrWhiteSpace(group) ? string.Empty : group.Trim();
|
||||
Role = role;
|
||||
}
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
|
||||
public sealed class ShrinkNetworkSubscribeAttribute : Attribute
|
||||
{
|
||||
public ShrinkNetworkAuthority Authority { get; set; } = ShrinkNetworkAuthority.Any;
|
||||
public string? Permission { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c0a58e8dd0b553646bc53a29187ee423
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace ShrinkNetwork
|
||||
{
|
||||
public interface IShrinkNetworkMessage
|
||||
{
|
||||
}
|
||||
|
||||
public interface IShrinkNetworkRequest : IShrinkNetworkMessage
|
||||
{
|
||||
}
|
||||
|
||||
public interface IShrinkNetworkResponse : IShrinkNetworkMessage
|
||||
{
|
||||
int ErrorCode { get; set; }
|
||||
string ErrorMessage { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2183af3c779211e43933d975814eb435
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,33 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
|
||||
namespace ShrinkNetwork
|
||||
{
|
||||
public static class ShrinkNetworkProtocol
|
||||
{
|
||||
public const int CurrentProtocolVersion = 1;
|
||||
public const int CurrentSchemaVersion = 1;
|
||||
}
|
||||
|
||||
public enum ShrinkNetworkPacketKind
|
||||
{
|
||||
Message = 0,
|
||||
Request = 1,
|
||||
Response = 2
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public sealed class ShrinkNetworkPacket
|
||||
{
|
||||
public int ProtocolVersion = ShrinkNetworkProtocol.CurrentProtocolVersion;
|
||||
public int SchemaVersion = ShrinkNetworkProtocol.CurrentSchemaVersion;
|
||||
public int Opcode;
|
||||
public ShrinkRequestToken RequestToken;
|
||||
public string SessionToken = string.Empty;
|
||||
public long SessionTokenExpiresAtUnixTimeSeconds;
|
||||
public string? Route;
|
||||
public ShrinkNetworkPacketKind Kind;
|
||||
public byte[] Payload = Array.Empty<byte>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d6e511da358c6c64d8c34fdb62928178
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,64 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ShrinkNetwork
|
||||
{
|
||||
public enum ShrinkNetworkPeerKind
|
||||
{
|
||||
Unknown = 0,
|
||||
Client = 1,
|
||||
Server = 2,
|
||||
TrustedServer = 3
|
||||
}
|
||||
|
||||
public enum ShrinkNetworkAuthority
|
||||
{
|
||||
Any = 0,
|
||||
ClientOnly = 1,
|
||||
ServerOnly = 2,
|
||||
TrustedServerOnly = 3
|
||||
}
|
||||
|
||||
public readonly struct ShrinkNetworkPermissionRequirement
|
||||
{
|
||||
public ShrinkNetworkAuthority Authority { get; }
|
||||
public string? Permission { get; }
|
||||
|
||||
public ShrinkNetworkPermissionRequirement(ShrinkNetworkAuthority authority, string? permission)
|
||||
{
|
||||
Authority = authority;
|
||||
Permission = string.IsNullOrWhiteSpace(permission) ? null : permission.Trim();
|
||||
}
|
||||
}
|
||||
|
||||
internal static class ShrinkNetworkPermissionValidator
|
||||
{
|
||||
public static bool IsAllowed(ShrinkNetworkSession session, ShrinkNetworkPermissionRequirement requirement)
|
||||
{
|
||||
if (session == null)
|
||||
return false;
|
||||
|
||||
if (!CheckAuthority(session.PeerKind, requirement.Authority))
|
||||
return false;
|
||||
|
||||
if (!string.IsNullOrEmpty(requirement.Permission) && !session.HasPermission(requirement.Permission))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool CheckAuthority(ShrinkNetworkPeerKind peerKind, ShrinkNetworkAuthority authority)
|
||||
{
|
||||
return authority switch
|
||||
{
|
||||
ShrinkNetworkAuthority.Any => true,
|
||||
ShrinkNetworkAuthority.ClientOnly => peerKind == ShrinkNetworkPeerKind.Client,
|
||||
ShrinkNetworkAuthority.ServerOnly => peerKind == ShrinkNetworkPeerKind.Server || peerKind == ShrinkNetworkPeerKind.TrustedServer,
|
||||
ShrinkNetworkAuthority.TrustedServerOnly => peerKind == ShrinkNetworkPeerKind.TrustedServer,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c6a9dc7f8d56afd41b1e47fac50c5180
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,71 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace ShrinkNetwork
|
||||
{
|
||||
public readonly struct ShrinkRequestToken : IEquatable<ShrinkRequestToken>
|
||||
{
|
||||
public static readonly ShrinkRequestToken Default = new(0);
|
||||
|
||||
public ShrinkRequestToken(int value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public int Value { get; }
|
||||
public bool IsDefault => Value == 0;
|
||||
|
||||
public bool Equals(ShrinkRequestToken other) => Value == other.Value;
|
||||
public override bool Equals(object? obj) => obj is ShrinkRequestToken other && Equals(other);
|
||||
public override int GetHashCode() => Value;
|
||||
public override string ToString() => Value.ToString();
|
||||
|
||||
public static bool operator ==(ShrinkRequestToken left, ShrinkRequestToken right) => left.Equals(right);
|
||||
public static bool operator !=(ShrinkRequestToken left, ShrinkRequestToken right) => !left.Equals(right);
|
||||
public static explicit operator int(ShrinkRequestToken token) => token.Value;
|
||||
public static explicit operator ShrinkRequestToken(int value) => new(value);
|
||||
}
|
||||
|
||||
public sealed class ShrinkRpcCallOptions
|
||||
{
|
||||
public int TimeoutMs { get; set; } = 10000;
|
||||
public string? RouteOverride { get; set; }
|
||||
public ShrinkRequestToken? RequestTokenOverride { get; set; }
|
||||
public string? DebugLabel { get; set; }
|
||||
public CancellationToken CancellationToken { get; set; } = default;
|
||||
}
|
||||
|
||||
public sealed class ShrinkRpcException : Exception
|
||||
{
|
||||
public int ErrorCode { get; }
|
||||
|
||||
public ShrinkRpcException(int errorCode, string message)
|
||||
: base(message)
|
||||
{
|
||||
ErrorCode = errorCode;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ShrinkRpcErrorCode
|
||||
{
|
||||
public const int Unknown = 1;
|
||||
public const int Timeout = 2;
|
||||
public const int Canceled = 3;
|
||||
public const int HandlerException = 4;
|
||||
public const int InvalidResponse = 5;
|
||||
public const int PermissionDenied = 6;
|
||||
public const int ConnectionClosed = 7;
|
||||
public const int ProtocolMismatch = 8;
|
||||
public const int AuthenticationFailed = 9;
|
||||
public const int SessionTokenExpired = 10;
|
||||
}
|
||||
|
||||
public abstract class ShrinkRpcResponseBase : IShrinkNetworkResponse
|
||||
{
|
||||
public int ErrorCode { get; set; }
|
||||
public string ErrorMessage { get; set; } = string.Empty;
|
||||
|
||||
public bool IsSuccess => ErrorCode == 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e430341a390625b4a90e3608f6bbc2f9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,49 @@
|
||||
#nullable enable
|
||||
|
||||
namespace ShrinkNetwork
|
||||
{
|
||||
public enum ShrinkNetworkTransportEventType
|
||||
{
|
||||
Connected = 0,
|
||||
Disconnected = 1,
|
||||
Packet = 2
|
||||
}
|
||||
|
||||
public sealed class ShrinkNetworkTransportEvent
|
||||
{
|
||||
public ShrinkNetworkTransportEventType Type;
|
||||
public long SessionId;
|
||||
public string RemoteAddress = string.Empty;
|
||||
public byte[] PacketData = System.Array.Empty<byte>();
|
||||
|
||||
public static ShrinkNetworkTransportEvent Connected(long sessionId, string remoteAddress)
|
||||
{
|
||||
return new ShrinkNetworkTransportEvent
|
||||
{
|
||||
Type = ShrinkNetworkTransportEventType.Connected,
|
||||
SessionId = sessionId,
|
||||
RemoteAddress = remoteAddress
|
||||
};
|
||||
}
|
||||
|
||||
public static ShrinkNetworkTransportEvent Disconnected(long sessionId, string remoteAddress)
|
||||
{
|
||||
return new ShrinkNetworkTransportEvent
|
||||
{
|
||||
Type = ShrinkNetworkTransportEventType.Disconnected,
|
||||
SessionId = sessionId,
|
||||
RemoteAddress = remoteAddress
|
||||
};
|
||||
}
|
||||
|
||||
public static ShrinkNetworkTransportEvent Packet(long sessionId, byte[] packetData)
|
||||
{
|
||||
return new ShrinkNetworkTransportEvent
|
||||
{
|
||||
Type = ShrinkNetworkTransportEventType.Packet,
|
||||
SessionId = sessionId,
|
||||
PacketData = packetData
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0fbbbfca75a4ebd4c924c2cf328d549d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user