Пример #1
0
        private void btnHost_Click(object sender, EventArgs e)
        {
            if (!ValidNameEntered(out string name))
            {
                return;
            }
            if (!ValidPortEntered(out _port))
            {
                return;
            }

            _myInfo = new ConnectedPlayerInformation(1, name);
            OnTcpPlayerConnected(_myInfo);

            _isHost         = true;
            btnHost.Enabled = false;
            btnJoin.Enabled = false;
            txtIP.Enabled   = false;
            txtName.Enabled = false;

            _tcpServer = new SpideyTcpServer(_port);
            var onConnected = new Progress <ConnectedPlayerInformation>(OnTcpPlayerConnected);

            _tcpServer.Start(onConnected, _myInfo.Data);

            _udpServer = new SpideyUdpServer(_port);
            _udpBase   = _udpServer;
            var onReceiveUdpInfo = new Progress <ConnectedPlayerUdpEndPoint>(OnReceiveUdpInfo);

            _udpServer.Start(_tcpServer, _onLocationUpdate, onReceiveUdpInfo);
        }
Пример #2
0
        private void OnTcpPlayerConnected(ConnectedPlayerInformation connectedPlayerInfo)
        {
            var displayInfo = new ListViewItem(new[] { connectedPlayerInfo.Data, "Not started" });

            displayInfo.Tag = connectedPlayerInfo.Number;
            lstPlayers.Items.Add(displayInfo);
            if (_isHost && lstPlayers.Items.Count > 1)
            {
                btnStart.Enabled = true;
            }
        }
Пример #3
0
 private void OnLocationUpdate(ConnectedPlayerInformation connectedPlayerInfo)
 {
     try
     {
         var players = lstPlayers.Items.Cast <ListViewItem>();
         var player  = players.FirstOrDefault(p => Convert.ToInt32(p.Tag) == connectedPlayerInfo.Number);
         if (player.SubItems[1].Text != connectedPlayerInfo.Data)
         {
             player.SubItems[1].Text = connectedPlayerInfo.Data;
         }
     }
     catch (Exception ex)
     {
         // TODO - handle this better, can fail when trying to update when shutting down
     }
 }
Пример #4
0
        private void btnJoin_Click(object sender, EventArgs e)
        {
            if (!ValidNameEntered(out string name))
            {
                return;
            }
            if (!ValidIpEntered(out _serverIp))
            {
                return;
            }
            if (!ValidPortEntered(out _port))
            {
                return;
            }

            _myInfo = new ConnectedPlayerInformation(0, name);

            btnHost.Enabled = false;
            btnJoin.Enabled = false;
            txtIP.Enabled   = false;
            txtName.Enabled = false;

            _udpClient = new SpideyUdpClient(_serverIp, _port);
            _udpBase   = _udpClient;
            var onUdpClientConnected = new Progress <bool>(OnUdpClientConnected);

            _udpClient.Start(onUdpClientConnected, _onLocationUpdate);

            _tcpClient = new SpideyTcpClient(_serverIp, _port);
            var onReceivePlayerNumber = new Progress <byte>(OnReceivePlayerNumber);
            var onConnected           = new Progress <ConnectedPlayerInformation>(OnTcpPlayerConnected);
            var onServerStarted       = new Progress <bool>(OnServerStarted);
            var onReceiveUdpInfo      = new Progress <ConnectedPlayerUdpEndPoint>(OnReceiveUdpInfo);

            _tcpClient.Start(_udpClient, onReceivePlayerNumber, onConnected, onServerStarted, onReceiveUdpInfo, name);

            OnReceiveUdpInfo(new ConnectedPlayerUdpEndPoint(1, new IPEndPoint(_serverIp, _port)));
        }
Пример #5
0
 private void OnReceivePlayerNumber(byte myPlayerNumber)
 {
     _myInfo = new ConnectedPlayerInformation(myPlayerNumber, _myInfo.Data);
 }