Beispiel #1
0
        /// <summary>
        /// serialize the NetUserData over the network to the room that has this player
        /// </summary>
        /// <returns>false, if the data was not synchronized (the user isn't in a room, or NetUserData is null)</returns>
        public bool SynchNetData()
        {
#if DEBUG
            Debug.Log($"Syncing net data for {this}");
#endif
            if (NetUserData == null)
            {
                return(false);
            }
            var msg = Server.GetMessage(NetUserData.AllocSize + 4);
            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            msg.Write(DandRRpcs.SyncNetUser);
            msg.Write(Id);
            NetUserData.OnSerialize(msg);

            var room = Room;
            if (room == null)
            {
                NetMessage.RecycleMessage(msg);
                return(false);
            }

            room.SendMessage(msg, ReliabilityMode.Ordered);

            return(true);
        }
Beispiel #2
0
        void SendRoomSwitch(Room room)
        {
            _switchToken = Guid.NewGuid();

            _switchingToRoom = room.Guid;

            //todo: tell old room that the player is going to leave.
            NetMessage rmsg;

            _oldRoom     = _currentRoom;
            _currentRoom = Guid.Empty;

            if (Server.TryGetRoom(_oldRoom, out var oldRoom))
            {
                rmsg = Server.GetMessage(4);
                rmsg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
                rmsg.Write(DandRRpcs.ExpectLeavingPlayer);
                rmsg.Write(Id);
                oldRoom.SendMessage(rmsg, ReliabilityMode.Ordered);
            }


            var pmsg = Server.GetMessage(30 + room.RoomId.Length * 2);

            pmsg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            pmsg.Write(DandPRpcs.RoomSwitch);
            pmsg.Write(room.Address);  //this is ~4 bytes + 3 bytes
            pmsg.Write(room.RoomId);
            pmsg.Write(_switchToken);
            SendMessage(pmsg, ReliabilityMode.Ordered);
        }
Beispiel #3
0
        internal void AddRoom(Room room)
        {
            _rooms[room.Guid] = room;

            try
            {
                RoomAdded?.Invoke(room);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            //tell all rooms about this room

            var msg = GetMessage(room.RoomId.Length * 2 + 18);

            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            msg.Write(DandRRpcs.RoomAdd);
            msg.Write(room.RoomId);
            msg.Write(room.Guid);
            room.SendMessageToOthers(msg, ReliabilityMode.Ordered);

            //tell this room about all other rooms
            msg = GetMessage(1000); // f**k if I know
            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            msg.Write(DandRRpcs.RoomAdd);
            foreach (var r in _rooms.ToArray())
            {
                msg.Write(r.Value.RoomId);
                msg.Write(r.Value.Guid);
            }
            room.SendMessage(msg, ReliabilityMode.Ordered);
        }
Beispiel #4
0
        partial void ImplementationShutdown(string reason)
        {
            _acceptEnabled = false;
            _roomListener.Stop();
            _clientListener.Stop();
            _shutdownTokenSource.Cancel();

            var shutReason = GetMessage(reason.Length * 2 + 6);

            shutReason.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Owner, MsgType.Internal));
            shutReason.Write(DandRRpcs.DisconnectMessage);
            shutReason.Write(reason);
            shutReason.WriteSize();

            foreach (var room in _rooms.ToArray())
            {
                room.Value.SendTcpReadyMessage(shutReason);
            }
            Player[] players;
            lock (_players)
            {
                players = _players.Values;
            }
            foreach (var player in players)
            {
                player.SendTcpReadyMessage(shutReason);
            }
            _tcpRunning = false;
        }
        private void EnsureConnected(ConnectionInfo connection)
        {
            var msg = GetMessage(2);

            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            msg.Write(DandPRpcs.Ping);
            SendTcpMessage(connection, msg);
        }
        internal static NetMessage StartMessage(Room room, byte rpcId, ReliabilityMode mode, int size)
        {
            var msg = room.RoomGetMessage(size + 2);

            msg.Write(RpcUtils.GetHeader(mode, BroadcastMode.Owner, MsgType.Static));
            msg.Write(rpcId);
            return(msg);
        }
        /// <summary>
        /// Create a stream to serialize into
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public NetMessage CreateStream(int size)
        {
            var msg = Room.RoomGetMessage(size + 3);

            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Unreliable, BroadcastMode.All, MsgType.Stream));
            msg.Write(Id);
            return(msg);
        }
