コード例 #1
0
ファイル: RelayedTransport.cs プロジェクト: bejita968/MLAPI
        public int Connect(string address, int port, object settings, bool websocket, out byte error)
        {
            NetworkTransport.Init();
            int hostId = RelayTransport.AddHost((HostTopology)settings, false);

            return(RelayTransport.Connect(hostId, address, port, 0, out error));
        }
コード例 #2
0
        public override SocketTasks StartClient()
        {
            var socketTask = SocketTask.Working;

            m_ServerHostId       = RelayTransport.AddHost(new HostTopology(GetConfig(), 1), false);
            m_ServerConnectionId = RelayTransport.Connect(m_ServerHostId, ConnectAddress, ConnectPort, 0, out byte error);

            var connectError = (NetworkError)error;

            switch (connectError)
            {
            case NetworkError.Ok:
                socketTask.Success       = true;
                socketTask.TransportCode = error;
                socketTask.SocketError   = System.Net.Sockets.SocketError.Success;
                socketTask.IsDone        = false;

                // We want to continue to wait for the successful connect
                m_ConnectTask = socketTask;
                break;

            default:
                socketTask.Success       = false;
                socketTask.TransportCode = error;
                socketTask.SocketError   = System.Net.Sockets.SocketError.SocketError;
                socketTask.IsDone        = true;
                break;
            }

            return(socketTask.AsTasks());
        }
コード例 #3
0
        public override void Send(ulong clientId, ArraySegment <byte> data, NetworkChannel networkChannel)
        {
            if (ProfilerEnabled)
            {
                s_TransportProfilerData.Increment(ProfilerConstants.NumberOfTransportSends);
            }

            GetUNetConnectionDetails(clientId, out byte hostId, out ushort connectionId);

            int channelId = 0;

            if (m_ChannelNameToId.TryGetValue(networkChannel, out int value))
            {
                channelId = value;
            }
            else
            {
                channelId = m_ChannelNameToId[NetworkChannel.Internal];
            }

            byte[] buffer;

            if (data.Offset > 0)
            {
                // UNET cant handle this, do a copy

                if (m_MessageBuffer.Length >= data.Count)
                {
                    buffer = m_MessageBuffer;
                }
                else
                {
                    object bufferRef = null;
                    if (m_TemporaryBufferReference != null && ((bufferRef = m_TemporaryBufferReference.Target) != null) && ((byte[])bufferRef).Length >= data.Count)
                    {
                        buffer = (byte[])bufferRef;
                    }
                    else
                    {
                        buffer = new byte[data.Count];
                        m_TemporaryBufferReference = new WeakReference(buffer);
                    }
                }

                Buffer.BlockCopy(data.Array, data.Offset, buffer, 0, data.Count);
            }
            else
            {
                buffer = data.Array;
            }

            if (MessageSendMode == SendMode.Queued)
            {
                RelayTransport.QueueMessageForSending(hostId, connectionId, channelId, buffer, data.Count, out byte error);
            }
            else
            {
                RelayTransport.Send(hostId, connectionId, channelId, buffer, data.Count, out byte error);
            }
        }
コード例 #4
0
        public override NetEventType PollEvent(out ulong clientId, out string channelName, out ArraySegment <byte> payload)
        {
            NetworkEventType eventType = RelayTransport.Receive(out int hostId, out int connectionId, out int channelId, messageBuffer, messageBuffer.Length, out int receivedSize, out byte error);

            clientId = GetMLAPIClientId((byte)hostId, (ushort)connectionId, false);

            NetworkError networkError = (NetworkError)error;

            if (networkError == NetworkError.MessageToLong)
            {
                byte[] tempBuffer;

                if (temporaryBufferReference != null && temporaryBufferReference.IsAlive && ((byte[])temporaryBufferReference.Target).Length >= receivedSize)
                {
                    tempBuffer = (byte[])temporaryBufferReference.Target;
                }
                else
                {
                    tempBuffer = new byte[receivedSize];
                    temporaryBufferReference = new WeakReference(tempBuffer);
                }

                eventType = RelayTransport.Receive(out hostId, out connectionId, out channelId, tempBuffer, tempBuffer.Length, out receivedSize, out error);
                payload   = new ArraySegment <byte>(tempBuffer, 0, receivedSize);
            }
            else
            {
                payload = new ArraySegment <byte>(messageBuffer, 0, receivedSize);
            }

            channelName = channelIdToName[channelId];

            if (networkError == NetworkError.Timeout)
            {
                // In UNET. Timeouts are not disconnects. We have to translate that here.
                eventType = NetworkEventType.DisconnectEvent;
            }

            // 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);
        }
