Added transport interface

This commit is contained in:
Albin Corén 2018-04-24 09:48:32 +02:00
parent 2cee9b0942
commit 6aef964fa4
5 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,15 @@
namespace MLAPI.Data
{
public enum ChannelType
{
Unreliable,
UnreliableFragmented,
UnreliableSequenced,
Reliable,
ReliableFragmented,
ReliableSequenced,
StateUpdate,
ReliableStateUpdate,
AllCostDelivery
}
}

View File

@ -0,0 +1,16 @@
namespace MLAPI.Data
{
public interface IUDPTransport
{
void QueueMessageForSending(uint clientId, ref byte[] dataBuffer, int dataSize, int channelId, bool skipqueue, out byte error);
void SendQueue(uint clientId, out byte error);
NetEventType PollReceive(out uint clientId, out int channelId, ref byte[] data, int bufferSize, out int receivedSize, out byte error);
int Connect(string address, int port, object settings, bool websocket, out byte error);
int RegisterServerListenSocket(object settings, bool websocket);
void Disconnect(uint clientId);
int GetCurrentRTT(uint clientId, out byte error);
int GetRemoteDelayTimeMS(uint clientId, int remoteTimestamp, out byte error);
int GetNetworkTimestamp();
void Shutdown();
}
}

View File

@ -0,0 +1,10 @@
namespace MLAPI.Data
{
public enum NetEventType
{
Data,
Connect,
Disconnect,
Nothing
}
}

View File

@ -0,0 +1,93 @@
using UnityEngine.Networking;
namespace MLAPI.Data
{
public class UnetTransport : IUDPTransport
{
public int Connect(string address, int port, object settings, bool websocket, out byte error)
{
NetworkTransport.Init();
int hostId = NetworkTransport.AddHost((HostTopology)settings);
return NetworkTransport.Connect(hostId, address, port, 0, out error);
}
public void Disconnect(uint clientId)
{
NetId netId = new NetId(clientId);
byte error;
NetworkTransport.Disconnect(netId.HostId, netId.ConnectionId, out error);
}
public int GetCurrentRTT(uint clientId, out byte error)
{
NetId netId = new NetId(clientId);
return NetworkTransport.GetCurrentRTT(netId.HostId, netId.ConnectionId, out error);
}
public int GetNetworkTimestamp()
{
return NetworkTransport.GetNetworkTimestamp();
}
public int GetRemoteDelayTimeMS(uint clientId, int remoteTimestamp, out byte error)
{
NetId netId = new NetId(clientId);
return NetworkTransport.GetRemoteDelayTimeMS(netId.HostId, netId.ConnectionId, remoteTimestamp, out error);
}
public NetEventType PollReceive(out uint clientId, out int channelId, ref byte[] data, int bufferSize, out int receivedSize, out byte error)
{
int hostId;
int connectionId;
byte err;
NetworkEventType eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, data, bufferSize, out receivedSize, out err);
clientId = new NetId((byte)hostId, (ushort)connectionId, false, false).GetClientId();
NetworkError errorType = (NetworkError)err;
if (errorType == NetworkError.Timeout)
eventType = NetworkEventType.DisconnectEvent; //In UNET. Timeouts are not disconnects. We have to translate that here.
error = 0;
//Translate NetworkEventType to NetEventType
switch (eventType)
{
case NetworkEventType.DataEvent:
return NetEventType.Data;
case NetworkEventType.ConnectEvent:
return NetEventType.Connect;
case NetworkEventType.DisconnectEvent:
return NetEventType.Disconnect;
case NetworkEventType.Nothing:
return NetEventType.Nothing;
case NetworkEventType.BroadcastEvent:
return NetEventType.Nothing;
}
return NetEventType.Nothing;
}
public int RegisterServerListenSocket(object settings, bool websockets)
{
NetworkTransport.Init();
return NetworkTransport.AddHost((HostTopology)settings);
}
public void QueueMessageForSending(uint clientId, ref byte[] dataBuffer, int dataSize, int channelId, bool skipqueue, out byte error)
{
NetId netId = new NetId(clientId);
if (skipqueue)
NetworkTransport.Send(netId.HostId, netId.ConnectionId, channelId, dataBuffer, dataSize, out error);
else
NetworkTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, channelId, dataBuffer, dataSize, out error);
}
public void Shutdown()
{
NetworkTransport.Shutdown();
}
public void SendQueue(uint clientId, out byte error)
{
NetId netId = new NetId(clientId);
NetworkTransport.SendQueuedMessages(netId.HostId, netId.ConnectionId, out error);
}
}
}

View File

@ -72,15 +72,19 @@
<Compile Include="Attributes\TargetRpc.cs" />
<Compile Include="Data\Cache.cs" />
<Compile Include="Data\Channel.cs" />
<Compile Include="Data\Transports\ChannelType.cs" />
<Compile Include="Data\FieldType.cs" />
<Compile Include="Attributes\SyncedVar.cs" />
<Compile Include="Data\FixedQueue.cs" />
<Compile Include="Data\Transports\IUDPTransport.cs" />
<Compile Include="Data\Transports\NetEventType.cs" />
<Compile Include="Data\NetworkConfig.cs" />
<Compile Include="Data\NetworkedPrefab.cs" />
<Compile Include="Data\NetworkPool.cs" />
<Compile Include="Data\SyncedVarField.cs" />
<Compile Include="Data\TrackedPointData.cs" />
<Compile Include="Data\TransportHost.cs" />
<Compile Include="Data\Transports\UnetTransport.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="MonoBehaviours\Prototyping\NetworkedAnimator.cs" />
<Compile Include="MonoBehaviours\Prototyping\NetworkedNavMeshAgent.cs" />