Beispiel #1
0
    private void Awake()
    {
        Framework.Debug.SetLogger(new Logger());
        GameFramework.Register(new NetworkManager());

        channel = new TcpNetworkChannel("testTCP", this);
    }
        /// <summary>
        /// 创建网络频道。
        /// </summary>
        /// <param name="name">网络频道名称。</param>
        /// <param name="serviceType">网络服务类型。</param>
        /// <param name="networkChannelHelper">网络频道辅助器。</param>
        /// <returns>要创建的网络频道。</returns>
        public INetworkChannel CreateNetworkChannel(string name, ServiceType serviceType, INetworkChannelHelper networkChannelHelper)
        {
            if (networkChannelHelper == null)
            {
                throw new GameFrameworkException("Network channel helper is invalid.");
            }

            if (networkChannelHelper.PacketHeaderLength < 0)
            {
                throw new GameFrameworkException("Packet header length is invalid.");
            }

            if (HasNetworkChannel(name))
            {
                throw new GameFrameworkException(Utility.Text.Format("Already exist network channel '{0}'.", name ?? string.Empty));
            }

            NetworkChannelBase networkChannel = null;

            switch (serviceType)
            {
            case ServiceType.Tcp:
                networkChannel = new TcpNetworkChannel(name, networkChannelHelper);
                break;

            case ServiceType.TcpWithSyncReceive:
                networkChannel = new TcpWithSyncReceiveNetworkChannel(name, networkChannelHelper);
                break;

            default:
                throw new GameFrameworkException(Utility.Text.Format("Not supported service type '{0}'.", serviceType.ToString()));
            }

            networkChannel.NetworkChannelConnected     += OnNetworkChannelConnected;
            networkChannel.NetworkChannelClosed        += OnNetworkChannelClosed;
            networkChannel.NetworkChannelMissHeartBeat += OnNetworkChannelMissHeartBeat;
            networkChannel.NetworkChannelError         += OnNetworkChannelError;
            networkChannel.NetworkChannelCustomError   += OnNetworkChannelCustomError;
            m_NetworkChannels.Add(name, networkChannel);
            return(networkChannel);
        }
Beispiel #3
0
        /// <summary>
        /// Constructs a controller for a new computation.
        /// </summary>
        /// <param name="config">Controller configuration</param>
        public BaseController(Configuration config)
        {
            this.activated     = false;
            this.configuration = config;

            this.SerializationFormat = SerializationFactory.GetCodeGeneratorForVersion(config.SerializerVersion.First, config.SerializerVersion.Second);

            // set up an initial endpoint to try starting the server listening on. If endpoint is null
            // when we call the server constructor, it will choose one by picking an available port to listen on
            IPEndPoint endpoint = null;

            if (this.configuration.Endpoints != null)
            {
                endpoint = this.configuration.Endpoints[this.configuration.ProcessID];
            }

            // if we pass in a null endpoint the server will pick one and return it in the ref arg
            this.server        = new NaiadServer(ref endpoint);
            this.localEndpoint = endpoint;

            this.workerGroup = new BaseWorkerGroup(this, config.WorkerCount);

            this.workerGroup.Start();
            this.workerGroup.Activate();

            if (this.configuration.ReadEndpointsFromPPM || this.configuration.Processes > 1)
            {
                this.server.Start();

                if (this.configuration.ReadEndpointsFromPPM)
                {
                    int pId;
                    this.configuration.Endpoints = RegisterAndWaitForPPM(out pId);
                    this.configuration.ProcessID = pId;
                }

                if (this.configuration.Processes > 1)
                {
                    TcpNetworkChannel networkChannel = new TcpNetworkChannel(0, this, config);
                    this.networkChannel = networkChannel;

                    this.server.RegisterNetworkChannel(networkChannel);

                    this.server.AcceptPeerConnections();

                    this.networkChannel.WaitForAllConnections();

                    Logging.Info("Network channel activated");
                }
                else
                {
                    Logging.Info("Configured for single-process operation");
                }
            }

            this.defaultPlacement = new Placement.RoundRobin(this.configuration.Processes, this.workerGroup.Count);

#if DEBUG
            Logging.Progress("Warning: DEBUG build. Not for performance measurements.");
#endif

            if (this.workerGroup.Count < Environment.ProcessorCount)
            {
                Logging.Progress("Warning: Using fewer threads than available processors (use -t to set number of threads).");
            }

            Logging.Progress("Initializing {0} {1}", this.workerGroup.Count, this.workerGroup.Count == 1 ? "thread" : "threads");
            Logging.Progress("Server GC = {0}", System.Runtime.GCSettings.IsServerGC);
            Logging.Progress("GC settings latencymode={0}", System.Runtime.GCSettings.LatencyMode);
            Logging.Progress("Using CLR {0}", System.Environment.Version);

            NaiadTracing.Trace.ProcessInfo(this.configuration.ProcessID, System.Environment.MachineName);
            NaiadTracing.Trace.LockInfo(this.GlobalLock, "Controller lock");

            if (this.NetworkChannel != null)
            {
                this.NetworkChannel.StartMessageDelivery();
            }

            this.graphsManaged    = 0;
            this.baseComputations = new List <BaseComputation>();
        }