コード例 #5
0
ファイル: UnetTransport.cs プロジェクト: Dzoni94/MLAPI
        public override void DisconnectRemoteClient(ulong clientId)
        {
            UpdateRelay();

            GetUnetConnectionDetails(clientId, out byte hostId, out ushort connectionId);

            RelayTransport.Disconnect((int)hostId, (int)connectionId, out byte error);
        }
コード例 #6
0
ファイル: UnetTransport.cs プロジェクト: Dzoni94/MLAPI
        public override void StartClient()
        {
            UpdateRelay();

            serverHostId = RelayTransport.AddHost(new HostTopology(GetConfig(), 1), false);

            serverConnectionId = RelayTransport.Connect(serverHostId, ConnectAddress, ConnectPort, 0, out byte error);
        }
コード例 #7
0
ファイル: UnetTransport.cs プロジェクト: Dzoni94/MLAPI
        public override void FlushSendQueue(ulong clientId)
        {
            UpdateRelay();

            GetUnetConnectionDetails(clientId, out byte hostId, out ushort connectionId);

            RelayTransport.SendQueuedMessages(hostId, connectionId, out byte error);
        }
コード例 #8
0
ファイル: RelayedTransport.cs プロジェクト: bejita968/MLAPI
        public void DisconnectClient(uint clientId)
        {
            NetId netId = new NetId(clientId);

            if (netId.IsServer())
            {
                return;
            }
            RelayTransport.Disconnect(netId.HostId, netId.ConnectionId, out byte error);
        }
コード例 #9
0
        public void SendQueued(ulong clientId)
        {
            if (ProfilerEnabled)
            {
                s_TransportProfilerData.Increment(ProfilerConstants.NumberOfTransportSendQueues);
            }

            GetUNetConnectionDetails(clientId, out byte hostId, out ushort connectionId);

            RelayTransport.SendQueuedMessages(hostId, connectionId, out byte error);
        }
コード例 #10
0
ファイル: RelayedTransport.cs プロジェクト: bejita968/MLAPI
        public void SendQueue(uint clientId, out byte error)
        {
            NetId netId = new NetId(clientId);

            if (netId.IsServer())
            {
                netId.ConnectionId = (ushort)serverConnectionId;
                netId.HostId       = (byte)serverHostId;
            }
            RelayTransport.SendQueuedMessages(netId.HostId, netId.ConnectionId, out error);
        }
コード例 #11
0
ファイル: UnetTransport.cs プロジェクト: rivFox/MLAPI
        public override void Send(ulong clientId, ArraySegment <byte> data, string channelName)
        {
            GetUnetConnectionDetails(clientId, out byte hostId, out ushort connectionId);

            int channelId = 0;

            if (channelNameToId.ContainsKey(channelName))
            {
                channelId = channelNameToId[channelName];
            }
            else
            {
                channelId = channelNameToId["MLAPI_INTERNAL"];
            }

            byte[] buffer;

            if (data.Offset > 0)
            {
                // UNET cant handle this, do a copy

                if (messageBuffer.Length >= data.Count)
                {
                    buffer = messageBuffer;
                }
                else
                {
                    object bufferRef = null;
                    if (temporaryBufferReference != null && ((bufferRef = temporaryBufferReference.Target) != null) && ((byte[])bufferRef).Length >= data.Count)
                    {
                        buffer = (byte[])bufferRef;
                    }
                    else
                    {
                        buffer = new byte[data.Count];
                        temporaryBufferReference = new WeakReference(buffer);
                    }
                }

                Buffer.BlockCopy(data.Array, data.Offset, buffer, 0, data.Count);
            }
            else
            {
                buffer = data.Array;
            }

            RelayTransport.Send(hostId, connectionId, channelId, buffer, data.Count, out byte error);
        }
