Exemplo n.º 1
0
        private void ParseMessage(UdpReceiveResult result, SpideyTcpServer tcpServer, IProgress <ConnectedPlayerInformation> onLocationUpdate, IProgress <ConnectedPlayerUdpEndPoint> onReceiveUdpInfo)
        {
            if (result.Buffer.Length < SpideyUdpMessage.MESSAGE_HEADER_SIZE)
            {
                return;
            }

            var  message     = result.Buffer;
            var  messageType = message[0];
            byte playerNumber;

            switch (messageType)
            {
            case SpideyUdpMessage.SPIN_A_WEB:
                if (!SpideyUdpMessage.ParseSpinAWebMessage(message, out playerNumber))
                {
                    break;
                }

                var added = tcpServer.AddUdpClientInformation(playerNumber, result.RemoteEndPoint);
                if (added)
                {
                    ++_playerCount;
                    onReceiveUdpInfo.Report(new ConnectedPlayerUdpEndPoint(playerNumber, new IPEndPoint(result.RemoteEndPoint.Address, result.RemoteEndPoint.Port)));
                }
                break;

            case SpideyUdpMessage.SPIDERMAN:
                if (!SpideyUdpMessage.ParseSpidermanMessage(message, out playerNumber, out byte[] spideyData, out byte levelData))
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
 public void Start(SpideyTcpServer tcpServer, IProgress <ConnectedPlayerInformation> onLocationUpdate, IProgress <ConnectedPlayerUdpEndPoint> onReceiveUdpInfo)
 {
     if (IsServerTaskStopped())
     {
         var lTask = new Task <Task>(async() => await StartListening(tcpServer, onLocationUpdate, onReceiveUdpInfo));
         _udpTask = lTask.Unwrap();
         //_serverTask.ObserveExceptions();
         _udpTaskCancellationToken = new CancellationTokenSource();
         lTask.Start();
     }
 }
Exemplo n.º 4
0
        private async Task StartListening(SpideyTcpServer tcpServer, IProgress <ConnectedPlayerInformation> onLocationUpdate, IProgress <ConnectedPlayerUdpEndPoint> onReceiveUdpInfo)
        {
            var udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, _port));

            try
            {
                _playerCount = 0;

                while (!_udpTaskCancellationToken.IsCancellationRequested)
                {
                    var result = await udpClient.ReceiveAsync().WithWaitCancellation(_udpTaskCancellationToken.Token);

                    ParseMessage(result, tcpServer, onLocationUpdate, onReceiveUdpInfo);
                }
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                udpClient.Close();
            }
        }