Пример #1
0
        public void Send(string data, ChannelType type, string recipient)
        {
            if (!_session.LocalId.HasValue)
            {
                Log.Warn("Attempted to send a text message before connected to Dissonance session");
                return;
            }

            if (type == ChannelType.Player)
            {
                //Find destination player
                ClientInfo <TPeer?> info;
                if (!_peers.TryGetClientInfoByName(recipient, out info))
                {
                    Log.Warn("Attempted to send text message to unknown player '{0}'", recipient);
                    return;
                }

                //Write packet
                var writer = new PacketWriter(_sender.GetSendBuffer());
                writer.WriteTextPacket(_session.SessionId, _session.LocalId.Value, type, info.PlayerId, data);

                //Send it
                _tmpDests.Clear();
                _tmpDests.Add(info);
                _sender.EnqueueReliableP2P(_session.LocalId.Value, _tmpDests, writer.Written);
                _tmpDests.Clear();
            }
            else
            {
                //Find destination players (early exit if no one is in this room)
                List <ClientInfo <TPeer?> > clients;
                if (!_peers.TryGetClientsInRoom(recipient, out clients))
                {
                    return;
                }

                //Write packet
                var writer = new PacketWriter(_sender.GetSendBuffer());
                writer.WriteTextPacket(_session.SessionId, _session.LocalId.Value, type, recipient.ToRoomId(), data);

                //send it
                _sender.EnqueueReliableP2P(_session.LocalId.Value, clients, writer.Written);
            }
        }
Пример #2
0
        private void SendClientState()
        {
            //Sanity check
            var clientId = _session.LocalId;

            if (Log.AssertAndLogError(clientId.HasValue, "EBC361ED-780A-4DE0-944D-3D4D983B785D", "Attempting to send local client state before assigned an ID by the server"))
            {
                return;
            }

            //Send the local state
            var writer = new PacketWriter(_sender.GetSendBuffer());

            writer.WriteClientState(_session.SessionId, _playerName, clientId.Value, _codecSettings, _localRooms);
            _sender.EnqueueReliable(writer.Written);
            Log.Debug("Sent local client state");

            //begin watching for changes in rooms
            _localRooms.JoinedRoom -= SendJoinRoom;
            _localRooms.JoinedRoom += SendJoinRoom;
            _localRooms.LeftRoom   -= SendLeaveRoom;
            _localRooms.LeftRoom   += SendLeaveRoom;
        }
Пример #3
0
        /// <summary>
        /// Begin negotiating a connection with the server by sending a handshake.
        /// </summary>
        /// <remarks>It is safe to call this several times, even once negotiation has finished</remarks>
        private void SendHandshake(DateTime utcNow)
        {
            //Sanity check. We can't do *anything* with a disconnected client, definitely not restart negotiation!
            Log.AssertAndThrowPossibleBug(
                State != ConnectionState.Disconnected,
                "39533F23-2DAC-4340-9A7D-960904464E23",
                "Attempted to begin connection negotiation with a client which is disconnected");

            _lastHandshakeRequest = utcNow;

            //Send the handshake request to the server (when the server replies with a response, we know we're connected)
            _sender.EnqueueReliable(
                new PacketWriter(new ArraySegment <byte>(_sender.GetSendBuffer()))
                .WriteHandshakeRequest(_playerName, _codecSettings)
                .Written
                );
            Log.Trace("Sent HandshakeRequest");

            //Set the state to negotiating only if the state was previously none
            Interlocked.CompareExchange(ref _connectionStateValue, (int)ConnectionState.Negotiating, (int)ConnectionState.None);
        }