Cleaning up PubSubMessages, as types messages instead of PubSubMetaData untypes dictionary
This commit is contained in:
1 parent
2e0e0658c6
commit
d32d5ef40c
12 files changed
+682
-262
No files matched your search
@@ -0,0 +1,177 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace bts {
|
||||
using BCG;
|
||||
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
|
||||
public interface IPubSubMessage {
|
||||
void serialize_into_str(Stream str);
|
||||
void consruct_deserialize(Stream str);
|
||||
}
|
||||
public class SubscribeMsg : IPubSubMessage {
|
||||
public string connection;
|
||||
public string subscribing_app;
|
||||
public string[] tags;
|
||||
public void serialize_into_str(Stream str) {
|
||||
BinaryWriter bw = new BinaryWriter(str);
|
||||
bw.Write(connection);
|
||||
bw.Write(subscribing_app);
|
||||
bw.Write(tags.Length);
|
||||
foreach (var t in tags)
|
||||
bw.Write(t);
|
||||
}
|
||||
|
||||
public void consruct_deserialize(Stream str) {
|
||||
BinaryReader br = new BinaryReader(str);
|
||||
this.connection = br.ReadString();
|
||||
this.subscribing_app = br.ReadString();
|
||||
var n_tags = br.ReadInt32();
|
||||
tags = new string[n_tags];
|
||||
for (var i = 0; i < n_tags; i++)
|
||||
tags[i] = br.ReadString();
|
||||
}
|
||||
public override string ToString() {
|
||||
return $"connection: {connection} subscribing_app: {subscribing_app} tags: {tags.Length}";
|
||||
}
|
||||
}
|
||||
|
||||
public class UnsubscribeMsg : SubscribeMsg {
|
||||
}
|
||||
|
||||
public class TransactionMsg : IPubSubMessage {
|
||||
uint n_msgs;
|
||||
public void serialize_into_str(Stream str) {
|
||||
BinaryWriter bw = new BinaryWriter(str);
|
||||
bw.Write(n_msgs);
|
||||
}
|
||||
public void consruct_deserialize(Stream str) {
|
||||
BinaryReader br = new BinaryReader(str);
|
||||
n_msgs = br.ReadUInt32();
|
||||
}
|
||||
}
|
||||
|
||||
public class PublishMsg : IPubSubMessage {
|
||||
public string connection;
|
||||
public string publishing_app;
|
||||
public string tag;
|
||||
public string tag_type;
|
||||
public bool is_compressed;
|
||||
// this is the size that of the msg, regarless of it being serialized or not
|
||||
public long publish_time_epoch_ms;
|
||||
public byte[] data_to_publish;
|
||||
|
||||
public void serialize_into_str(Stream str) {
|
||||
BinaryWriter bw = new BinaryWriter(str);
|
||||
bw.Write(connection);
|
||||
bw.Write(publishing_app);
|
||||
bw.Write(tag);
|
||||
bw.Write(tag_type);
|
||||
bw.Write(is_compressed);
|
||||
bw.Write(publish_time_epoch_ms);
|
||||
var sz = data_to_publish.Length;
|
||||
bw.Write(sz);
|
||||
bw.Write(data_to_publish);
|
||||
}
|
||||
public void consruct_deserialize(Stream str) {
|
||||
BinaryReader br = new BinaryReader(str);
|
||||
connection = br.ReadString();
|
||||
publishing_app = br.ReadString();
|
||||
tag = br.ReadString();
|
||||
tag_type = br.ReadString();
|
||||
is_compressed = br.ReadBoolean();
|
||||
publish_time_epoch_ms = br.ReadInt64();
|
||||
var size = br.ReadInt32();
|
||||
data_to_publish = br.ReadBytes(size);
|
||||
}
|
||||
|
||||
}
|
||||
public enum PubSubMessageType {
|
||||
SUBSCRIIBE = 0,
|
||||
UNSUBSCRIIBE,
|
||||
TRANSACTION,
|
||||
PUBLISH
|
||||
}
|
||||
|
||||
public class PubSubClientMessage : IPubSubMessage {
|
||||
public PubSubMessageType msg_type;
|
||||
// only one of these wil be non null
|
||||
public SubscribeMsg sub_msg;
|
||||
public UnsubscribeMsg unsub_msg;
|
||||
public TransactionMsg transaction_msg;
|
||||
public PublishMsg pub_msg;
|
||||
|
||||
public void serialize_into_str(Stream str) {
|
||||
BinaryWriter bw = new BinaryWriter(str);
|
||||
bw.Write((int)msg_type);
|
||||
switch (msg_type) {
|
||||
case PubSubMessageType.SUBSCRIIBE:
|
||||
sub_msg.serialize_into_str(str);
|
||||
break;
|
||||
case PubSubMessageType.UNSUBSCRIIBE:
|
||||
unsub_msg.serialize_into_str(str);
|
||||
break;
|
||||
case PubSubMessageType.TRANSACTION:
|
||||
transaction_msg.serialize_into_str(str);
|
||||
break;
|
||||
case PubSubMessageType.PUBLISH:
|
||||
pub_msg.serialize_into_str(str);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void consruct_deserialize(Stream str) {
|
||||
BinaryReader br = new BinaryReader(str);
|
||||
int type = br.ReadInt32();
|
||||
var msg_type = (PubSubMessageType)type;
|
||||
switch (msg_type) {
|
||||
case PubSubMessageType.SUBSCRIIBE:
|
||||
sub_msg = new SubscribeMsg();
|
||||
sub_msg.consruct_deserialize(str);
|
||||
break;
|
||||
case PubSubMessageType.UNSUBSCRIIBE:
|
||||
unsub_msg = new UnsubscribeMsg();
|
||||
unsub_msg.consruct_deserialize(str);
|
||||
break;
|
||||
case PubSubMessageType.TRANSACTION:
|
||||
transaction_msg = new TransactionMsg();
|
||||
transaction_msg.consruct_deserialize(str);
|
||||
break;
|
||||
case PubSubMessageType.PUBLISH:
|
||||
pub_msg = new PublishMsg();
|
||||
pub_msg.consruct_deserialize(str);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
public class PubSubServerMessage : IPubSubMessage {
|
||||
public string tag;
|
||||
public string tag_type;
|
||||
public byte[] data;
|
||||
public void serialize_into_str(Stream str) {
|
||||
BinaryWriter bw = new BinaryWriter(str);
|
||||
bw.Write(tag);
|
||||
bw.Write(tag_type);
|
||||
bw.Write(data.Length);
|
||||
bw.Write(data);
|
||||
}
|
||||
public void consruct_deserialize(Stream str) {
|
||||
BinaryReader br = new BinaryReader(str);
|
||||
tag = br.ReadString();
|
||||
tag_type = br.ReadString();
|
||||
var data_sz = br.ReadInt32();
|
||||
data = br.ReadBytes(data_sz);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user