-
Notifications
You must be signed in to change notification settings - Fork 16
/
PublishedMessage.cs
99 lines (88 loc) · 2.42 KB
/
PublishedMessage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using Ipfs;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PeerTalk.PubSub
{
/// <summary>
/// A published messaged for a topic(s).
/// </summary>
/// <seealso ref="https://github.com/libp2p/specs/blob/master/pubsub/README.md"/>
/// <remarks>
/// TODO: Sender should really be called Author.
///
/// </remarks>
[ProtoContract]
public class PublishedMessage : IPublishedMessage
{
string messageId;
/// <inheritdoc />
public Peer Sender { get; set; }
/// <summary>
/// Who sent the the message.
/// </summary>
public Peer Forwarder {get; set; }
[ProtoMember(1)]
byte[] From
{
get
{
return Sender?.Id.ToArray();
}
set
{
Sender = new Peer { Id = new MultiHash(value) };
}
}
/// <inheritdoc />
[ProtoMember(4)]
public IEnumerable<string> Topics { get; set; }
/// <inheritdoc />
[ProtoMember(3)]
public byte[] SequenceNumber { get; set; }
/// <inheritdoc />
[ProtoMember(2)]
public byte[] DataBytes { get; set; }
/// <inheritdoc />
public Stream DataStream
{
get
{
return new MemoryStream(DataBytes, false);
}
}
/// <summary>>
/// NOT SUPPORTED, use <see cref="MessageId"/>.
/// </summary>
/// <exception cref="NotSupportedException">
/// A published message does not have a content id.
/// </exception>
public Cid Id => throw new NotSupportedException();
/// <summary>
/// A universally unique id for the message.
/// </summary>
/// <value>
/// The sender's ID concatenated with the <see cref="SequenceNumber"/>.
/// </value>
public string MessageId
{
get
{
if (messageId == null)
{
messageId = Sender.Id.ToBase58() + SequenceNumber.ToHexString();
}
return messageId;
}
}
/// <inheritdoc />
public long Size
{
get { return DataBytes.Length; }
}
}
}