using System; using Unity.Networking.Transport.Protocols; using Unity.Collections; using Unity.Jobs; using Unity.Burst; using Unity.Collections.LowLevel.Unsafe; using System.Runtime.InteropServices; namespace Unity.Networking.Transport { /// /// The NetworkPacketReceiver is an interface for handling received packets, needed by the /// public struct NetworkPacketReceiver { public int ReceiveCount { get {return m_Driver.ReceiveCount;} set{m_Driver.ReceiveCount = value;} } /// /// AppendPacket is where we parse the data from the network into easy to handle events. /// /// The address of the endpoint we received data from. /// The header data indicating what type of packet it is. for more information. /// The size of the payload, if any. /// public int AppendPacket(NetworkInterfaceEndPoint address, int dataLen) { return m_Driver.AppendPacket(address, dataLen); } /// /// Get the datastream associated with this Receiver. /// /// Returns a NativeList of bytes public NativeList GetDataStream() { return m_Driver.GetDataStream(); } public int GetDataStreamSize() { return m_Driver.GetDataStreamSize(); } /// /// Check if the DataStreamWriter uses dynamic allocations to automatically resize the buffers or not. /// /// True if its dynamically resizing the DataStreamWriter public bool DynamicDataStreamSize() { return m_Driver.DynamicDataStreamSize(); } /// /// Check if an address is currently assosiated with a valid connection. /// This is mostly useful to keep interface internal lists of connections in sync with the correct state. /// public bool IsAddressUsed(NetworkInterfaceEndPoint address) { return m_Driver.IsAddressUsed(address); } public long LastUpdateTime => m_Driver.LastUpdateTime; public int ReceiveErrorCode { set{m_Driver.ReceiveErrorCode = value;} } internal NetworkDriver m_Driver; } [Flags] public enum SendHandleFlags { AllocatedByDriver = 1 << 0 } public struct NetworkInterfaceSendHandle { public IntPtr data; public int capacity; public int size; public int id; public SendHandleFlags flags; } public struct NetworkSendQueueHandle { private IntPtr handle; internal static unsafe NetworkSendQueueHandle ToTempHandle(NativeQueue.ParallelWriter sendQueue) { void* ptr = UnsafeUtility.Malloc(UnsafeUtility.SizeOf.ParallelWriter>(), UnsafeUtility.AlignOf.ParallelWriter>(), Allocator.Temp); UnsafeUtility.WriteArrayElement(ptr, 0, sendQueue); return new NetworkSendQueueHandle { handle = (IntPtr)ptr }; } public unsafe NativeQueue.ParallelWriter FromHandle() { void* ptr = (void*)handle; return UnsafeUtility.ReadArrayElement.ParallelWriter>(ptr, 0); } } public struct NetworkSendInterface { [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int BeginSendMessageDelegate(out NetworkInterfaceSendHandle handle, IntPtr userData, int requiredPayloadSize); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int EndSendMessageDelegate(ref NetworkInterfaceSendHandle handle, ref NetworkInterfaceEndPoint address, IntPtr userData, ref NetworkSendQueueHandle sendQueue); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void AbortSendMessageDelegate(ref NetworkInterfaceSendHandle handle, IntPtr userData); public TransportFunctionPointer BeginSendMessage; public TransportFunctionPointer EndSendMessage; public TransportFunctionPointer AbortSendMessage; [NativeDisableUnsafePtrRestriction] public IntPtr UserData; } public interface INetworkInterface : IDisposable { NetworkInterfaceEndPoint LocalEndPoint { get; } int Initialize(params INetworkParameter[] param); /// /// Schedule a ReceiveJob. This is used to read data from your supported medium and pass it to the AppendData function /// supplied by /// /// A used to parse the data received. /// A to any dependency we might have. /// A to our newly created ScheduleReceive Job. JobHandle ScheduleReceive(NetworkPacketReceiver receiver, JobHandle dep); /// /// Schedule a SendJob. This is used to flush send queues to your supported medium /// /// The send queue which can be used to emulate parallel send. /// A to any dependency we might have. /// A to our newly created ScheduleSend Job. JobHandle ScheduleSend(NativeQueue sendQueue, JobHandle dep); /// /// Binds the medium to a specific endpoint. /// /// /// A valid . /// /// 0 on Success int Bind(NetworkInterfaceEndPoint endpoint); /// /// Start listening for incoming connections. This is normally a no-op for real UDP sockets. /// /// 0 on Success int Listen(); NetworkSendInterface CreateSendInterface(); int CreateInterfaceEndPoint(NetworkEndPoint address, out NetworkInterfaceEndPoint endpoint); NetworkEndPoint GetGenericEndPoint(NetworkInterfaceEndPoint endpoint); } }