/**
         *   Activate/Deactivate server
         */

        public NetworkBackgammonRemoteGameRoom ActivateServer(string port)
        {
            if (m_channel == null)
            {
                // We need to use binary formatters, which allow the serialization of generic collections
                BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
                serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
                BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();

                IDictionary props = new Hashtable();
                props["port"] = Convert.ToInt32(port);
                props["name"] = String.Empty;

                m_channel = new HttpChannel(props, clientProv, serverProv);
            }
            ChannelServices.RegisterChannel(m_channel, false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(NetworkBackgammonRemoteGameRoom), "GameRoom", WellKnownObjectMode.Singleton);

            // Assign the instantiated remote server to the local server
            MarshalByRefObject obj = (MarshalByRefObject)RemotingServices.Connect(typeof(NetworkBackgammonRemoteGameRoom), "http://127.0.0.1:" + port + "/GameRoom");

            gameRoom = obj as NetworkBackgammonRemoteGameRoom;

            return(gameRoom);
        }
コード例 #2
0
        // Connect to a remote server (remoting) via an ip address and port
        public bool ConnectServer(string ipAddr, string port)
        {
            bool retval = false;

            try
            {
                // First disconenct any previous connection with the server
                DisconnectServer();

                // Get an available channel to use
                int clientChannel = FindUnusedPort(IPAddress.Loopback);

                if (clientChannel != 0)
                {
                    BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
                    serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
                    BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();

                    IDictionary props = new Hashtable();
                    props["port"]        = clientChannel;
                    props["machineName"] = System.Environment.MachineName;
                    props["name"]        = String.Empty;

                    // Register a client channel so the server an communicate back - it needs a channel
                    // opened for the callback to the CallbackSink object that is anchored on the client!
                    // channel = new HttpChannel(clientChannel++);
                    channel = new HttpChannel(props, clientProv, serverProv);
                }
                else
                {
                    throw new Exception("Couldn't find unused client port!");
                }

                // Registers a channel with the channel services
                ChannelServices.RegisterChannel(channel, false);

                // Now create a transparent proxy to the server component
                MarshalByRefObject obj = (MarshalByRefObject)RemotingServices.Connect(typeof(NetworkBackgammonRemoteGameRoom), "http://" + ipAddr + ":" + port + "/GameRoom");
                gameRoom = obj as NetworkBackgammonRemoteGameRoom;

                serverIpAddress = ipAddr;
                serverPort      = port;

                retval = gameRoom.VerifyConnection("ack") == "nack";
            }
            catch (Exception ex)
            {
                // Disconnect from the server
                DisconnectServer();
            }

            return(retval);
        }
コード例 #3
0
        // Disconnect as from the server and remove client as listener
        public void DisconnectServer()
        {
            if (IsConnected && (Player != null))
            {
                // Remove the player from the game room
                gameRoom.Leave(Player);
            }

            if (channel != null)
            {
                ChannelDataStore cds = (ChannelDataStore)channel.ChannelData;

                foreach (string s in cds.ChannelUris)
                {
                    channel.StopListening(s);
                }

                ChannelServices.UnregisterChannel(channel);
            }

            gameRoom = null;
            Player   = null;
            channel  = null;
        }