public UpstreamPayload DecodeUpstream(byte[] bytes) { var(headerLength, payloadLength) = DecodeLengths(bytes); PacketHeader header = DecodeHeader(bytes, headerLength, PrintHeader); byte[] payload; if (header.EncryptionMode != PacketHeader.Types.EncryptionMode.KEncryptionNone) { var key = header.EncryptionMode == PacketHeader.Types.EncryptionMode.KEncryptionServiceToken ? SecurityKey : SessionKey; payload = Decrypt(bytes, headerLength, payloadLength, key); } else { payload = new byte[payloadLength]; Array.Copy(bytes, Offset + headerLength, payload, 0, payloadLength); } if (payload.Length != header.DecodedPayloadLen) { #if DEBUG Console.WriteLine("Payload length does not match"); Console.WriteLine(Convert.ToBase64String(payload)); #endif return(null); } UpstreamPayload upstream = UpstreamPayload.Parser.ParseFrom(payload); return(upstream); }
byte[] PushMessage() { var msg = new UpstreamPayload { Command = Command.PUSH_MESSAGE, SeqId = SeqId, RetryCount = RetryCount, SubBiz = SubBiz }; var body = msg.ToByteString(); var header = new PacketHeader { AppId = AppId, Uid = UserId, InstanceId = InstanceId, DecodedPayloadLen = body.Length, EncryptionMode = PacketHeader.Types.EncryptionMode.KEncryptionSessionKey, SeqId = HeaderSeqId, Kpn = KPN, }; return(Encode(header, body)); }
byte[] KeepAlive() { var keepalive = new KeepAliveRequest { PresenceStatus = RegisterRequest.Types.PresenceStatus.KPresenceOnline, AppActiveStatus = RegisterRequest.Types.ActiveStatus.KAppInForeground, }; var payload = new UpstreamPayload { Command = Command.KEEP_ALIVE, SeqId = SeqId, RetryCount = RetryCount, PayloadData = keepalive.ToByteString(), SubBiz = SubBiz }; var body = payload.ToByteString(); var header = new PacketHeader { AppId = AppId, Uid = UserId, InstanceId = InstanceId, DecodedPayloadLen = body.Length, EncryptionMode = PacketHeader.Types.EncryptionMode.KEncryptionSessionKey, SeqId = SeqId, Kpn = KPN, }; return(Encode(header, body)); }
byte[] Ping() { var ping = new PingRequest { PingType = PingRequest.Types.PingType.KPostRegister, }; var payload = new UpstreamPayload { Command = Command.PING, SeqId = SeqId, RetryCount = RetryCount, PayloadData = ping.ToByteString(), SubBiz = SubBiz }; var body = payload.ToByteString(); var header = new PacketHeader { AppId = AppId, Uid = UserId, InstanceId = InstanceId, DecodedPayloadLen = body.Length, EncryptionMode = PacketHeader.Types.EncryptionMode.KEncryptionSessionKey, Kpn = KPN }; return(Encode(header, body)); }
byte[] Unregister() { var unregister = new UnregisterRequest(); var payload = new UpstreamPayload { Command = Command.UNREGISTER, RetryCount = RetryCount, PayloadData = unregister.ToByteString(), SubBiz = SubBiz }; var body = payload.ToByteString(); var header = new PacketHeader { AppId = AppId, Uid = UserId, EncryptionMode = PacketHeader.Types.EncryptionMode.KEncryptionSessionKey, DecodedPayloadLen = body.Length, Kpn = KPN }; return(Encode(header, body)); }
byte[] Register() { var request = new RegisterRequest { AppInfo = new AppInfo { AppName = AppName, SdkVersion = SdkVersion, }, DeviceInfo = new DeviceInfo { PlatformType = DeviceInfo.Types.PlatformType.H5, DeviceModel = "h5", }, PresenceStatus = RegisterRequest.Types.PresenceStatus.KPresenceOnline, AppActiveStatus = RegisterRequest.Types.ActiveStatus.KAppInForeground, InstanceId = InstanceId, ZtCommonInfo = new ZtCommonInfo { Kpn = KPN, Kpf = KPF, Uid = UserId, } }; var payload = new UpstreamPayload { Command = Command.REGISTER, SeqId = SeqId++, RetryCount = RetryCount, PayloadData = request.ToByteString(), SubBiz = SubBiz, }; var body = payload.ToByteString(); var header = new PacketHeader { AppId = AppId, Uid = UserId, EncryptionMode = PacketHeader.Types.EncryptionMode.KEncryptionServiceToken, DecodedPayloadLen = body.Length, TokenInfo = new TokenInfo { TokenType = TokenInfo.Types.TokenType.KServiceToken, Token = ByteString.CopyFromUtf8(ServiceToken), }, SeqId = HeaderSeqId++, Kpn = KPN, }; return(Encode(header, body)); }
byte[] Heartbeat() { var hearbeat = new ZtLiveCsHeartbeat { ClientTimestampMs = DateTimeOffset.Now.ToUnixTimeMilliseconds(), Sequence = HeartbeatSeqId++, }; var cmd = new ZtLiveCsCmd { CmdType = GlobalCommand.HEARTBEAT, Payload = hearbeat.ToByteString(), Ticket = Tickets[TicketIndex], LiveId = LiveId, }; var payload = new UpstreamPayload { Command = Command.GLOBAL_COMMAND, SeqId = SeqId, RetryCount = RetryCount, PayloadData = cmd.ToByteString(), SubBiz = SubBiz, }; var body = payload.ToByteString(); var header = new PacketHeader { AppId = AppId, Uid = UserId, InstanceId = InstanceId, DecodedPayloadLen = body.Length, EncryptionMode = PacketHeader.Types.EncryptionMode.KEncryptionSessionKey, SeqId = SeqId++, Kpn = KPN }; return(Encode(header, body)); }
byte[] EnterRoom() { var request = new ZtLiveCsEnterRoom { EnterRoomAttach = EnterRoomAttach, ClientLiveSdkVersion = ClientLiveSdkVersion }; var cmd = new ZtLiveCsCmd { CmdType = GlobalCommand.ENTER_ROOM, Payload = request.ToByteString(), Ticket = Tickets[TicketIndex], LiveId = LiveId, }; var payload = new UpstreamPayload { Command = Command.GLOBAL_COMMAND, SeqId = SeqId++, RetryCount = RetryCount, PayloadData = cmd.ToByteString(), SubBiz = SubBiz, }; var body = payload.ToByteString(); var header = new PacketHeader { AppId = AppId, Uid = UserId, InstanceId = InstanceId, DecodedPayloadLen = body.Length, EncryptionMode = PacketHeader.Types.EncryptionMode.KEncryptionSessionKey, SeqId = HeaderSeqId++, Kpn = KPN }; return(Encode(header, body)); }