public void SendBlob(string destID, int contractID, IMessage body, int blobSize = 1024) { List <MessagePacket> packets = new List <MessagePacket>(); byte[] buffer = body.ToByteArray(); int blobLength = buffer.Length / blobSize; if (blobLength != 0 && buffer.Length % blobLength > 0) { blobLength++; } int blobID = MessageIDCreater.GetNewID(); if (blobLength == 0) { BlobContract contract = new BlobContract() { BlobID = blobID, FragmentIndex = 0, Fragment = ByteString.CopyFrom(buffer), IsLast = true }; packets.Add(new MessagePacket(GloblParams.CurrentClientID, destID, GloblParams.CurrentClientType, MessageType.Blob, contractID, contract.ToByteArray())); } else { for (int i = 0; i < blobLength; i++) { int startIndex = i * blobSize; int endIndex = ((i + 1) * blobSize) > buffer.Length ? buffer.Length : ((i + 1) * blobSize); int length = endIndex - startIndex; byte[] sendBuffer = new byte[length]; System.Buffer.BlockCopy(buffer, startIndex, sendBuffer, 0, length); BlobContract contract = new BlobContract() { BlobID = blobID, FragmentIndex = i, Fragment = ByteString.CopyFrom(sendBuffer), IsLast = i == blobLength }; packets.Add(new MessagePacket(GloblParams.CurrentClientID, destID, GloblParams.CurrentClientType, MessageType.Blob, contractID, contract.ToByteArray())); } } messageBus.SendMessage(packets); }
public MessagePacket(string userID, string destID, ClientType clientType, byte messsageType, int contractID, byte[] body = null) { this.BodyContent = body; this.ContractID = contractID; int bodyLen = BodyContent == null ? 4 : BodyContent.Length + 4; this.MessageHeader = new MessageHeader() { Client = (byte)clientType, MessageBodyLen = bodyLen, MessageType = messsageType, MessageID = MessageIDCreater.GetNewID(), SourceUserID = userID, DestUserID = destID }; }