private async Task SendPrintUplinkMessageAsync(Random random) { SmsServerSessionlist = GetSmsServerSessions(); for (int i = 0; i < SmsServerSessionlist.Count; i++) { var session = SmsServerSessionlist[i]; SmgpMessageDeliver deliver = new SmgpMessageDeliver() { Id = Guid.NewGuid().ToByteArray().Take(10).ToArray(), MessageConent = "content" + String.Format("_{0:00000}", random.Next(1000000)), Format = (byte)SmgpEncodings.GBK, ReportRequired = 0,//up link ReceiveTime = DateTime.Now.ToString("yyyyMMddHHmmss"), SenderId = "18101870165", ReceiverId = session.Server.Configurations.ServiceID, Reserved = String.Empty }; int[] digit = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; string mobile = String.Empty; StringBuilder sbMobile = new StringBuilder("181"); while (true) { sbMobile.Append(digit[random.Next(0, 8)]); mobile = sbMobile.ToString(); if (sbMobile.Length == 11) { deliver.SenderId = mobile; break; } } deliver.CalculateContentByteCount(SmgpEncodings.ASCII);// 计算上行短信内容的byte var result = await session.SendSmgpUplinkAsync(deliver); var sb = new StringBuilder(); if (result) { sb.AppendLine(); sb.AppendFormat("MsgContent: {0}; ", deliver.MessageConent); sb.AppendFormat("ReceiverId: {0}; ", deliver.ReceiverId); sb.AppendFormat("SenderId: {0}", deliver.SenderId); sb.AppendLine(); } Console.WriteLine(sb.ToString()); } ; }
private async Task DoReceiveDeliverAsync(SmgpMessageDeliver message) { if (message.ReportRequired == 0) //up link request { this.RaiseDeliverReceived(message); } else { var report = message.GetReport(); this.RaiseReportReceived(report); } var response = new SmgpMessageDeliverResponse() { SequenceId = message.SequenceId, Id = message.Id, Status = 0 }; await this.Send(response); }
public static void CalculateContentByteCount(this SmgpMessageDeliver message, SmgpEncodings encoding) { var byteCount = GetEncoding(encoding).GetByteCount(message.MessageConent); message.ContentByteCount = (byte)byteCount; }
protected override async Task DoNetworkMessageReceived(SmsServerSession session, NetworkMessage message) { if (message is SmgpMessageLogin) { var m = message as SmgpMessageLogin; var r = new SmgpMessageLoginResponse(); r.SequenceId = m.SequenceId; r.Version = m.Version; r.Signature = new byte[16]; r.Status = 0; await session.SendAsync(r); } else if (message is SmgpMessageSubmit) { var m = message as SmgpMessageSubmit; var r = new SmgpMessageSubmitResponse(); r.SequenceId = m.SequenceId; r.Status = 0; r.Id = Guid.NewGuid().ToByteArray().Take(10).ToArray(); await session.SendAsync(r); var time = DateTime.UtcNow; var report = new SmgpMessageReport(); report.Id = r.Id; report.Status = "DELIVRD"; report.SubmitTime = time.ToString("yyMMddHHmm"); report.CompleteTime = time.ToString("yyMMddHHmm"); report.Text = Encoding.GetEncoding("gb2312").GetBytes(m.Content); report.Error = "001"; //err report.Submited = "001"; //sub report.Delivered = "001"; //dkvrd; var d = new SmgpMessageDeliver() { SequenceId = this.SequenceId, Id = r.Id, Format = (byte)SmgpEncodings.GBK, ReceiverId = m.ReceiverIds[0], ReportRequired = 1,//IsReport SenderId = m.SenderId, ReceiveTime = time.ToString("yyyyMMddHHmmss") }; d.SetReport(report); await session.SendAsync(d); lock (this) { _submitCount++; var stamp = DateTime.Now; if (_startStamp == DateTime.MinValue) { _startStamp = stamp; } _endStamp = stamp; var tick = (long)(_endStamp - _startStamp).TotalSeconds; if (tick != _reportTick) { _reportTick = tick; Console.WriteLine("{0}: {1}...", _submitCount, stamp.ToLongTimeString()); } } } else if (message is SmgpMessageActiveTest) { var m = message as SmgpMessageActiveTest; var r = new SmgpMessageActiveTestResponse(); r.SequenceId = m.SequenceId; await session.SendAsync(r); } }
public static async Task <bool> SendSmgpUplinkAsync(this SmsServerSession session, SmgpMessageDeliver deliver) { if (session == null) { return(false); } try { deliver.SequenceId = session.SequenceId; return(await session.SendAsync(deliver)); } catch (Exception) { return(false); } }
public NetworkMessage CreateNetworkMessage(BinaryReader reader) { uint byteCount = reader.NetworkReadUInt32(); SmgpCommands command = (SmgpCommands)reader.NetworkReadUInt32(); uint sequenceId = reader.NetworkReadUInt32(); SmgpMessage message = null; switch (command) { case SmgpCommands.Login: message = new SmgpMessageLogin(); break; case SmgpCommands.LoginResponse: message = new SmgpMessageLoginResponse(); break; case SmgpCommands.Submit: message = new SmgpMessageSubmit(); break; case SmgpCommands.SubmitResponse: message = new SmgpMessageSubmitResponse(); break; case SmgpCommands.Deliver: message = new SmgpMessageDeliver(); break; case SmgpCommands.DeliverResponse: message = new SmgpMessageDeliverResponse(); break; case SmgpCommands.ActiveTest: message = new SmgpMessageActiveTest(); break; case SmgpCommands.ActiveTestResponse: message = new SmgpMessageActiveTestResponse(); break; case SmgpCommands.Exit: message = new SmgpMessageExit(); break; case SmgpCommands.ExitResponse: message = new SmgpMessageExitResponse(); break; default: message = new SmgpMessage(); break; } //end switch if (message != null) { message.Command = command; message.ByteCount = byteCount; message.SequenceId = sequenceId; message.NetworkRead(reader); } return(message); }