public static void Disconnect()
        {
            initRetryCounter = 0;

            if (state == ConnectionState.Init)
            {
                return;
            }

#if UNITY_WEBGL
            WebSocketDisconnect();
#else
            if (hSocketListen.handle != Baselib_Socket_Handle_Invalid.handle)
            {
                Baselib_Socket_Close(hSocketListen);
                hSocketListen = Baselib_Socket_Handle_Invalid;
            }

            if (hSocket.handle != Baselib_Socket_Handle_Invalid.handle)
            {
                Baselib_Socket_Close(hSocket);
                hSocket = Baselib_Socket_Handle_Invalid;
            }

            errState.code = Baselib_ErrorCode.Success;
#endif

            state = ConnectionState.Init;

            MessageStreamManager.RecycleAll();
            receiveStream.RecycleStreamAndFreeExtra();
        }
Ejemplo n.º 2
0
        internal unsafe void SpoofInitListenInternal()
        {
            spoofInitCalled = true;
            Baselib_ErrorState errState = new Baselib_ErrorState();

            hSpoofSocket = Baselib_Socket_Create(Baselib_NetworkAddress_Family.IPv4, Baselib_Socket_Protocol.TCP, (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState));

            if (errState.code != Baselib_ErrorCode.Success)
            {
                SpoofExitInternal();
                Assert.AreEqual(Baselib_ErrorCode.Success, errState.code, "Unable to create socket when initializing spoof player listener server");
            }

            if (errState.code == Baselib_ErrorCode.Success)
            {
                fixed(byte *bip = System.Text.Encoding.UTF8.GetBytes(spoofIp))
                {
                    Baselib_NetworkAddress_Encode((Baselib_NetworkAddress *)UnsafeUtility.AddressOf(ref hSpoofAddress), Baselib_NetworkAddress_Family.IPv4,
                                                  bip, spoofPort, (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState));
                }
            }

            if (errState.code != Baselib_ErrorCode.Success)
            {
                SpoofExitInternal();
                Assert.AreEqual(Baselib_ErrorCode.Success, errState.code, "Unable to encode network address when initializing spoof player listener server");
            }

            if (errState.code == Baselib_ErrorCode.Success)
            {
                Baselib_Socket_Bind(hSpoofSocket, (Baselib_NetworkAddress *)UnsafeUtility.AddressOf(ref hSpoofAddress), Baselib_NetworkAddress_AddressReuse.Allow,
                                    (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState));
            }

            if (errState.code != Baselib_ErrorCode.Success)
            {
                SpoofExitInternal();
                Assert.AreEqual(Baselib_ErrorCode.Success, errState.code, "Unable to bind socket when initializing spoof player listener server");
            }

            if (errState.code == Baselib_ErrorCode.Success)
            {
                Baselib_Socket_TCP_Listen(hSpoofSocket, (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState));
            }

            if (errState.code != Baselib_ErrorCode.Success)
            {
                SpoofExitInternal();
                Assert.AreEqual(Baselib_ErrorCode.Success, errState.code, "Unable to initiate listen mode when initializing spoof player listener server");
            }
        }
Ejemplo n.º 3
0
        internal unsafe void SpoofAcceptInternal()
        {
            Baselib_ErrorState errState = new Baselib_ErrorState();

            hSpoofSocketConnected = Baselib_Socket_Handle_Invalid;
            var start = Time.timeAsDouble;

            while (hSpoofSocketConnected.handle == Baselib_Socket_Handle_Invalid.handle && Time.timeAsDouble - start < kConnectTime)
            {
                hSpoofSocketConnected = Baselib_Socket_TCP_Accept(hSpoofSocket, (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState));
            }
            Assert.AreEqual(Baselib_ErrorCode.Success, errState.code, "Error accepting spoof player connection");
            Assert.IsFalse(hSpoofSocketConnected.handle == Baselib_Socket_Handle_Invalid.handle, "Unable to accept spoof player connection");
        }
        public static void Shutdown()
        {
            if (!m_initialized)
            {
                return;
            }

            if (m_hSocket.handle != Baselib_Socket_Handle_Invalid.handle)
            {
                Baselib_Socket_Close(m_hSocket);
                m_hSocket = Baselib_Socket_Handle_Invalid;
            }

            m_errState.code           = Baselib_ErrorCode.Success;
            m_initialized             = false;
            m_WebBroadcastInitialized = false;
        }
