public ContactSessionWindow(ContactSession session, MessagePack messagePack, bool openFromDesktopBuddy) : this(new SessionTabItem(session)) { if (messagePack != null) { Buddy sender = ComponentManager.GetBuddyListBuilder().FindOrCreateBuddy(session.Uin, true); this.CurrentIMSession.AddMsg(sender, messagePack.Header.Time, messagePack); } }
public void AddMsg(InstanceAnswerPro.Core.Buddy sender, DateTime time, MessagePack messagePack) { IMMessage message = new IMMessage(sender, time, messagePack); this.imMessageList.Add(message); if (this.MessageAdded != null) { this.MessageAdded(this, null); } }
public static void AnalyzeUrl(TextBlock textBlock, string messageBoxText) { if (string.IsNullOrEmpty(messageBoxText)) { textBlock.Text = messageBoxText; } else { Paragraph paragraph = new Paragraph(); AddNormalText(paragraph.Inlines, messageBoxText); MessagePack msgPack = new MessagePack(); InputBox.ParseInlines(msgPack, paragraph.Inlines); MessagePack pack2 = new MessagePack(CoreMessenger.Instance.MsgStorage.TransformMsg(msgPack.Key)); uint elemCount = pack2.GetElemCount(); for (uint i = 0; i < elemCount; i++) { MessageElement elem = pack2.GetElem(i); if (elem.Category == MsgPackCat.ELEMTYPE_TEXT) { Guid guid; Guid guid2; TextElement element2 = (TextElement)elem; string text = element2.GetText(); string url = element2.GetUrl(out guid, out guid2); if (string.IsNullOrEmpty(url)) { AddNormalText(textBlock.Inlines, text); } else { AddLinkText(textBlock.Inlines, text, new HyperLinkObj_Url(url)); } } } } }
public static void ParseInlineUIContainer(MessagePack msgPack, InlineUIContainer inlineUIContainer) { ImageEx child = inlineUIContainer.Child as ImageEx; if (child != null) { ParseImage(msgPack, child); } }
private void ParseSpan(MessagePack msgPack, Span span) { this.ParseInlines(msgPack, span.Inlines); }
private void ParseRun(MessagePack msgPack, System.Windows.Documents.Run run) { string text = run.Text; if (!string.IsNullOrEmpty(text)) { ((InstanceAnswerPro.Core.TextElement)msgPack.CreateElement(MsgPackCat.ELEMTYPE_TEXT)).SetText(text); } }
private void ParseLineBreak(MessagePack msgPack, LineBreak lineBreak) { ((InstanceAnswerPro.Core.TextElement)msgPack.CreateElement(MsgPackCat.ELEMTYPE_TEXT)).SetText("\r\n"); }
/// <summary> /// 接收消息 /// </summary> /// <returns></returns> protected MessagePack ReceiveMessage() { MessagePack messageData = null; try { byte[] headerBytes = SocketReceive(this._tcpClient, messageHeaderLength); if (headerBytes == null || headerBytes.Length == 0) return null; messageData = new MessagePack(headerBytes); this.logHelper.LogInfoMsg("收到Message,msgType={0},BodyLength={1},threadId={2}", messageData.Header.Type, messageData.Header.BodyLength, Thread.CurrentThread.ManagedThreadId); //if (!Enum.IsDefined(typeof(MsgType), (int)messageData.Header.Type)) // return null; if (messageData.Header.BodyLength > 2000) { this.logHelper.LogWarnMsg("数据包头中指示的数据长度异常"); this.tcpDisconnectedEvent.Set(); return null; } if (messageData.Header.BodyLength > 0) { messageData.BodyData = SocketReceive(this._tcpClient, (int)messageData.Header.BodyLength); messageData.TrailerData = SocketReceive(this._tcpClient, tailSize); string msg; if (messageData.Validate(out msg)) { return messageData; } else { this.logHelper.LogWarnMsg("验证消息:{0},Message:\r\n{1}", msg, messageData.ToLogString()); return null; } } else if (messageData.Header.Type == (UInt32)MsgType.Heartbeat) { messageData.TrailerData = SocketReceive(this._tcpClient, tailSize); return messageData; } else return null; } catch (Exception ex) { this.logHelper.LogWarnMsg(ex, string.Format("行情网关连接断开,IP={0},Port={1}", this._ip, this._port)); this.tcpDisconnectedEvent.Set(); return null; } }
public static void ParseImage(MessagePack msgPack, ImageEx image) { EmoticonItem tag = image.Tag as EmoticonItem; if (tag.IsSysEmoticon) { SysFaceElement element = (SysFaceElement)msgPack.CreateElement(MsgPackCat.ELEMTYPE_SYSFACE); element.Index = Convert.ToByte(tag.Id); } else { ImageElement element2 = (ImageElement)msgPack.CreateElement(MsgPackCat.ELEMTYPE_IMAGE); element2.Path = "OSRoot:" + tag.Fileorg; ImageElementNum++; } }
public static void ParseSpan(MessagePack msgPack, Span span) { ParseInlines(msgPack, span.Inlines); }
/// <summary> /// 创建消息包 /// </summary> /// <returns></returns> public MessagePack CreateMessagePack() { MessagePack msgPack = new MessagePack { Header = { FontName = this.InputBox.FontFamily.ToString(), FontSize = (byte)(this.InputBox.FontSize - 3.0) } }; SolidColorBrush foreground = this.InputBox.Foreground as SolidColorBrush; if (foreground != null) { msgPack.Header.FontColor = foreground.Color; } byte num = 0; if (this.InputBox.FontWeight == FontWeights.Bold) { num = (byte)(num | 1); } if (this.InputBox.FontStyle == FontStyles.Italic) { num = (byte)(num | 2); } if (true)//Underline { num = (byte)(num | 4); } msgPack.Header.FontEffect = num; if (!string.IsNullOrEmpty(this.InputBox.Text)) { ((TextElement)msgPack.CreateElement(MsgPackCat.ELEMTYPE_TEXT)).SetText(this.InputBox.Text); } return new MessagePack(CoreMessenger.Instance.MsgStorage.TransformMsg(msgPack.Key)); }
private void MessageReceived(byte[] message) { MessagePack mp = (Blockchain.ByteArrayToObject(message)) as MessagePack; if (mp.Text != null) { string[] tokens = mp.Text.Split(new string[] { @";" }, StringSplitOptions.None); string sender = (tokens[0] == userName) ? "me" : "other"; string function = tokens[1]; switch (function) { case "request": if (sender == "me") { string hash = chain.Chain[chain.Chain.Count() - 1].Hash; mp.Text = "Nobody" + ";requestUpdate;" + hash + ";" + userName; //mp.Val.Add(hash); //mp.Val.Add(length.ToString()); byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } break; case "requestUpdate": if (sender == "other") { string hash = chain.Chain[chain.Chain.Count() - 1].Hash; if (hash != tokens[2]) { mp.Text = userName + ";peerReady;" + tokens[3] + ";" + hash; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } } break; case "peerReady": peerNames.Add(tokens[0]); if (sender == "other" && tokens[2] == userName && peerNames[0] == tokens[0]) { int length = chain.Chain.Count(); string hash = chain.Chain[chain.Chain.Count() - 1].Hash; if (hash != tokens[3] && length != 1) { mp.Text = userName + ";chainRequest;" + tokens[0] + ";" + hash; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } else if (hash == tokens[3]) { MessageBox.Show("Czy padany łańcuch jest poprawny: " + chain.IsValid().ToString()); if (chain.IsValid() == false || chain.Chain.Count() == 0) { chain = new Blockchain(); chain.AddGenesisBlock(); hash = chain.Chain[chain.Chain.Count() - 1].Hash; mp.Text = "Nobody" + ";requestUpdate;" + hash + ";" + userName; //mp.Val.Add(hash); //mp.Val.Add(length.ToString()); byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } } else { rtbChat.Text = null; chain = new Blockchain(); mp.Text = userName + ";chainRequest;" + tokens[0] + ";" + hash; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } } break; case "chainRequest": if (sender == "other" && tokens[2] == userName) { if (chain.Chain.Find(i => i.Hash == tokens[3]) != null) { Block lastBlock = chain.Chain.Find(i => i.Hash == tokens[3]); int startIndex = lastBlock.Index + 1; int finishIndex = lastBlock.Index + 10; int lastIndex = chain.Chain[chain.Chain.Count() - 1].Index; List <Block> newBlocks = new List <Block>(); if ((lastIndex - lastBlock.Index) < 10) { finishIndex = lastIndex + 1; } for (int i = startIndex; i < finishIndex; i++) { newBlocks.Add(chain.Chain[i]); } mp.Text = userName + ";newBlockchain;" + tokens[0]; mp.listOfBlocks = newBlocks; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } else { List <Block> newBlocks = new List <Block>(); for (int i = 0; i < 10; i++) { newBlocks.Add(chain.Chain[i]); } mp.Text = userName + ";newBlockchain;" + tokens[0]; mp.listOfBlocks = newBlocks; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } } break; case "newBlockchain": if (sender == "other" && tokens[2] == userName) { foreach (Block el in mp.listOfBlocks) { chain.Chain.Add(el); rtbChat.Text += el.Data + "\n"; } string hash = chain.Chain[chain.Chain.Count() - 1].Hash; mp.Text = "Nobody" + ";requestUpdate;" + hash + ";" + userName; //mp.Val.Add(hash); //mp.Val.Add(length.ToString()); byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); Blockchain.SerializeToJson(chain); peerNames.Clear(); } break; } } else { chain.AddBlock(mp.SmObj); rtbChat.Text += chain.Chain[chain.Chain.Count() - 1].Data + "\n"; Blockchain.SerializeToJson(chain); } }
public void Execute(MessagePack <T> message) => base.Execute((h, m) => h.Invoke(m), message);
public void SendMessage(MessagePack messagePack) { if (this._contactSession != null) { this._contactSession.SendMessage(messagePack); } else if (this._communitySession != null) { this._communitySession.SendMessage(messagePack); } }
private void ProcessMessage(MessagePack msg) { throw new NotImplementedException(); }
public static void ParseLineBreak(MessagePack msgPack, LineBreak lineBreak) { ((TextElement)msgPack.CreateElement(MsgPackCat.ELEMTYPE_TEXT)).SetText("\r\n"); }
public static void ParseRun(MessagePack msgPack, Run run) { string text = run.Text; if (!string.IsNullOrEmpty(text)) { ((TextElement)msgPack.CreateElement(MsgPackCat.ELEMTYPE_TEXT)).SetText(text); } }
private void ParseInlines(MessagePack msgPack, InlineCollection inlines) { foreach (Inline inline in inlines) { if (inline is System.Windows.Documents.Run) { this.ParseRun(msgPack, (System.Windows.Documents.Run)inline); } else if (inline is Span) { this.ParseSpan(msgPack, (Span)inline); } else if (inline is LineBreak) { this.ParseLineBreak(msgPack, (LineBreak)inline); } else if (inline is InlineUIContainer) { this.ParseInlineUIContainer(msgPack, (InlineUIContainer)inline); } } }
public MessagePack CreateMessagePack(out int imageCount) { ImageElementNum = 0; imageCount = 0; //if (this.IsEmpty()==true) //{ // return null; //} BlockCollection blocks = base.Document.Blocks; //if (blocks.Count == 0) //{ // return null; //} MessagePack msgPack = new MessagePack { Header = { FontName = base.Document.FontFamily.ToString(), FontSize = (byte)(base.Document.FontSize - 3.0) } }; SolidColorBrush foreground = base.Document.Foreground as SolidColorBrush; if (foreground != null) { msgPack.Header.FontColor = foreground.Color; } byte num = 0; if (base.Document.FontWeight == FontWeights.Bold) { num = (byte)(num | 1); } if (base.Document.FontStyle == FontStyles.Italic) { num = (byte)(num | 2); } if (this.Underline) { num = (byte)(num | 4); } msgPack.Header.FontEffect = num; bool flag = false; foreach (Block block in blocks) { if (flag) { ((TextElement)msgPack.CreateElement(MsgPackCat.ELEMTYPE_TEXT)).SetText("\r\n"); } else { flag = true; } if (block is Paragraph) { Paragraph paragraph = block as Paragraph; ParseInlines(msgPack, paragraph.Inlines); } else if (block is BlockUIContainer) { BlockUIContainer container = block as BlockUIContainer; ImageEx child = container.Child as ImageEx; if (child != null) { ParseImage(msgPack, child); } } } ITXMsgPack xcdbca = CoreMessenger.Instance.MsgStorage.TransformMsg(msgPack.Key); imageCount = ImageElementNum; ImageElementNum = 0; return new MessagePack(xcdbca); }
private void ParseInlineUIContainer(MessagePack msgPack, InlineUIContainer inlineUIContainer) { ImageEx child = inlineUIContainer.Child as ImageEx; if (child != null) { this.ParseImage(msgPack, child); } }
public static void ParseInlines(MessagePack msgPack, InlineCollection inlines) { foreach (Inline inline in inlines) { if (inline is Run) { ParseRun(msgPack, (Run)inline); } else if (inline is Span) { ParseSpan(msgPack, (Span)inline); } else if (inline is LineBreak) { ParseLineBreak(msgPack, (LineBreak)inline); } else if (inline is InlineUIContainer) { ParseInlineUIContainer(msgPack, (InlineUIContainer)inline); } } }
async void UpdateShipAvatars() { try { var rRequest = WebRequest.CreateHttp("http://heavenlywind.cc/api/ship_avatars"); rRequest.UserAgent = ProductInfo.UserAgent; var rPath = Path.Combine(ProductInfo.RootDirectory, "Resources", "Avatars", "Ships") + "\\"; var rDirectory = new DirectoryInfo(rPath); if (!rDirectory.Exists) { rDirectory.Create(); } var rFiles = Directory.EnumerateFiles(rPath, "*.png").ToArray(); if (rFiles.Length == 0) { rRequest.Method = "GET"; } else { rRequest.Method = "POST"; var rData = new SortedList <int, int>(rFiles.Length); foreach (var rFile in rFiles) { var rMatch = r_ShipAvatarRegex.Match(Path.GetFileName(rFile)); if (!rMatch.Success) { continue; } var rShip = int.Parse(rMatch.Groups[1].Value); int rType; rData.TryGetValue(rShip, out rType); rType += rMatch.Groups[2].Value == "n" ? 1 : 2; rData[rShip] = rType; } using (var rStream = await rRequest.GetRequestStreamAsync()) { var rSerializer = new JsonSerializer(); var rWriter = new JsonTextWriter(new StreamWriter(rStream)); rSerializer.Serialize(rWriter, rData); await rWriter.FlushAsync(); } } using (var rResponse = await rRequest.GetResponseAsync()) using (var rStream = rResponse.GetResponseStream()) { var rResult = (IDictionary <object, object>)MessagePack.Unpack(new MessagePackReader(rStream)); foreach (var rInfo in rResult) { var rData = rInfo.Value as object[]; if (rData == null || rData.Length == 0) { continue; } var rShip = rInfo.Key.ToString(); if (rData[0] != null) { File.WriteAllBytes(rPath + rShip + "_n.png", (byte[])rData[0]); } if (rData.Length > 1 && rData[1] != null) { File.WriteAllBytes(rPath + rShip + "_d.png", (byte[])rData[1]); } } } } catch (Exception e) { Logger.Write(LoggingLevel.Error, string.Format(StringResources.Instance.Main.Log_CheckForUpdate_Exception, e.Message)); } }