コード例 #1
0
        /// <summary>
        /// 这个可以调用的messageReceived事件调用的包装器
        /// A wrapper for the messageReceived event call that chindren of this can call
        /// </summary>
        protected void OnBinaryMessageReceived(NetworkingPlayer player, Binary frame)
        {
            byte routerId = frame.RouterId;

            if (routerId == RouterIds.RPC_ROUTER_ID || routerId == RouterIds.BINARY_DATA_ROUTER_ID || routerId == RouterIds.CREATED_OBJECT_ROUTER_ID)
            {
                uint          id           = frame.StreamData.GetBasicType <uint>();
                NetworkObject targetObject = null;

                lock (NetworkObjectDict)
                {
                    NetworkObjectDict.TryGetValue(id, out targetObject);
                }

                if (targetObject == null)
                {
                    // 收到该网络对象的消息包
                    // 但是该玩家机器上还没有创建网络对象
                    // 就将该消息缓存器来,等待网络对象创建完再掉用
                    lock (missingObjectBuffer)
                    {
                        if (!missingObjectBuffer.ContainsKey(id))
                        {
                            missingObjectBuffer.Add(id, new List <Action <NetworkObject> >());
                        }

                        missingObjectBuffer[id].Add((networkObject) =>
                        {
                            ExecuteRouterAction(routerId, networkObject, (Binary)frame, player);
                        });
                    }

                    // TODO:  If the server is missing an object, it should have a timed buffer
                    // that way useless messages are not setting around in memory
                    // TODO:如果服务器缺少一个对象,它应该有一个定时缓冲区
                    //这种无用的消息不会在内存中设置

                    return;
                }

                ExecuteRouterAction(routerId, targetObject, (Binary)frame, player);
            }
            // 创建网络对象
            else if (routerId == RouterIds.NETWORK_OBJECT_ROUTER_ID)
            {
                CreateNetworkObject(player, (Binary)frame);
            }
            // 在服务器接受客户端时,将服务器现有的所有网络对象 发给该玩家,让他创建
            else if (routerId == RouterIds.ACCEPT_MULTI_ROUTER_ID)
            {
                CreateMultiNetworkObject(player, (Binary)frame);
            }
        }
コード例 #2
0
        /// <summary>
        /// 网络对象注册完成
        /// </summary>
        /// <param name="networkObject">网络对象</param>
        public void CompleteInitialization(NetworkObject networkObject)
        {
            lock (NetworkObjectDict)
            {
                if (NetworkObjectDict.ContainsKey(networkObject.NetworkId))
                {
                    return;
                }

                NetworkObjectDict.Add(networkObject.NetworkId, networkObject);
                NetworkObjectList.Add(networkObject);
            }
        }
コード例 #3
0
        /// <summary>
        ///注册一个网络对象与这个网络, 如果forceId是0,会分配一个ID
        /// </ summary>
        /// <param name =“networkObject”>要使用此联网器注册的对象</ param>
        /// <returns> <c> true </ c>如果对象已经成功注册,否则<c> false </ c>已注册</ returns>
        public bool RegisterNetworkObject(NetworkObject networkObject, uint forceId = 0)
        {
            uint id = currentNetworkObjectId;

            lock (NetworkObjectDict)
            {
                // If we are forcing this object
                if (forceId != 0)
                {
                    if (NetworkObjectDict.ContainsKey(forceId))
                    {
                        return(false);
                    }

                    id = forceId;

                    if (!networkObject.RegisterOnce(id))
                    {
                        throw new BaseNetworkException("The supplied network object has already been assigned to a networker and has an id");
                    }

                    //AddNetworkObject(forceId, networkObject);
                    //NetworkObjectList.Add(networkObject);
                }
                else
                {
                    do
                    {
                        if (NetworkObjectDict.ContainsKey(++currentNetworkObjectId))
                        {
                            continue;
                        }

                        if (!networkObject.RegisterOnce(currentNetworkObjectId))
                        {
                            //返回,因为下次调用这个方法会在检查之前增加
                            // Backtrack since the next call to this method will increment before checking
                            currentNetworkObjectId--;

                            throw new BaseNetworkException("The supplied network object has already been assigned to a networker and has an id");
                        }

                        //AddNetworkObject(currentNetworkObjectId, networkObject);
                        //NetworkObjectList.Add(networkObject);
                        break;
                    } while (IsBound);
                }
            }

            //将网络ID分配给网络对象
            // Assign the network id to the network object
            networkObject.RegisterOnce(id);

            // When this object is destroyed it needs to remove itself from the list
            networkObject.onDestroy += (RoomScene sender) =>
            {
                lock (NetworkObjectDict)
                {
                    NetworkObjectDict.Remove(networkObject.NetworkId);
                    NetworkObjectList.Remove(networkObject);
                }
            };

            return(true);
        }