コード例 #12
0
ファイル: RelayedTransport.cs プロジェクト: bejita968/MLAPI
        public void RegisterServerListenSocket(object settings)
        {
            HostTopology topology = new HostTopology((ConnectionConfig)settings, NetworkingManager.singleton.NetworkConfig.MaxConnections);

            for (int i = 0; i < ServerTransports.Count; i++)
            {
                if (ServerTransports[i].Websockets)
                {
                    RelayTransport.AddWebsocketHost(topology, ServerTransports[i].Port, true);
                }
                else
                {
                    RelayTransport.AddHost(topology, ServerTransports[i].Port, true);
                }
            }
        }
コード例 #13
0
ファイル: UnetTransport.cs プロジェクト: Dzoni94/MLAPI
        public override void Send(ulong clientId, ArraySegment <byte> data, string channelName, bool skipQueue)
        {
            UpdateRelay();

            GetUnetConnectionDetails(clientId, out byte hostId, out ushort connectionId);

            int channelId = channelNameToId[channelName];

            byte[] buffer;

            if (data.Offset > 0)
            {
                // UNET cant handle this, do a copy

                if (messageBuffer.Length >= data.Count)
                {
                    buffer = messageBuffer;
                }
                else
                {
                    if (temporaryBufferReference != null && temporaryBufferReference.IsAlive && ((byte[])temporaryBufferReference.Target).Length >= data.Count)
                    {
                        buffer = (byte[])temporaryBufferReference.Target;
                    }
                    else
                    {
                        buffer = new byte[data.Count];
                        temporaryBufferReference = new WeakReference(buffer);
                    }
                }

                Buffer.BlockCopy(data.Array, data.Offset, buffer, 0, data.Count);
            }
            else
            {
                buffer = data.Array;
            }

            if (skipQueue)
            {
                RelayTransport.Send(hostId, connectionId, channelId, buffer, data.Count, out byte error);
            }
            else
            {
                RelayTransport.QueueMessageForSending(hostId, connectionId, channelId, buffer, data.Count, out byte error);
            }
        }
コード例 #14
0
ファイル: RelayedTransport.cs プロジェクト: bejita968/MLAPI
        public void QueueMessageForSending(uint clientId, byte[] dataBuffer, int dataSize, int channelId, bool skipqueue, out byte error)
        {
            NetId netId = new NetId(clientId);

            if (netId.IsServer())
            {
                netId.ConnectionId = (ushort)serverConnectionId;
                netId.HostId       = (byte)serverHostId;
            }
            if (skipqueue)
            {
                RelayTransport.Send(netId.HostId, netId.ConnectionId, channelId, dataBuffer, dataSize, out error);
            }
            else
            {
                RelayTransport.QueueMessageForSending(netId.HostId, netId.ConnectionId, channelId, dataBuffer, dataSize, out error);
            }
        }
コード例 #15
0
        public override void StartServer()
        {
            HostTopology topology = new HostTopology(GetConfig(), MaxConnections);

            if (SupportWebsocket)
            {
                if (!UseMLAPIRelay)
                {
                    int websocketHostId = NetworkTransport.AddWebsocketHost(topology, ServerWebsocketListenPort);
                }
                else
                {
                    if (LogHelper.CurrentLogLevel <= LogLevel.Error)
                    {
                        LogHelper.LogError("Cannot create websocket host when using MLAPI relay");
                    }
                }
            }

            int normalHostId = RelayTransport.AddHost(topology, ServerListenPort, true);
        }
コード例 #16
0
        public override SocketTasks StartServer()
        {
            var topology = new HostTopology(GetConfig(), MaxConnections);

            if (SupportWebsocket)
            {
                if (!UseMLAPIRelay)
                {
                    int websocketHostId = UnityEngine.Networking.NetworkTransport.AddWebsocketHost(topology, ServerWebsocketListenPort);
                }
                else
                {
                    if (NetworkLog.CurrentLogLevel <= LogLevel.Error)
                    {
                        NetworkLog.LogError("Cannot create websocket host when using MLAPI relay");
                    }
                }
            }

            int normalHostId = RelayTransport.AddHost(topology, ServerListenPort, true);

            return(SocketTask.Done.AsTasks());
        }
コード例 #17
0
ファイル: UnetTransport.cs プロジェクト: Dzoni94/MLAPI
        public override void DisconnectLocalClient()
        {
            UpdateRelay();

            RelayTransport.Disconnect(serverHostId, serverConnectionId, out byte error);
        }
コード例 #18
0
 public override void DisconnectLocalClient()
 {
     RelayTransport.Disconnect(m_ServerHostId, m_ServerConnectionId, out byte error);
 }