Beispiel #8
0
        NetMessage StartMessage(byte rpcId, ReliabilityMode mode, int size)
        {
            var msg = _server.GetMessage(size + 2);

            msg.Write(RpcUtils.GetHeader(mode, BroadcastMode.Server, MsgType.Static));
            msg.Write(rpcId);
            return(msg);
        }
        NetMessage StartMessage(byte cid, byte rpcId, RpcMode mode, int size)
        {
            var msg = Room.RoomGetMessage(size + 5);

            msg.Write(RpcUtils.GetHeader(mode, MsgType.Netview));
            Id.OnSerialize(msg);
            msg.Write(cid);
            msg.Write(rpcId);
            return(msg);
        }
Beispiel #10
0
        NetMessage StartMessage(byte rpcId, RpcMode mode, int size)
        {
            var msg = _room.RoomGetMessage(size + 5);

            msg.Write(RpcUtils.GetHeader(mode, MsgType.Internal));
            msg.Write(RandPRpcs.SceneObjectRpc);
            msg.Write(NetworkID);
            msg.Write(rpcId);
            return(msg);
        }
Beispiel #11
0
        /// <summary>
        /// change the player to the specified room
        /// </summary>
        /// <param name="roomName"></param>
        public void ChangeRoom(string roomName)
        {
            var msg = Room.ServerGetMessage(roomName.Length * 2 + 2);

            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            msg.Write(DandRRpcs.RoomSwitch);
            msg.Write(Id);
            msg.Write(roomName);
            Room.Server.SendMessage(msg, ReliabilityMode.Ordered);
        }
 private void EnsureConnected()
 {
     while (!_shutdownQueued)
     {
         Thread.Sleep(5000);
         var msg = GetMessage(2);
         msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
         msg.Write(DandRRpcs.Ping);
         SendMessage(msg, ReliabilityMode.Ordered);
     }
 }
        /// <summary>
        /// start a message for one specific player
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="rpcId"></param>
        /// <param name="mode"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        NetMessage StartMessage(byte cid, byte rpcId, ReliabilityMode mode, int size)
        {
            var msg = Room.RoomGetMessage(size + 5);

            //just using "server" because its going to one target that isn't the owner
            msg.Write(RpcUtils.GetHeader(mode, BroadcastMode.Server, MsgType.Netview));
            Id.OnSerialize(msg);
            msg.Write(cid);
            msg.Write(rpcId);
            return(msg);
        }
        private void ReturnFuncRpc(byte componentId, byte rpc, Player sender, object ret)
        {
            var msg = Room.RoomGetMessage(5 + Room.Serializer.SizeOf(ret));

            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Netview,
                                         ret is Exception ? SubMsgType.Error : SubMsgType.Reply));
            msg.Write(Id);
            msg.Write(componentId);
            msg.Write(rpc);
            Room.Serializer.Serialize(ret is Exception ? (ret as Exception).Message : ret, msg);
            SendMessage(msg, sender, ReliabilityMode.Ordered);
        }
Beispiel #15
0
        internal NetMessage GetDestroyMessage(NetworkView view, byte destType, byte reasonCode = 0)
        {
            var msg = RoomGetMessage(6);

            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.All, MsgType.Internal));
            msg.Write(destType);
            msg.Write(view.Id.Id);
            if (reasonCode != 0)
            {
                msg.Write(reasonCode);
            }
            return(msg);
        }
        protected internal override void Disconnect(string reason)
        {
            var server = Room.Server;

            if (server != null)
            {
                var msg = GetMessage(reason.Length * 2 + 5);
                msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
                msg.Write(DandRRpcs.DisconnectMessage);
                msg.Write(reason);
                SendMessage(msg, ReliabilityMode.Ordered);
            }
            _serverClient.Close();
        }
Beispiel #17
0
        /// <summary>
        ///
        /// </summary>
        public void SynchNetData()
        {
            if (NetUserData == null)
            {
                return;
            }
            var msg = Room.ServerGetMessage(NetUserData.AllocSize + 4);

            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            msg.Write(DandRRpcs.SyncNetUser);
            msg.Write(Id);
            NetUserData.OnSerialize(msg);
            Room.Server.SendMessage(msg, ReliabilityMode.Ordered);
        }
        partial void ImplDispatchDisconnect(string reason)
        {
            var server = Server;

            if (server != null)
            {
                var msg = ServerGetMessage(reason.Length * 2 + 5);
                msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
                msg.Write(DandRRpcs.DisconnectMessage);
                msg.Write(reason);
                server.SendMessage(msg, ReliabilityMode.Ordered);
            }
            _serverClient.Close();
        }
