// Обработка сообщения от клиента о начале игры private void StartGameEvent(string ipClient) { try { // Установка клиента к ipClient m_FromServer = (FromServerTransmittor)Activator.GetObject(typeof(FromServerTransmittor), ipClient + "/FromServerTransmittor"); BeginInvoke(new SetStartGameDelegate(SetStartGame)); } catch { MessageBox.Show("Ошибка работы по сети.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// Непосредственно разрыв private void DisableConnect() { ChannelServices.UnregisterChannel(ChannelServices.RegisteredChannels[0]); try { if (m_RefFromClient != null) { RemotingServices.Unmarshal(m_RefFromClient); RemotingServices.Disconnect(m_FromClient); } } // ReSharper disable EmptyGeneralCatchClause catch { } // ReSharper restore EmptyGeneralCatchClause try { if (m_RefFromServer != null) { RemotingServices.Unmarshal(m_RefFromServer); RemotingServices.Disconnect(m_FromServer); } } // ReSharper disable EmptyGeneralCatchClause catch { } // ReSharper restore EmptyGeneralCatchClause m_TypePlayer = TypePlayer.Local; serverButton.Visible = connectButton.Visible = true; disconnectButton.Visible = false; m_FromClient = null; m_FromServer = null; startGameButton.Enabled = true; m_WhoStep = ObjectType.Empty; m_ClientReady = m_ServerReady = false; textBoxNetInfo.Text += "Соединение разорвано."; }
// Подключиться к серверу private void connectButton_Click(object sender, EventArgs e) { try { m_ClientReady = m_ServerReady = false; new ServerConnectForm(this).ShowDialog(); if (IpConnect != "") { ChannelServices.RegisterChannel(new TcpChannel(8081), false); m_FromClient = (FromClientTransmittor)Activator.GetObject(typeof(FromClientTransmittor), "tcp://" + IpConnect + ":8080/FromClientTransmittor"); // Установить свой сервер для получения ответа от сервера m_FromServer = new FromServerTransmittor(); m_RefFromServer = RemotingServices.Marshal(m_FromServer, "FromServerTransmittor"); // Обработка события на запрос о ничьей m_FromServer.ToClientRemiEvent += ToClientRemiEvent; // Обработка события на сообщение о сдаче сервера m_FromServer.ToClientDeliverEvent += ToClientDeliverEvent; // Обработка очередного хода сервера m_FromServer.ToClientStepEvent += ToClientStepEvent; // Обработка события на готовность клиента играть m_FromServer.ToClientReadyEvent += ToClientReadyEvent; // Обработка события на отключение клиента m_FromServer.ToClientDisableEvent += ToClientDisableEvent; // Получение сетевого адреса клиента (вида tcp://xxx.xxx.xxx.xxx:8081) IPAddress ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList[Dns.GetHostEntry(Dns.GetHostName()).AddressList.Length - 1]; string addr = "tcp://" + ip + ":8081"; var cb = new AsyncCallback(StartGameQuery); var d = new StartGameDelegate(m_FromClient.StartGame); d.BeginInvoke(addr, cb, null); m_WhoStep = ObjectType.Cross; m_TypePlayer = TypePlayer.Client; serverButton.Visible = connectButton.Visible = false; disconnectButton.Visible = true; textBoxNetInfo.Text = "Соединение с сервером установлено\r\n"; } } catch { MessageBox.Show("Подключение к серверу не установлено.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); ChannelServices.UnregisterChannel(ChannelServices.RegisteredChannels[0]); try { if (m_RefFromClient != null) { RemotingServices.Unmarshal(m_RefFromClient); RemotingServices.Disconnect(m_FromClient); } } // ReSharper disable EmptyGeneralCatchClause catch { } // ReSharper restore EmptyGeneralCatchClause try { if (m_RefFromServer != null) { RemotingServices.Unmarshal(m_RefFromServer); RemotingServices.Disconnect(m_FromServer); } } // ReSharper disable EmptyGeneralCatchClause catch { } // ReSharper restore EmptyGeneralCatchClause } }