Ejemplo n.º 5
0
        internal void SpoofExitInternal()
        {
            if (!spoofInitCalled)
            {
                return;
            }

            spoofInitCalled = false;

            if (hSpoofSocket.handle != Baselib_Socket_Handle_Invalid.handle)
            {
                Baselib_Socket_Close(hSpoofSocket);
                hSpoofSocket = Baselib_Socket_Handle_Invalid;
            }

            if (hSpoofSocketConnected.handle != Baselib_Socket_Handle_Invalid.handle)
            {
                Baselib_Socket_Close(hSpoofSocketConnected);
                hSpoofSocketConnected = Baselib_Socket_Handle_Invalid;
            }
        }
        public static void Initialize(bool directConnect, ushort listenPort, uint editorGuid32, string gameName)
        {
            if (m_WebBroadcastInitialized)
            {
                Shutdown();
            }

            if (m_initialized)
            {
                return;
            }

            unsafe
            {
                m_hSocket = Baselib_Socket_Create(Baselib_NetworkAddress_Family.IPv4, Baselib_Socket_Protocol.UDP, (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref m_errState));
                if (m_errState.code == Baselib_ErrorCode.Success)
                {
                    fixed(byte *bip = UTF8.GetBytes(m_broadcastIp))
                    {
                        Baselib_NetworkAddress_Encode((Baselib_NetworkAddress *)UnsafeUtility.AddressOf(ref m_hAddress), Baselib_NetworkAddress_Family.IPv4,
                                                      bip, m_broadcastPort, (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref m_errState));
                    }
                }
            }

            if (m_errState.code != Baselib_ErrorCode.Success)
            {
                if (m_hSocket.handle != Baselib_Socket_Handle_Invalid.handle)
                {
                    Baselib_Socket_Close(m_hSocket);
                    m_hSocket = Baselib_Socket_Handle_Invalid;
                }
                return;
            }

            CreateWhoAmI(directConnect, listenPort, editorGuid32, gameName);

            m_initialized = true;
        }
        public static void Connect()
        {
            if (ConnectionInitialized)
            {
                return;
            }

#if UNITY_WEBGL
            if (state == ConnectionState.Init)
            {
                if (initRetryCounter > 0)
                {
                    initRetryCounter--;
                    return;
                }

                unsafe
                {
                    fixed(byte *bip = UTF8.GetBytes(initIp + $":{initPort}"))
                    {
                        WebSocketConnect(bip);
                    }
                }

                state = ConnectionState.Ready;
            }
#else
            if (state == ConnectionState.Init)
            {
                if (initRetryCounter > 0)
                {
                    initRetryCounter--;
                    return;
                }

                unsafe
                {
                    hSocket = Baselib_Socket_Create(Baselib_NetworkAddress_Family.IPv4, Baselib_Socket_Protocol.TCP, (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState));
                    if (errState.code == Baselib_ErrorCode.Success)
                    {
                        fixed(byte *bip = UTF8.GetBytes(initIp))
                        {
                            Baselib_NetworkAddress_Encode((Baselib_NetworkAddress *)UnsafeUtility.AddressOf(ref hAddress), Baselib_NetworkAddress_Family.IPv4,
                                                          bip, initPort, (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState));
                        }
                    }
                }

                if (errState.code == Baselib_ErrorCode.Success)
                {
                    state = initType;
                }
                else
                {
                    state = ConnectionState.Invalid;
                }
            }

            if (state == ConnectionState.ConnectDirect)
            {
                unsafe
                {
                    Baselib_Socket_TCP_Connect(hSocket, (Baselib_NetworkAddress *)UnsafeUtility.AddressOf(ref hAddress), Baselib_NetworkAddress_AddressReuse.Allow,
                                               (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState));
                }

                if (errState.code == Baselib_ErrorCode.Success)
                {
                    state = ConnectionState.Ready;
                }
                else
                {
                    state = ConnectionState.Invalid;
                }
            }
            else
            {
                if (state == ConnectionState.ConnectListenBind)
                {
                    unsafe
                    {
                        Baselib_Socket_Bind(hSocket, (Baselib_NetworkAddress *)UnsafeUtility.AddressOf(ref hAddress), Baselib_NetworkAddress_AddressReuse.Allow,
                                            (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState));
                    }

                    if (errState.code == Baselib_ErrorCode.Success)
                    {
                        state = ConnectionState.ConnectListenListen;
                    }
                    else
                    {
                        state = ConnectionState.Invalid;
                    }
                }

                if (state == ConnectionState.ConnectListenListen)
                {
                    unsafe
                    {
                        Baselib_Socket_TCP_Listen(hSocket, (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState));
                    }

                    if (errState.code == Baselib_ErrorCode.Success)
                    {
                        state = ConnectionState.ConnectListenAccept;
                    }
                    else
                    {
                        state = ConnectionState.Invalid;
                    }
                }

                if (state == ConnectionState.ConnectListenAccept)
                {
                    unsafe
                    {
                        hSocketListen = Baselib_Socket_TCP_Accept(hSocket, (Baselib_ErrorState *)UnsafeUtility.AddressOf(ref errState));
                    }

                    if (errState.code != Baselib_ErrorCode.Success)
                    {
                        state = ConnectionState.Invalid;
                    }
                    else if (hSocketListen.handle != Baselib_Socket_Handle_Invalid.handle)
                    {
                        // Swap so rx/tx code works on same path
                        var hSocketTemp = hSocket;
                        hSocket       = hSocketListen;
                        hSocketListen = hSocketTemp;
                        state         = ConnectionState.Ready;
                    }
                }
            }

            if (state == ConnectionState.Invalid)
            {
                if (hSocket.handle != Baselib_Socket_Handle_Invalid.handle)
                {
                    Baselib_Socket_Close(hSocket);
                    hSocket = Baselib_Socket_Handle_Invalid;
                }
            }
#endif
        }
 public static extern void Baselib_Socket_Close(Baselib_Socket_Handle socket);
 public static extern void Baselib_Socket_TCP_Connect(Baselib_Socket_Handle socket, Baselib_NetworkAddress *address, Baselib_NetworkAddress_AddressReuse addressReuse, Baselib_ErrorState *errorState);
 public static extern UInt32 Baselib_Socket_TCP_Recv(Baselib_Socket_Handle socket, IntPtr data, UInt32 dataLen, Baselib_ErrorState *errorState);
 public static extern UInt32 Baselib_Socket_UDP_Recv(Baselib_Socket_Handle socket, Baselib_Socket_Message *messages, UInt32 messagesCount, Baselib_ErrorState *errorState);
 public static extern Baselib_Socket_Handle Baselib_Socket_TCP_Accept(Baselib_Socket_Handle socket, Baselib_ErrorState *errorState);
 public static extern void Baselib_Socket_TCP_Listen(Baselib_Socket_Handle socket, Baselib_ErrorState *errorState);
 public static extern void Baselib_Socket_GetAddress(Baselib_Socket_Handle socket, Baselib_NetworkAddress *address, Baselib_ErrorState *errorState);