Beispiel #19
0
        internal void RemoveRoom(Room room)
        {
            if (room == null)
            {
                return;
            }

            if (!_rooms.TryRemove(room.Guid, out var removed))
            {
                Debug.LogWarning($"Tried to remove {removed}, but it wasn't in the list of rooms");
                //we'll still send messages and stuff regarding this, just in case people didn't get it. or something.
            }
            else if (removed != room)
            {
                Debug.LogError($"Removed {removed}, but we were attempting to remove {room}");
            }

            try
            {
                RoomRemoved?.Invoke(room);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            var msg = GetMessage(18);

            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            msg.Write(DandRRpcs.RoomRemove);
            msg.Write(room.Guid);
            SendToOtherRooms(room, msg, ReliabilityMode.Ordered);

            Player[] players;
            lock (_players)
                players = _players.Values;
            foreach (var player in players)
            {
                if (player == null)
                {
                    continue;
                }
                if (player.SwitchingToRoom == room.Guid)
                {
                    player.OnSwitchingToRoomInvalidated();
                }
            }
        }
Beispiel #20
0
        internal NetMessage ConstructInstMessage(NetworkView view, Vector3 position, Vector3 rotation)
        {
            var msg = RoomGetMessage(view.Resource.Length * 2 + 34);

            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.All, MsgType.Internal));
            msg.Write(RandPRpcs.Instantiate);
            msg.Write(view.Id.Id);
            msg.Write(view.Owner.Id);
            msg.Write(view.Resource);
            msg.Write(position.X);
            msg.Write(position.Y);
            msg.Write(position.Z);
            msg.Write(rotation.X);
            msg.Write(rotation.Y);
            msg.Write(rotation.Z);
            return(msg);
        }
Beispiel #21
0
        private void RemoveRoom(Room room)
        {
            if (room == null)
            {
                return;
            }

            Room removed;

            _rooms.TryRemove(room.Guid, out removed);
            if (removed != room)
            {
                Debug.LogError($"Removed {removed}, but we were attempting to remove {room}");
            }

            try
            {
                RoomRemoved?.Invoke(removed);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            var msg = GetMessage(18);

            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            msg.Write(DandRRpcs.RoomRemove);
            msg.Write(removed.Guid);
            removed.SendMessageToOthers(msg, ReliabilityMode.Ordered);

            Player[] players;
            lock (_players)
                players = _players.Values;
            foreach (var player in players)
            {
                if (player == null)
                {
                    continue;
                }
                if (player.SwitchingToRoom == removed.Guid)
                {
                    player.OnSwitchingToRoomInvalidated();
                }
            }
        }
Beispiel #22
0
        /// <summary>
        /// tell the room, if this is in one, to disconnect the player
        /// </summary>
        internal void DisconnectOnRoom()
        {
            //tell the room to disconnect the player
            var room = Room;

            if (room == null)
            {
                return;
            }

            var rmsg = Server.GetMessage(4);

            rmsg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            rmsg.Write(DandRRpcs.DisconnectPlayer);
            rmsg.Write(Id);
            room.SendMessage(rmsg, ReliabilityMode.Ordered);
        }
        protected internal override async Task Shutdown(string reason)
        {
            _acceptEnabled = false;
            _roomListener.Stop();
            _clientListener.Stop();
            _shutdownTokenSource.Cancel();

            var shutReason = GetMessage(reason.Length * 2 + 6);

            shutReason.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Owner, MsgType.Internal));
            shutReason.Write(DandRRpcs.DisconnectMessage);
            shutReason.Write(reason);
            shutReason.WriteSize();

            await Task.WhenAll(Task.WhenAll(LockRooms().Select(c => SendTcpMessage(c, shutReason, false))),
                               Task.WhenAll(LockPlayers().Select(c => SendTcpMessage(c, shutReason, false))));

            NetMessage.RecycleMessage(shutReason);
            _tcpRunning = false;
        }
