Ejemplo n.º 1
0
    public bool Init(int port = 0, int maxConnections = 16)
    {
        var config = new UnityEngine.Networking.GlobalConfig();

        config.ThreadAwakeTimeout = 1;
        UnityEngine.Networking.NetworkTransport.Init(config);

        m_ReadBuffer = new byte[NetworkConfig.maxPackageSize + 1024];

        m_ConnectionConfig           = new ConnectionConfig();
        m_ConnectionConfig.SendDelay = 0;

        m_ChannelUnreliable = m_ConnectionConfig.AddChannel(QosType.Unreliable);
        m_Topology          = new HostTopology(m_ConnectionConfig, maxConnections);

        if (UnityEngine.Debug.isDebugBuild && m_isNetworkSimuationActive)
        {
            m_HostId = NetworkTransport.AddHostWithSimulator(m_Topology, 1, 300, port);
        }
        else
        {
            m_HostId = NetworkTransport.AddHost(m_Topology, port);
        }

        if (m_HostId != -1 && port != 0)
        {
            GameDebug.Log("Listening on " + string.Join(", ", NetworkUtils.GetLocalInterfaceAddresses()) + " on port " + port);
        }

        return(m_HostId != -1);
    }
Ejemplo n.º 2
0
        public void Start()
        {
            NetworkTransport.Init();

            // create the configuration for the Client > Server connection
            // add 1 reliable channel to ensure that all message will arrive
            // create a topology for the host that allows a maximum of 1 connection.
            var config = new ConnectionConfig();

            _reliableChannel = config.AddChannel(QosType.Reliable);
            var topology = new HostTopology(config, 1);

            // Create a host for sending and recieving messages (simulate this if in unity editor)
#if UNITY_EDITOR
            _mHostId = NetworkTransport.AddHostWithSimulator(topology, 200, 400);
#else
            _mHostId = NetworkTransport.AddHost(topology);
#endif

            byte error;
            _connectionId = NetworkTransport.Connect(_mHostId, HostIp, int.Parse(PortField), 0, out error);
            if (_connectionId != 0) // Could go over total connect count
            {
                _mConnectionId = _connectionId;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NetServer"/> class. We can simulate real world network conditions by using the simMinTimeout/simMaxTimeout params
        /// to simulate connection lag.
        /// </summary>
        /// <param name="maxConnections">Max connections.</param>
        /// <param name="port">Port.</param>
        /// <param name="simMinTimeout">Minimum lag timeout to simulate in ms. Set to zero for none.</param>
        /// <param name="simMaxTimeout">Maximum lag timeout to simulate in ms. Set to zero for none.</param>
        ///
        public NetServer(int maxConnections, int port, int simMinTimeout = 0, int simMaxTimeout = 0)
        {
            if (!NetManager.mIsInitialized)
            {
                Debug.Log("NetServer( ... ) - NetManager was not initialized. Did you forget to call NetManager.Init()?");
                return;
            }

            HostTopology ht = new HostTopology(NetManager.mConnectionConfig, maxConnections);

            if (simMinTimeout != 0 || simMaxTimeout != 0)
            {
                mSocket = NetworkTransport.AddHostWithSimulator(ht, simMinTimeout, simMaxTimeout, port);
            }
            else
            {
                mSocket = NetworkTransport.AddHost(ht, port);
            }

            mPort = port;

            if (!NetUtils.IsSocketValid(mSocket))
            {
                Debug.Log("NetServer::NetServer( " + maxConnections + " , " + port.ToString() + " ) returned an invalid socket ( " + mSocket.ToString() + " )");
            }

            mIsRunning = true;
        }
    protected override void ConfigureHosts(ConnectionConfig config)
    {
        HostTopology topology = new HostTopology(config, 1);

        if (hostID != -1)
        {
            return;
        }
        #if UNITY_EDITOR
        if (simulatedNetworking)
        {
            hostID = NetworkTransport.AddHostWithSimulator(topology, 200, 400);
        }
        else
        {
            hostID = NetworkTransport.AddHost(topology);
        }
#else
        hostID = NetworkTransport.AddHost(topology);
#endif

        byte error;
        // Cannot tell we're connected until we receive the event at later time
        NetworkTransport.Connect(hostID, serverIP, 25000, 0, out error);
    }
Ejemplo n.º 5
0
        internal static int AddSocket(int maxConnections, int simMinTimeout, int simMaxTimeout, int port, bool websocket = false)
        {
            if (!Initialized)
            {
                Log(WakeError.NotInitialized);
            }

            int socket;

            var ht = new HostTopology(_config.ConnectionConfig, maxConnections);

            Log("ChannelConfig : " + JsonUtility.ToJson(ht));
            if (websocket)
            {
                Log(WakeError.NotImplemented);
                return(-1);
            }
            if (simMinTimeout > 0 || simMaxTimeout > 0)
            {
                socket = NetworkTransport.AddHostWithSimulator(ht, simMinTimeout, simMaxTimeout, port);
            }
            else
            {
                socket = NetworkTransport.AddHost(ht, port);
            }

            if (!IsValidSocketToCreate(socket))
            {
                Log(WakeError.InvalidHostCreated);
                return(-1);
            }

            _sockets.Add(socket);
            return(socket);
        }
Ejemplo n.º 6
0
    void Awake()
    {
        self = this;
        Application.runInBackground = true;
        NetworkTransport.Init();
        GlobalConfig gConfig = new GlobalConfig();

        gConfig.MaxPacketSize = 512;
        NetworkTransport.Init(gConfig);

        ConnectionConfig config = new ConnectionConfig();

        reliableCHN   = config.AddChannel(QosType.AllCostDelivery);
        unreliableCHN = config.AddChannel(QosType.StateUpdate);

        HostTopology topology = new HostTopology(config, 10); // max connections

        //socketId = NetworkTransport.AddHost(topology, serverPort);
        socketId = NetworkTransport.AddHostWithSimulator(topology, 30, 200, serverPort);
        Debug.Log("Socket Open. SocketId is: " + socketId);
        recvBuffer = new byte[bufferSize];

        int defaultMaxPlayerCount = 8;

        playerOwned = new List <PlayerOwnedInfo>(defaultMaxPlayerCount);
        //playerStates = new List<int>(defaultMaxPlayerCount);
        //sendBuffers = new List<SerializedBuffer>(defaultMaxPlayerCount * 2);
        //gameObjectsByOwner = new List<List<GameObjectSpawnInfo>>();
        synchronizedComponents = new Dictionary <int, ReplicatedProperties>();

        newConnectionList = new List <int>();
    }
Ejemplo n.º 7
0
 private void SetupHost(int minLatency = -1, int maxLatency = -1)
 {
     if (!this.setup)
     {
         this.eventChannel = this.settings.AddChannel(QosType.ReliableSequenced);
         this.syncChannel  = this.settings.AddChannel(QosType.StateUpdate);
         this.topology     = new HostTopology(this.settings, this.maxConnections);
         if (Networker.mode == NetworkerMode.Server)
         {
             if (minLatency > 0)
             {
                 maxLatency    = maxLatency > 0 ? minLatency : maxLatency;
                 this.socketID = NetworkTransport.AddHostWithSimulator(topology, minLatency, maxLatency, this.hostPort);
                 return;
             }
             this.socketID = NetworkTransport.AddHost(topology, this.hostPort);
         }
                         #if !UNITY_WEBGL
         else
         {
             this.socketID = NetworkTransport.AddHost(topology);
         }
                         #endif
         this.setup = true;
     }
 }
Ejemplo n.º 8
0
    public int AddHostWithSimulator(HostTopology topology, int min_timeout, int max_timeout, ref IP2PAddress address)
    {
        P2PAddressUnet p2PAddressUnet = address as P2PAddressUnet;

        if (p2PAddressUnet != null)
        {
            return(NetworkTransport.AddHostWithSimulator(topology, min_timeout, max_timeout, p2PAddressUnet.m_Port));
        }
        Debug.LogError(string.Format("Using invalid IP2PAddress type {0}", address.GetType()));
        return(-1);
    }
Ejemplo n.º 9
0
        public static int AddHostWithSimulator(HostTopology topology, int minTimeout, int maxTimeout, int port, string ip, bool createServer, string relayAddress, int relayPort)
        {
            isClient = !createServer;
            SetChannelsFromTopology(topology);
            int ret = NetworkTransport.AddHostWithSimulator(topology, minTimeout, maxTimeout, port, ip);

            if (createServer)
            {
                relayConnectionId = NetworkTransport.Connect(ret, relayAddress, relayPort, 0, out byte b);
            }
            return(ret);
        }
Ejemplo n.º 10
0
        public void Start()
        {
            NetworkTransport.Init();

            var config = new ConnectionConfig();

            _reliableChannel = config.AddChannel(QosType.Reliable);
            var topology = new HostTopology(config, 1); // Only connect once

#if UNITY_EDITOR
            _mHostId = NetworkTransport.AddHostWithSimulator(topology, 200, 400);
#else
            _mHostId = NetworkTransport.AddHost(topology);
#endif
        }
Ejemplo n.º 11
0
        // Use this for initialization
        void Start()
        {
            NetworkTransport.Init();

            ConnectionConfig config = new ConnectionConfig();

            _reliableChannel   = config.AddChannel(QosType.Reliable);
            _unreliableChannel = config.AddChannel(QosType.Unreliable);

            HostTopology topology = new HostTopology(config, 1);

#if UNITY_EDITOR
            _hostId = NetworkTransport.AddHostWithSimulator(topology, MinSimulatedPing, MaxSimulatedPing);
#else
            _hostId = NetworkTransport.AddHost(topology);
#endif
        }
Ejemplo n.º 12
0
        public void StartServer()
        {
            ConnectionConfig config = new ConnectionConfig();

            _reliableChannel   = config.AddChannel(QosType.Reliable);
            _unreliableChannel = config.AddChannel(QosType.Unreliable);

            HostTopology topology = new HostTopology(config, MaxConnections);

#if UNITY_EDITOR
            _serverHostId = NetworkTransport.AddHostWithSimulator(topology, MinSimulatedPing, MaxSimulatedPing, Port);
            Debug.Log("SceneServer started (editor)");
#else
            _serverHostId = NetworkTransport.AddHost(topology, Port);
            Debug.Log("SceneServer started");
#endif
        }
Ejemplo n.º 13
0
        public static int AddHostWithSimulator(HostTopology topology, int minTimeout, int maxTimeout, int port, bool createServer)
        {
            if (!Enabled)
            {
                return(NetworkTransport.AddHostWithSimulator(topology, minTimeout, maxTimeout, port));
            }

            isClient = !createServer;
            SetChannelsFromTopology(topology);
            int  ret = NetworkTransport.AddHostWithSimulator(topology, minTimeout, maxTimeout, port);
            byte error;

            if (createServer)
            {
                relayConnectionId = NetworkTransport.Connect(ret, RelayAddress, RelayPort, 0, out error);
            }
            return(ret);
        }
Ejemplo n.º 14
0
    void Start()
    {
        Application.runInBackground = true;
        NetworkTransport.Init();

        ConnectionConfig config = new ConnectionConfig();

        reliableChannel = config.AddChannel(QosType.Reliable);
        HostTopology topology = new HostTopology(config, 5);

#if UNITY_EDITOR
        // Listen on port 25000
        m_hostId = NetworkTransport.AddHostWithSimulator(topology, 200, 400, 25000);
#else
        m_hostId = NetworkTransport.AddHost(topology, 25000);
#endif

        // Send client position data every so often to those connected
        StartCoroutine(SendPositionCoroutine());
    }
Ejemplo n.º 15
0
    void Start()
    {
        Application.runInBackground = true;
        NetworkTransport.Init();

        ConnectionConfig config = new ConnectionConfig();

        config.AddChannel(QosType.Reliable);
        HostTopology topology = new HostTopology(config, 1); // Just need 1 connection

#if UNITY_EDITOR
        m_hostId = NetworkTransport.AddHostWithSimulator(topology, 200, 400);
#else
        m_hostId = NetworkTransport.AddHost(topology);
#endif

        byte error;
        // Cannot tell we're connected until we receive the event at later time
        NetworkTransport.Connect(m_hostId, System.Net.IPAddress.Loopback.ToString(), 25000, 0, out error);
    }
Ejemplo n.º 16
0
        private void InitializeServer(HostTopology topology)
        {
            if (m_Settings.m_SimulateNetworking)
            {
                m_HostID = NetworkTransport.AddHostWithSimulator(topology, m_Settings.m_MinLatency, m_Settings.m_MaxLatency, m_Settings.m_Port);
            }
            else
            {
                m_HostID = NetworkTransport.AddHost(topology, m_Settings.m_Port);
            }

            IsConnected = true;
            IsServer    = true;

            ServerStarted?.Invoke();

            RegisterHandlers();
            LoadOnlineScene();

            DebugLog("Server started...");
        }
Ejemplo n.º 17
0
        public override void Connect(string ipAddress, int serverPort)
        {
            byte num;

            if (this._isDelaySimulator)
            {
                this._hostID     = NetworkTransport.AddHostWithSimulator(this._ht, 0x3e8, 0x5dc);
                this._selfConnID = NetworkTransport.ConnectWithSimulator(this._hostID, ipAddress, serverPort, 0, out num, this._simulatorConfig);
            }
            else
            {
                this._hostID     = NetworkTransport.AddHost(this._ht);
                this._selfConnID = NetworkTransport.Connect(this._hostID, ipAddress, serverPort, 0, out num);
            }
            if (num != 0)
            {
                throw new UnityException("bad connection : " + ((NetworkError)num));
            }
            this._state = MPPeer.PeerState.ClientConnecting;
            this._clientConnectState = ClientConnectState.Idle;
        }
Ejemplo n.º 18
0
        public static int AddHostWithSimulator(HostTopology topology, int minTimeout, int maxTimeout, bool createServer)
        {
            if (!Enabled)
            {
                return(NetworkTransport.AddHostWithSimulator(topology, minTimeout, maxTimeout));
            }

            isClient = !createServer;

            defaultChannelId = topology.DefaultConfig.AddChannel(QosType.ReliableSequenced);

            SetChannelsFromTopology(topology);

            int ret = NetworkTransport.AddHostWithSimulator(topology, minTimeout, maxTimeout);

            if (createServer)
            {
                relayConnectionId = NetworkTransport.Connect(ret, RelayAddress, RelayPort, 0, out byte b);
            }

            return(ret);
        }
Ejemplo n.º 19
0
        public void StartHost(int desiredPort, int maxConnections)
        {
            if (hostID != -1)
            {
                Debug.LogWarning("hostID is not -1");
                return;
            }

            NetworkTransport.Init();

            port = desiredPort;
            if (port <= 0)
            {
                port = GetFreeTcpPort();
            }

            NetworkTransport.Init();

            ConnectionConfig config = new ConnectionConfig();

            foreach (var channelInfo in channelNameToInfoDict.Values)
            {
                channelInfo.id = config.AddChannel(channelInfo.type);
            }
            HostTopology topology = new HostTopology(config, maxConnections);

            bool simulateLatency = false;

            if (simulateLatency)
            {
                hostID = NetworkTransport.AddHostWithSimulator(topology, 50, 60, port);
            }
            else
            {
                hostID = NetworkTransport.AddHost(topology, port);
            }

            hostIDDict.Add(hostID, this);
        }
Ejemplo n.º 20
0
        private void InitializeClient(string serverIP, HostTopology topology)
        {
            if (m_Settings.m_SimulateNetworking)
            {
                m_HostID = NetworkTransport.AddHostWithSimulator(topology, m_Settings.m_MinLatency, m_Settings.m_MaxLatency, 0);
            }
            else
            {
                m_HostID = NetworkTransport.AddHost(topology, 0);
            }

            byte error;

            if (m_Settings.m_SimulateNetworking)
            {
                int min = m_Settings.m_MinLatency;

                int avg = (m_Settings.m_MinLatency + m_Settings.m_MaxLatency) / 2;

                ConnectionSimulatorConfig conf = new ConnectionSimulatorConfig(min, avg, min, avg, m_Settings.m_PacketLoss);

                ConnectionID = NetworkTransport.ConnectWithSimulator(m_HostID, serverIP, m_Settings.m_Port, 0, out error, conf);
            }
            else
            {
                ConnectionID = NetworkTransport.Connect(m_HostID, serverIP, m_Settings.m_Port, 0, out error);
            }

            DebugLog(string.Format("Connecting to {0}...", serverIP));

            if ((NetworkError)error != NetworkError.Ok)
            {
                DebugLog("Connection failed: " + (NetworkError)error);
                return;
            }

            RegisterHandlers();
        }
Ejemplo n.º 21
0
    void Start()
    {
        Application.runInBackground = true;
        NetworkTransport.Init();

        ConnectionConfig config = new ConnectionConfig();

        reliableChannel = config.AddChannel(QosType.Reliable);
        HostTopology topology = new HostTopology(config, 5);

#if UNITY_EDITOR
        m_hostId = NetworkTransport.AddHostWithSimulator(topology, 200, 400, 25000);
#else
        m_hostId = NetworkTransport.AddHost(topology, 25000);
#endif

        Physics.gravity = new Vector3(); // No gravity

        // Spawn a sphere every so often
        StartCoroutine(SpawnRandomCoroutine());
        // Send sphere data every so often to those connected
        StartCoroutine(SendCoroutine());
    }
    protected override void ConfigureHosts(ConnectionConfig config)
    {
        HostTopology topology = new HostTopology(config, 5);

        if (hostID != -1)
        {
            return;
        }
#if UNITY_EDITOR
        // Listen on port 25000
        if (simulatedNetworking)
        {
            hostID = NetworkTransport.AddHost(topology, 25000);
        }
        else
        {
            hostID = NetworkTransport.AddHostWithSimulator(topology, 200, 400, 25000);
        }
#else
        hostID = NetworkTransport.AddHost(topology, 25000);
#endif

        Debug.Log(Format.localIPAddress());
    }
Ejemplo n.º 23
0
 public int AddHostWithSimulator(HostTopology topology, int minTimeout, int maxTimeout, int port)
 {
     return(NetworkTransport.AddHostWithSimulator(topology, minTimeout, maxTimeout, port));
 }