Esempio n. 1
0
        void ParseMessage(byte[] recv)
        {
            UnpackingResult <MessagePackObject> raw = Unpacking.UnpackObject(recv);

            var message = raw.Value.AsList();
            var code    = message [0].AsInt32();

            // Parse roomId or roomName
            Room   room        = null;
            int    roomIdInt32 = 0;
            string roomId      = "0";
            string roomName    = null;

            try {
                roomIdInt32 = message[1].AsInt32();
                roomId      = roomIdInt32.ToString();
            } catch (InvalidOperationException) {
                try {
                    roomName = message[1].AsString();
                } catch (InvalidOperationException) {}
            }

            if (code == Protocol.USER_ID)
            {
                this.id = message [1].AsString();
                this.OnOpen.Invoke(this, EventArgs.Empty);
            }
            else if (code == Protocol.JOIN_ROOM)
            {
                roomName = message[2].AsString();

                if (this.rooms.ContainsKey(roomName))
                {
                    this.rooms [roomId] = this.rooms [roomName];
                    this.rooms.Remove(roomName);
                }

                room    = this.rooms [roomId];
                room.id = roomIdInt32;
            }
            else if (code == Protocol.JOIN_ERROR)
            {
                room = this.rooms [roomName];

                MessageEventArgs error = new MessageEventArgs(room, message);
                room.EmitError(error);
                this.OnError.Invoke(this, error);
                this.rooms.Remove(roomName);
            }
            else if (code == Protocol.LEAVE_ROOM)
            {
                room = this.rooms [roomId];
                room.Leave(false);
            }
            else if (code == Protocol.ROOM_STATE)
            {
                var state             = message [2];
                var remoteCurrentTime = message [3].AsInt32();
                var remoteElapsedTime = message [4].AsInt32();

                room = this.rooms [roomId];
                // JToken.Parse (message [2].ToString ())
                room.SetState(state, remoteCurrentTime, remoteElapsedTime);
            }
            else if (code == Protocol.ROOM_STATE_PATCH)
            {
                room = this.rooms [roomId];

                IList <MessagePackObject> patchBytes = message [2].AsList();
                byte[] patches = new byte[patchBytes.Count];

                int idx = 0;
                foreach (MessagePackObject obj in patchBytes)
                {
                    patches[idx] = obj.AsByte();
                    idx++;
                }

                room.ApplyPatch(patches);
            }
            else if (code == Protocol.ROOM_DATA)
            {
                room = this.rooms [roomId];
                room.ReceiveData(message [2]);
                this.OnMessage.Invoke(this, new MessageEventArgs(room, message[2]));
            }
        }
Esempio n. 2
0
		void ParseMessage(byte[] recv)
		{
			var stream = new MemoryStream(recv);
			var raw = MsgPack.Deserialize<List<object>>(stream);

			//object[] message = new object[raw.Values.Count];
			//raw.Values.CopyTo(message, 0);

			var message = raw;
			var code = (byte) message[0];

			// Parse roomId or roomName
			Room room = null;
			int roomIdInt32 = 0;
			string roomId = "0";
			string roomName = null;

			try
			{
				roomIdInt32 = (byte) message[1];
				roomId = roomIdInt32.ToString();
			}
			catch (Exception)
			{
				try
				{
					roomName = (string) message[1];
				}
				catch (Exception)
				{
				}
			}

			if (code == Protocol.USER_ID)
			{
				this.id = (string) message[1];
				this.OnOpen.Invoke(this, EventArgs.Empty);
			}
			else if (code == Protocol.JOIN_ROOM)
			{
				roomName = (string) message[2];

				if (this.rooms.ContainsKey(roomName))
				{
					this.rooms[roomId] = this.rooms[roomName];
					this.rooms.Remove(roomName);
				}

				room = this.rooms[roomId];
				room.id = roomIdInt32;
			}
			else if (code == Protocol.JOIN_ERROR)
			{
				room = this.rooms[roomName];

				MessageEventArgs error = new MessageEventArgs(room, message);
				room.EmitError(error);
				this.OnError.Invoke(this, error);
				this.rooms.Remove(roomName);
			}
			else if (code == Protocol.LEAVE_ROOM)
			{
				room = this.rooms[roomId];
				room.Leave(false);
			}
			else if (code == Protocol.ROOM_STATE)
			{
				var state = (IndexedDictionary<string, object>) message[2];
				var remoteCurrentTime = (double) message[3];
				var remoteElapsedTime = (byte) message[4];

				room = this.rooms[roomId];
				// JToken.Parse (message [2].ToString ())
				room.SetState(state, remoteCurrentTime, remoteElapsedTime);
			}
			else if (code == Protocol.ROOM_STATE_PATCH)
			{
				room = this.rooms[roomId];

				var patchBytes = (List<object>) message[2];
				byte[] patches = new byte[patchBytes.Count];

				int idx = 0;
				foreach (byte obj in patchBytes)
				{
					patches[idx] = obj;
					idx++;
				}

				room.ApplyPatch(patches);
			}
			else if (code == Protocol.ROOM_DATA)
			{
				room = this.rooms[roomId];
				room.ReceiveData(message[2]);
				this.OnMessage.Invoke(this, new MessageEventArgs(room, message[2]));
			}
		}