コード例 #19
0
        public override NetworkEvent PollEvent(out ulong clientId, out NetworkChannel networkChannel, out ArraySegment <byte> payload, out float receiveTime)
        {
            var eventType = RelayTransport.Receive(out int hostId, out int connectionId, out int channelId, m_MessageBuffer, m_MessageBuffer.Length, out int receivedSize, out byte error);

            clientId    = GetMLAPIClientId((byte)hostId, (ushort)connectionId, false);
            receiveTime = UnityEngine.Time.realtimeSinceStartup;

            var networkError = (NetworkError)error;

            if (networkError == NetworkError.MessageToLong)
            {
                byte[] tempBuffer;

                if (m_TemporaryBufferReference != null && m_TemporaryBufferReference.IsAlive && ((byte[])m_TemporaryBufferReference.Target).Length >= receivedSize)
                {
                    tempBuffer = (byte[])m_TemporaryBufferReference.Target;
                }
                else
                {
                    tempBuffer = new byte[receivedSize];
                    m_TemporaryBufferReference = new WeakReference(tempBuffer);
                }

                eventType = RelayTransport.Receive(out hostId, out connectionId, out channelId, tempBuffer, tempBuffer.Length, out receivedSize, out error);
                payload   = new ArraySegment <byte>(tempBuffer, 0, receivedSize);
            }
            else
            {
                payload = new ArraySegment <byte>(m_MessageBuffer, 0, receivedSize);
            }

            if (m_ChannelIdToName.TryGetValue(channelId, out NetworkChannel value))
            {
                networkChannel = value;
            }
            else
            {
                networkChannel = NetworkChannel.Internal;
            }

            if (m_ConnectTask != null && hostId == m_ServerHostId && connectionId == m_ServerConnectionId)
            {
                if (eventType == NetworkEventType.ConnectEvent)
                {
                    // We just got a response to our connect request.
                    m_ConnectTask.Message            = null;
                    m_ConnectTask.SocketError        = networkError == NetworkError.Ok ? System.Net.Sockets.SocketError.Success : System.Net.Sockets.SocketError.SocketError;
                    m_ConnectTask.State              = null;
                    m_ConnectTask.Success            = networkError == NetworkError.Ok;
                    m_ConnectTask.TransportCode      = (byte)networkError;
                    m_ConnectTask.TransportException = null;
                    m_ConnectTask.IsDone             = true;

                    m_ConnectTask = null;
                }
                else if (eventType == NetworkEventType.DisconnectEvent)
                {
                    // We just got a response to our connect request.
                    m_ConnectTask.Message            = null;
                    m_ConnectTask.SocketError        = System.Net.Sockets.SocketError.SocketError;
                    m_ConnectTask.State              = null;
                    m_ConnectTask.Success            = false;
                    m_ConnectTask.TransportCode      = (byte)networkError;
                    m_ConnectTask.TransportException = null;
                    m_ConnectTask.IsDone             = true;

                    m_ConnectTask = null;
                }
            }

            if (networkError == NetworkError.Timeout)
            {
                // In UNET. Timeouts are not disconnects. We have to translate that here.
                eventType = NetworkEventType.DisconnectEvent;
            }

            // Translate NetworkEventType to NetEventType
            switch (eventType)
            {
            case NetworkEventType.DataEvent:
                return(NetworkEvent.Data);

            case NetworkEventType.ConnectEvent:
                return(NetworkEvent.Connect);

            case NetworkEventType.DisconnectEvent:
                return(NetworkEvent.Disconnect);

            case NetworkEventType.Nothing:
                return(NetworkEvent.Nothing);

            case NetworkEventType.BroadcastEvent:
                return(NetworkEvent.Nothing);
            }

            return(NetworkEvent.Nothing);
        }
コード例 #20
0
ファイル: RelayedTransport.cs プロジェクト: bejita968/MLAPI
 public void Connect(string address, int port, object settings, out byte error)
 {
     serverHostId       = RelayTransport.AddHost(new HostTopology((ConnectionConfig)settings, 1), false);
     serverConnectionId = RelayTransport.Connect(serverHostId, address, port, 0, out error);
 }
コード例 #21
0
ファイル: RelayedTransport.cs プロジェクト: bejita968/MLAPI
 public void DisconnectFromServer() => RelayTransport.Disconnect(serverHostId, serverConnectionId, out byte error);