Beispiel #24
0
        void ClientFinishedRoomSwitch()
        {
            if (_switchingToRoom == Guid.Empty)
            {
                Debug.LogError($"{this} said they had finished room switch, but they aren't currently in a room switching state");
                return;
            }

            if (!Server.TryGetRoom(_switchingToRoom, out var room))
            {
                Debug.LogError($"Could not get room {_switchingToRoom} when client notified us they were finishing switching their rooms. This is incomplete, and we should probably switch the player to a different room");
                return;
            }

            var rmsg = Server.GetMessage(20);

            rmsg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            rmsg.Write(DandRRpcs.ExpectPlayer);
            rmsg.Write(_switchToken);
            rmsg.Write(Id);
            room.SendMessage(rmsg, ReliabilityMode.Ordered);
        }
 internal void OnFinishedInstantiate(Player player)
 {
     //get the player up to speed
     SendBuffer(player);
     try
     {
         FinishedInstantiation?.Invoke(player);
     }
     catch (Exception e)
     {
         Debug.LogException(e);
     }
     if (MoreInstantiates != null)
     {
         var msg = Room.RoomGetMessage(4 + MoreInstantiates.AllocSize);
         msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
         msg.Write(RandPRpcs.InstantiatePlus);
         Id.OnSerialize(msg);
         MoreInstantiates.OnSerialize(msg);
         Room.SendToPlayer(player, msg, ReliabilityMode.Ordered);
     }
 }
Beispiel #26
0
        void UpdateRoomsOfNewRoom(Room room)
        {
            //tell all rooms about this room

            var msg = GetMessage(room.RoomId.Length * 2 + 18);

            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            msg.Write(DandRRpcs.RoomAdd);
            msg.Write(room.RoomId);
            msg.Write(room.Guid);
            room.SendMessageToOthers(msg, ReliabilityMode.Ordered);

            //tell this room about all other rooms
            msg = GetMessage(1000); // f**k if I know
            msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            msg.Write(DandRRpcs.RoomAdd);
            foreach (var r in _rooms.ToArray())
            {
                msg.Write(r.Value.RoomId);
                msg.Write(r.Value.Guid);
            }
            room.SendMessage(msg, ReliabilityMode.Ordered);
        }
Beispiel #27
0
        private void AddPlayer(NetConnection senderConnection)
        {
            var    player = GetPlayer(senderConnection);
            Player oldPlayer;

            _players.TryGetValue(player.Id, out oldPlayer);
            if (oldPlayer != null)
            {
                Debug.LogWarning($"Contention over id {player.Id} : {oldPlayer} is still connected, but should probably not be. Disconnecting");
                oldPlayer.Disconnect("player id contention");
            }

            _players[player.Id] = player;
            Debug.Log($"Player {player.Id} joined at {senderConnection}");
            SendViewInstantiates(player);

            var pconnected = ServerGetMessage(4);

            pconnected.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Server, MsgType.Internal));
            pconnected.Write(DandRRpcs.PlayerConnected);
            pconnected.Write(player.Id);
            Server.SendMessage(pconnected, ReliabilityMode.Ordered);
        }
        protected internal override async void Disconnect(Player player, string reason)
        {
            var msg = Server.GetMessage(reason.Length * 2 + 5);
            var ci  = player.Connection as ConnectionInfo;

            if (ci.AllowConnectCompletion.TrySetResult(false))
            {
                //we're actually denying connection, not sending a disconnect
                msg.Write(false);
                msg.WritePadBits();
                msg.Write(reason);
            }
            else
            {
                msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Owner, MsgType.Internal));
                msg.Write(DandPRpcs.DisconnectMessage);
                msg.Write(reason);
            }
            msg.WriteSize();
            try
            {
                var strm = ci.Stream;
                if (strm != null)
                {
                    await strm.WriteAsync(msg.Data, 0, msg.LengthBytes);
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            await Task.Delay(5000);

            NetMessage.RecycleMessage(msg);
            ci.Client.Close();
        }
        async partial void ImplementationDisconnect(string reason)
        {
            var msg = Server.GetMessage(reason.Length * 2 + 5);

            if (AllowConnectCompletion.TrySetResult(false))
            {
                //we're actually denying connection, not sending a disconnect
                msg.Write(false);
                msg.WritePadBits();
                msg.Write(reason);
            }
            else
            {
                msg.Write(RpcUtils.GetHeader(ReliabilityMode.Ordered, BroadcastMode.Owner, MsgType.Internal));
                msg.Write(DandPRpcs.DisconnectMessage);
                msg.Write(reason);
            }
            msg.WriteSize();
            try
            {
                var strm = NetworkStream;
                if (strm != null)
                {
                    await strm.WriteAsync(msg.Data, 0, msg.LengthBytes);
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }
            finally
            {
                NetMessage.RecycleMessage(msg);
                TcpClient.Close();
            }
        }