private async Task <object> ProcessHttpWait(IMTSession session, InnerMessage message) { var result = await Task.FromResult <object>(null); var _req = session.Services.Serialization().Deserialize(message.Content) as MTHttpWait; if (_req != null) { this.max_delay = _req.MaxDelay; this.max_wait = _req.MaxWait; this.wait_after = _req.WaitAfter; message.max_wait = _req.MaxWait; message.max_delay = _req.MaxDelay; message.wait_after = _req.WaitAfter; } var _ret = new TLInputPeerEmpty { }; if (_ret as MTObject != null) { result = new MTRpcResult { ReqMsgId = (long)message.msg_id, Result = new MTGzipPacked { PackedData = Utils.compress(BitConverter.GetBytes(0x997275b5)) } }; //session.AddMessage(result as MTObject, true, true); } return(result); }
private async Task <object> ProcessAck(IMTSession session, InnerMessage message) { var result = await Task.FromResult <object>(null); var ack = session.Services.Serialization().Deserialize(message.Content) as MTMsgsAck; var pendings = (await session.GetPending()); var prev = pendings.Count(); foreach (var msg_id in ack.MsgIds) { session.Ack(msg_id); } //session.AddMessage(new MTRpcResult //{ // ReqMsgId = (long)message.inner_msg_id, // Result = new MTGzipPacked // { // PackedData = Utils.compress( new TLInputPeerEmpty { }.ToByteArray()) // } //}, false, true); //(session as MTSession).GetMessageBox().Clear(); this.logger.LogInformation( $"{prev - pendings.Count() } messages acknowledged"); return(result); }
private async Task <object> ProcessContainer(IMTSession session, InnerMessage message, CancellationToken cancellationToken) { var result = await Task.FromResult <object>(null); using (var stream = new MemoryStream(message.Content)) using (var reader = new BinaryReader(stream)) { this.logger.LogInformation("Container..."); var type = reader.ReadUInt32(); var count = reader.ReadInt32(); for (var i = 0; i < count; i++) { var _message_id = reader.ReadUInt64(); var _seq_no = reader.ReadInt32(); var _len = reader.ReadInt32(); var _content = reader.ReadBytes(_len); var _resp = await this.ProcessProtoMessage(session, message.DoClone(_content, m => { m.msg_id = _message_id; m.seq_no = _seq_no; }), cancellationToken); } } return(result); }
private async Task <object> ProcessInvokeAftert(IMTSession session, InnerMessage message, CancellationToken cancellationToken) { var result = await Task.FromResult <object>(null); var _req = session.Services.Serialization().Deserialize(message.Content) as TLInvokeAfterMsg; if (_req != null) { await ProcessProtoMessage(session, message.DoClone(_req.Query.ToByteArray(), cfg => { }), cancellationToken); } return(result); }
private async Task <object> ProcessInitInvokeWithLayer(IMTSession session, InnerMessage message, CancellationToken cancellationToken) { var result = await Task.FromResult <object>(null); using (var stream = new MemoryStream(message.Content)) using (var reader = new BinaryReader(stream)) { var type = reader.ReadUInt32(); var layer = reader.ReadInt32(); var inner_message = reader.ReadAllBytes(); this.logger.LogInformation("WithLayer..."); result = await this.ProcessProtoMessage(session, message.DoClone(inner_message), cancellationToken); } return(result); }
private async Task <object> ProcessMessage(IMTSession session, InnerMessage message, CancellationToken cancellationToken) { object result = null; ulong type = 0; try { type = BitConverter.ToUInt32(message.Content, 0); var req = message.Content.ToMTObject(); // session.Services.Serialization().Deserialize(message.Content); { this.logger.LogInformation(req.GetType().Name); var envelop = MessageContext.Create(req); envelop.container_seq_no(message.container_seq_no); envelop.container_msg_id(message.container_msg_id); envelop.AuthKey(message.AuthKey); envelop.msg_id(message.msg_id); envelop.seq_no(message.seq_no); var _ret = await serviceProvider.Bus().Send(envelop, cancellationToken: cancellationToken) as MTObject; if (_ret as MTObject != null) { result = new MTRpcResult { ReqMsgId = (long)message.msg_id, Result = new MTGzipPacked { PackedData = Utils.compress(_ret.ToByteArray()) } }; session.AddMessage(result as MTObject, true, true); } } } catch (Exception err) { if (err is InvalidDataException) { this.logger.LogWarning( $"Invalid or Not Supported Constructor: '{type.ToString("X4")}'"); } else { this.logger.LogError($"An error occured while trying to process this message: {err}"); } } return(result); }
private async Task <object> ProcessAuthSendCode(IMTSession session, InnerMessage message) { var result = await Task.FromResult <object>(null); using (var stream = new MemoryStream(message.Content)) using (var reader = new BinaryReader(stream)) { var o = session.Services.Serialization().Deserialize(message.Content) as MTObject; //o.ToByteArray(); //var type = reader.ReadUInt32(); //var flags = reader.ReadUInt32(); //var phone = StringSerializer.Deserialize(reader); /// /// auth.sentCode#5e002502 flags:# type:auth.SentCodeType phone_code_hash:string next_type:flags.1?auth.CodeType timeout:flags.2?int = auth.SentCode; /// this.logger.LogInformation("************************SendCode"); var hash = new byte[8]; new Random().NextBytes(hash); var code = "1234"; //this.SentCode = true; var sent_code = new TLSentCode() { Flags = 4, PhoneCodeHash = "phonehash", Type = new TLSentCodeTypeSms { Length = 4, }, Timeout = 40000 }; var ss = sent_code.ToByteArray(); result = new MTRpcResult { ReqMsgId = (long)message.msg_id, Result = new MTGzipPacked { PackedData = Utils.compress(sent_code.ToByteArray()) } }; session.AddMessage(result as MTObject, false, true); } return(result); }
public IMTSession GetSessionByUserId(int userId) { IMTSession result = this.sessions.Values.FirstOrDefault(x => x.Data.UserId == userId); if (result == null) { var data = this.sessionStore.GetByUserId(userId); if (data == null) { throw new Exception($"Session not found. UserId:{userId}"); } result = this.GetSession(data.AuthKeyId); //result = this.provider.GetService<MTSession>() // .Initialize(data); } return(result); }
private async Task <Tuple <int, byte[]> > GenerateResponse(IMTSession session) { byte[] repl = new byte[] { }; var pendings = (await session.GetPending()).ToArray(); if (session.GetUserId() == 2 && POSTED > 0) { (await session.GetPending()).ToArray(); } var count = pendings.Count(); //this.logger.LogInformation($"*** Generating Responses Pending count: {pendings.Length}"); using (var memory = new MemoryStream()) using (var w = new BinaryWriter(memory)) { w.Write(0x73f1f8dc); //Message Container w.Write(pendings.Length); foreach (var item in pendings) { using (var _memory = new MemoryStream()) using (var _w = new BinaryWriter(_memory)) { _w.Write(item.MsgId); _w.Write(item.Seqno); _w.Write(item.Bytes); _w.Write(item.Body.PackedData); _w.Close(); w.Write(_memory.ToArray()); } } w.Close(); if (session.GetUserId() == 2 && pendings.Count() > 1) { } repl = await Task.FromResult(memory.ToArray()); return(new Tuple <int, byte[]>(count, repl)); } }
private async Task <object> ProcessInitConnection(IMTSession session, InnerMessage message, CancellationToken cancellationToken) { var result = await Task.FromResult <object>(null); using (var stream = new MemoryStream(message.Content)) using (var reader = new BinaryReader(stream)) { var type = reader.ReadUInt32(); var appId = reader.ReadUInt32(); var device_info = StringSerializer.Deserialize(reader); var os_info = StringSerializer.Deserialize(reader); var app_version = StringSerializer.Deserialize(reader); var system_language = StringSerializer.Deserialize(reader); var system_language_pack = StringSerializer.Deserialize(reader); var lang_code = StringSerializer.Deserialize(reader); var remain = reader.ReadAllBytes(); this.logger.LogInformation("Init..."); result = await this.ProcessProtoMessage(session, message.DoClone(remain), cancellationToken); } return(result); }
public IMTSession GetSession(ulong?AuthKeyId) { IMTSession result = null; if (!AuthKeyId.HasValue) { return(result); } result = this.sessions.GetOrAdd(AuthKeyId.Value, id => { using (var store = this.provider.GetService <ISessionStore>()) { var data = store.GetByAuthId(AuthKeyId.Value); if (data == null) { return(null); } return(this.provider.GetService <MTSession>() .Initialize(data)); } }); return(result); }
private async Task <object> ProcessProtoMessage(IMTSession session, InnerMessage message, CancellationToken cancellationToken) { var type = BitConverter.ToUInt32(message.Content, 0); var result = await Task.FromResult <object>(null); try { switch (type) { case 0xcb9f372d: { // Invoke after Message await this.ProcessInvokeAftert(session, message, cancellationToken); } break; case 0x73f1f8dc: // container await this.ProcessContainer(session, message, cancellationToken); break; case 0xc7481da6: // init connection result = await this.ProcessInitConnection(session, message, cancellationToken); break; case 0xda9b0d0d: result = await this.ProcessInitInvokeWithLayer(session, message, cancellationToken); break; case 0x86aef0ec: result = await this.ProcessAuthSendCode(session, message); break; case 0x62d6b459: result = await this.ProcessAck(session, message); break; case 0x9299359f: /// Http Wait /// { await this.ProcessHttpWait(session, message); } break; default: await this.ProcessMessage(session, message, cancellationToken); break; } } catch (Exception err) { this.logger.LogError( $"===============================================>Unknown Request: '{type.ToString("X4")}'"); } return(result); }
public SessionMessageQueue(IMTSession session) { //this.UserId = session.GetUserId(); this.session = session; }