public static byte[] ProtoToByteArray(IMessage message) { int size = message.CalculateSize(); byte[] buffer = new byte[size]; CodedOutputStream output = new CodedOutputStream(buffer); message.WriteTo(output); return buffer; }
public void SendResponse(uint token, IMessage response) { Header header = new Header(); header.Token = token; header.ServiceId = 0xFE; header.Size = (uint)response.CalculateSize(); var headerSizeBytes = BitConverter.GetBytes((ushort)header.CalculateSize()); Array.Reverse(headerSizeBytes); ByteBuffer packet = new ByteBuffer(); packet.WriteBytes(headerSizeBytes, 2); packet.WriteBytes(header.ToByteArray()); packet.WriteBytes(response.ToByteArray()); AsyncWrite(packet.GetData()); }
public override bool Encode() { try { int packetSize = message.CalculateSize(); Buffer = new byte[sizeof(int) * 3 + packetSize]; BitConverter.GetBytes((int)packetSize).CopyTo(Buffer, sizeof(int) * 0); BitConverter.GetBytes((int)Type).CopyTo(Buffer, sizeof(int) * 1); BitConverter.GetBytes((int)ProcessType).CopyTo(Buffer, sizeof(int) * 2); message.ToByteArray().CopyTo(Buffer, sizeof(int) * 3); } catch (Exception) { return(false); } return(true); }
public async void SendMsg(Int32 cmd, IMessage msg) { int size = msg.CalculateSize(); byte[] msgBuf = msg.ToByteArray(); Int32 sendMsgLen = IPAddress.HostToNetworkOrder(size); Int32 sendCmd = IPAddress.HostToNetworkOrder(cmd); byte[] msgLenBuf = BitConverter.GetBytes(sendMsgLen); byte[] msgCmdBuf = BitConverter.GetBytes(sendCmd); // 注意, 这里可能会有问题! // 有可能会发不过去! await self.Send(msgLenBuf, 0, msgLenBuf.Length); await self.Send(msgCmdBuf, 0, msgCmdBuf.Length); await self.Send(msgBuf, 0, size); }
/// <summary> /// Serializes the <paramref name="message"/>, appends suffix and returns the hash of it. /// </summary> /// <param name="provider">The hash provider.</param> /// <param name="message">The protocol message.</param> /// <param name="suffix">The suffix that should be appended to the message. </param> /// <returns></returns> public static MultiHash ComputeMultiHash(this IHashProvider provider, IMessage message, byte[] suffix) { ProtoPreconditions.CheckNotNull(message, nameof(message)); var calculateSize = message.CalculateSize(); var required = calculateSize + suffix.Length; var array = ArrayPool <byte> .Shared.Rent(required); using (var output = new CodedOutputStream(array)) { message.WriteTo(output); } suffix.CopyTo(array, calculateSize); var result = provider.ComputeMultiHash(array, 0, required); ArrayPool <byte> .Shared.Return(array); return(result); }
public static bool Verify(this ICryptoContext crypto, ISignature signature, IMessage message, IMessage context) { ProtoPreconditions.CheckNotNull(message, nameof(message)); ProtoPreconditions.CheckNotNull(context, nameof(context)); var messageSize = message.CalculateSize(); var contextSize = context.CalculateSize(); var array = ArrayPool <byte> .Shared.Rent(messageSize + contextSize); using (var output = new CodedOutputStream(array)) { message.WriteTo(output); context.WriteTo(output); } var result = crypto.Verify(signature, array.AsSpan(0, messageSize), array.AsSpan(messageSize, contextSize)); ArrayPool <byte> .Shared.Return(array); return(result); }
public void Send(IMessage packet) { // 패킷 이름을 이용해서 ID를 정함 string msgName = packet.Descriptor.Name.Replace("_", string.Empty); // S_Chat => SChat MsgId msgId = (MsgId)Enum.Parse(typeof(MsgId), msgName); ushort size = (ushort)packet.CalculateSize(); byte[] sendBuffer = new byte[size + 4]; Array.Copy(BitConverter.GetBytes((ushort)(size + 4)), 0, sendBuffer, 0, sizeof(ushort)); Array.Copy(BitConverter.GetBytes((ushort)msgId), 0, sendBuffer, 2, sizeof(ushort)); Array.Copy(packet.ToByteArray(), 0, sendBuffer, 4, size); lock (_lock) { // 예약만하고 보내지는 않음 _reserveQueue.Add(sendBuffer); _reservedSendBytes += sendBuffer.Length; } }
/// <summary> /// 发送protobuf对象 /// </summary> /// <param name="cmd">命令号</param> /// <param name="sendMessage">protobuf对象</param> /// <returns>已发送长度</returns> public Int32 SendProtoMessage(Int32 cmd, IMessage message) { try { if (_Client.Client == null || !_Client.Client.Connected) { Close(); Connect(); } // 组装发送数据 int sendLen = message.CalculateSize(); var sendBuffer = new Byte[8 + sendLen]; // 4个字节的包长 var headBuffer = BitConverter.GetBytes(sendLen); Buffer.BlockCopy(headBuffer, 0, sendBuffer, 0, headBuffer.Length); // 4个字节的CMDID headBuffer = BitConverter.GetBytes((Int32)cmd); Buffer.BlockCopy(headBuffer, 0, sendBuffer, 4, headBuffer.Length); // 数据体 var buffer = new Byte[sendLen]; using (CodedOutputStream cos = new CodedOutputStream(buffer)) { message.WriteTo(cos); } Buffer.BlockCopy(buffer, 0, sendBuffer, 8, sendLen); // 发包 Int32 sendOKLen = _Client.Client.Send(sendBuffer); return(sendOKLen); } catch (Exception ex) { return(0); } }
public void SendRequest(uint serviceHash, uint methodId, IMessage request) { Header header = new Header(); header.ServiceId = 0; header.ServiceHash = serviceHash; header.MethodId = methodId; header.Size = (uint)request.CalculateSize(); header.Token = _requestToken++; var headerSizeBytes = BitConverter.GetBytes((ushort)header.CalculateSize()); Array.Reverse(headerSizeBytes); ByteBuffer packet = new ByteBuffer(); packet.WriteBytes(headerSizeBytes, 2); packet.WriteBytes(header.ToByteArray()); packet.WriteBytes(request.ToByteArray()); AsyncWrite(packet); }
/// <summary> /// Serializes the <paramref name="message"/> returns the hash of it. /// </summary> /// <param name="provider">The hash provider.</param> /// <param name="message">The protocol message.</param> /// <returns></returns> public static MultiHash ComputeMultiHash(this IHashProvider provider, IMessage message) { ProtoPreconditions.CheckNotNull(message, nameof(message)); var required = message.CalculateSize(); var array = ArrayPool <byte> .Shared.Rent(required); try { using (var output = new CodedOutputStream(array)) { message.WriteTo(output); } var result = provider.ComputeMultiHash(array, 0, required); return(result); } finally { ArrayPool <byte> .Shared.Return(array); } }
private byte[] ObjectToByteBuffer(int descriptorId, object obj) { IMessage u = (IMessage)obj; int size = u.CalculateSize(); byte[] bytes = new byte[size]; CodedOutputStream cos = new CodedOutputStream(bytes); u.WriteTo(cos); cos.Flush(); WrappedMessage wm = new WrappedMessage(); wm.WrappedMessageBytes = ByteString.CopyFrom(bytes); wm.WrappedDescriptorId = descriptorId; byte[] msgBytes = new byte[wm.CalculateSize()]; CodedOutputStream msgCos = new CodedOutputStream(msgBytes); wm.WriteTo(msgCos); msgCos.Flush(); return(msgBytes); }
public int CalculateSize() { return(Content.CalculateSize() + 4 + Content.GetType().FullName.Length); }
/// <summary> /// Computes the number of bytes that would be needed to encode a /// group field, including the tag. /// </summary> public static int ComputeGroupSize(IMessage value) { return(value.CalculateSize()); }
/// <summary> /// Computes the number of bytes that would be needed to encode an /// embedded message field, including the tag. /// </summary> public static int ComputeMessageSize(IMessage value) { int size = value.CalculateSize(); return(ComputeLengthSize(size) + size); }
public static void WriteMessage(ref WriteContext ctx, IMessage value) { WritingPrimitives.WriteLength(ref ctx.buffer, ref ctx.state, value.CalculateSize()); WriteRawMessage(ref ctx, value); }
/// <summary> /// Writes a message, without a tag, to the stream. /// The data is length-prefixed. /// </summary> /// <param name="value">The value to write</param> public void WriteMessage(IMessage value) { WriteLength(value.CalculateSize()); value.WriteTo(this); }
public static int ComputeMessageSize(IMessage value) { int num = value.CalculateSize(); return(CodedOutputStream.ComputeLengthSize(num) + num); }
/// <summary> /// Determines whether this instance is empty. /// </summary> /// <param name="message">The message.</param> /// <returns> /// <c>true</c> if the specified message is empty; otherwise, <c>false</c>. /// </returns> public static bool IsEmpty(this IMessage message) { ProtoPreconditions.CheckNotNull(message, nameof(message)); return(message.CalculateSize() == 0); }
static void testProtocol(IMessage message) { byte[] arrayByte = new byte[message.CalculateSize()]; message.WriteTo(new Google.Protobuf.CodedOutputStream(arrayByte)); }
/// <summary> /// Computes the number of bytes that would be needed to encode a /// group field, including the tag. /// </summary> public static int ComputeGroupSize(IMessage value) { return value.CalculateSize(); }
internal void Send(IMessage envelope) { m_OutputWriter.Write((int)envelope.CalculateSize()); m_OutputWriter.Write(envelope.ToByteArray()); m_OutputWriter.Flush(); }
/// <summary> /// Computes the number of bytes that would be needed to encode an /// embedded message field, including the tag. /// </summary> public static int ComputeMessageSize(IMessage value) { int size = value.CalculateSize(); return ComputeLengthSize(size) + size; }