365 lines
13 KiB
C#
365 lines
13 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
|
|
namespace bts {
|
|
using BCG;
|
|
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
|
|
public interface IPubSubMessage {
|
|
void serialize_into_str(Stream str);
|
|
void consruct_deserialize(Stream str);
|
|
}
|
|
public static class PubSubMessageUtils {
|
|
public static bool compare_obj<T>(T t1, T t2) {
|
|
if (t1 == null) {
|
|
return t2 == null;
|
|
} else
|
|
return t1.Equals(t2);
|
|
}
|
|
public static bool compare_arrays<T>(IEnumerable<T> col1, IEnumerable<T> col2) {
|
|
if (col1.Count() != col2.Count())
|
|
return false;
|
|
int i = 0;
|
|
foreach (T t in col1) {
|
|
T t1 = t;
|
|
T t2 = col2.ElementAt(i++);
|
|
var eqs = compare_obj(t1, t2);
|
|
if (!eqs)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
// Subscribe //////////////////////////////////////////////////////////////
|
|
///////////////////////////////////////////////////////////////////////////
|
|
public class SubscribeMsg : IPubSubMessage {
|
|
|
|
public string connection;
|
|
public string subscribing_app = ClientInfo.thisClient.app;
|
|
public string[] tags;
|
|
public void serialize_into_str(Stream str) {
|
|
BinaryWriter bw = new BinaryWriter(str);
|
|
bw.write_string_cpp(connection);
|
|
bw.write_string_cpp(subscribing_app);
|
|
bw.Write(tags.Length);
|
|
foreach (var t in tags)
|
|
bw.write_string_cpp(t);
|
|
}
|
|
|
|
public void consruct_deserialize(Stream str) {
|
|
BinaryReader br = new BinaryReader(str);
|
|
this.connection = br.read_string_cpp();
|
|
this.subscribing_app = br.read_string_cpp();
|
|
var n_tags = br.ReadInt32();
|
|
tags = new string[n_tags];
|
|
for (var i = 0; i < n_tags; i++)
|
|
tags[i] = br.read_string_cpp();
|
|
}
|
|
public override string ToString() {
|
|
var tgs_str = String.Empty;
|
|
foreach (var t in tags)
|
|
tgs_str += t;
|
|
return $"{GetType().Name} connection: {connection} subscribing_app: {subscribing_app} tags: {tgs_str}";
|
|
}
|
|
public override bool Equals(object? obj) {
|
|
if (obj != null && obj is SubscribeMsg) {
|
|
var obj_sub_msg = obj as SubscribeMsg;
|
|
return PubSubMessageUtils.compare_obj(this.connection, obj_sub_msg.connection) &&
|
|
PubSubMessageUtils.compare_obj(this.subscribing_app, obj_sub_msg.subscribing_app) &&
|
|
PubSubMessageUtils.compare_arrays(tags, obj_sub_msg.tags);
|
|
// TODO compare tags
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public override int GetHashCode() {
|
|
var str = ToString();
|
|
return str.GetHashCode();
|
|
}
|
|
}
|
|
|
|
// Unsubscribe ////////////////////////////////////////////////////////////
|
|
///////////////////////////////////////////////////////////////////////////
|
|
public class UnsubscribeMsg : SubscribeMsg {
|
|
}
|
|
|
|
// Tasnaction /////////////////////////////////////////////////////////////
|
|
///////////////////////////////////////////////////////////////////////////
|
|
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 override string ToString() {
|
|
return $"TransactionMsg n_msgs: {n_msgs}";
|
|
}
|
|
|
|
public override bool Equals(object? obj) {
|
|
if (obj != null && (obj is TransactionMsg)) {
|
|
TransactionMsg other_ = obj as TransactionMsg;
|
|
return n_msgs == other_.n_msgs;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Publish ////////////////////////////////////////////////////////////////
|
|
/// ///////////////////////////////////////////////////////////////////////
|
|
public class PublishMsg : IPubSubMessage {
|
|
public string connection;
|
|
public string publishing_app = ClientInfo.thisClient.app;
|
|
public string tag;
|
|
public string tag_type;
|
|
public bool is_compressed = false;
|
|
// this is the size that of the msg, regarless of it being serialized or not
|
|
public long publish_time_epoch_ms = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
public byte[] data_to_publish;
|
|
|
|
public void sanity_check() {
|
|
Log.assert(!String.IsNullOrEmpty(tag_type));
|
|
Log.assert(!String.IsNullOrEmpty(tag));
|
|
Log.assert(!String.IsNullOrEmpty(connection));
|
|
}
|
|
|
|
public void serialize_into_str(Stream str) {
|
|
sanity_check();
|
|
BinaryWriter bw = new BinaryWriter(str);
|
|
bw.write_string_cpp(connection);
|
|
bw.write_string_cpp(publishing_app);
|
|
bw.write_string_cpp(tag);
|
|
bw.write_string_cpp(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.read_string_cpp();
|
|
publishing_app = br.read_string_cpp();
|
|
tag = br.read_string_cpp();
|
|
tag_type = br.read_string_cpp();
|
|
is_compressed = br.ReadBoolean();
|
|
publish_time_epoch_ms = br.ReadInt64();
|
|
var size = br.ReadInt32();
|
|
data_to_publish = br.ReadBytes(size);
|
|
}
|
|
public override string ToString() {
|
|
var data_txt = StreamUtilityMethods.first_n_bytes_to_string(data_to_publish, 4);
|
|
return $"PublishMsg connection: {connection} publishing_app: {publishing_app} " +
|
|
$"tag: {tag} tag_type: {tag_type} is_compressed: {is_compressed} " +
|
|
$"publish_time_epoch_ms: {publish_time_epoch_ms} data len: {data_to_publish.Length} " +
|
|
$"data (4): {data_txt}";
|
|
}
|
|
|
|
public override bool Equals(object? obj) {
|
|
if (obj != null && obj is PublishMsg) {
|
|
PublishMsg pub_msg_ = obj as PublishMsg;
|
|
return connection.Equals(pub_msg_.connection) &&
|
|
publishing_app.Equals(pub_msg_.publishing_app) &&
|
|
tag.Equals(pub_msg_.tag) &&
|
|
tag_type.Equals(pub_msg_.tag_type) &&
|
|
is_compressed.Equals(pub_msg_.is_compressed) &&
|
|
publish_time_epoch_ms.Equals(pub_msg_.publish_time_epoch_ms) &&
|
|
PubSubMessageUtils.compare_arrays(data_to_publish, pub_msg_.data_to_publish);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|
|
public enum PubSubMessageType {
|
|
SUBSCRIBE = 0,
|
|
UNSUBSCRIIBE,
|
|
TRANSACTION,
|
|
PUBLISH
|
|
}
|
|
|
|
public class PubSubClientMessage : IPubSubMessage {
|
|
public PubSubMessageType msg_type;
|
|
// only one of these wil be non null
|
|
public SubscribeMsg sub_msg = null;
|
|
public UnsubscribeMsg unsub_msg = null;
|
|
public TransactionMsg transaction_msg = null;
|
|
public PublishMsg pub_msg = null;
|
|
|
|
public PubSubClientMessage() { }
|
|
|
|
public PubSubClientMessage(SubscribeMsg sub_msg_) {
|
|
sub_msg = sub_msg_;
|
|
msg_type = PubSubMessageType.SUBSCRIBE;
|
|
}
|
|
|
|
public PubSubClientMessage(UnsubscribeMsg unsub_msg_) {
|
|
unsub_msg = unsub_msg_;
|
|
msg_type = PubSubMessageType.UNSUBSCRIIBE;
|
|
}
|
|
|
|
public PubSubClientMessage(TransactionMsg transaction_msg_) {
|
|
transaction_msg = transaction_msg_;
|
|
msg_type = PubSubMessageType.TRANSACTION;
|
|
}
|
|
|
|
public PubSubClientMessage(PublishMsg pub_msg_) {
|
|
pub_msg = pub_msg_;
|
|
msg_type = PubSubMessageType.PUBLISH;
|
|
}
|
|
|
|
public void serialize_into_str(Stream str) {
|
|
BinaryWriter bw = new BinaryWriter(str);
|
|
bw.Write((int)msg_type);
|
|
switch (msg_type) {
|
|
case PubSubMessageType.SUBSCRIBE:
|
|
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 byte[] serialize_into_bytes() {
|
|
MemoryStream ms = new MemoryStream();
|
|
BinaryReader br = new BinaryReader(ms);
|
|
serialize_into_str(ms);
|
|
var len = Convert.ToInt32(ms.Position);
|
|
ms.Position = 0;
|
|
return br.ReadBytes(len);
|
|
}
|
|
|
|
public void consruct_deserialize(Stream str) {
|
|
BinaryReader br = new BinaryReader(str);
|
|
int type = br.ReadInt32();
|
|
msg_type = (PubSubMessageType)type;
|
|
switch (msg_type) {
|
|
case PubSubMessageType.SUBSCRIBE:
|
|
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 void consruct_deserialize(byte[] msg) {
|
|
MemoryStream ms = new MemoryStream(msg);
|
|
ms.Position = 0;
|
|
consruct_deserialize(ms);
|
|
}
|
|
|
|
public override string ToString() {
|
|
var rv = $"type: {msg_type} ";
|
|
switch (msg_type) {
|
|
case PubSubMessageType.SUBSCRIBE:
|
|
rv += $"{sub_msg}";
|
|
break;
|
|
case PubSubMessageType.UNSUBSCRIIBE:
|
|
rv += $"{unsub_msg}";
|
|
break;
|
|
case PubSubMessageType.TRANSACTION:
|
|
rv += $"{transaction_msg}";
|
|
break;
|
|
case PubSubMessageType.PUBLISH:
|
|
rv += $"{pub_msg}";
|
|
break;
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
public override bool Equals(object? obj) {
|
|
if (obj != null && obj is PubSubClientMessage) {
|
|
var msg_ = obj as PubSubClientMessage;
|
|
return PubSubMessageUtils.compare_obj(msg_type, msg_.msg_type) &&
|
|
PubSubMessageUtils.compare_obj(sub_msg, msg_.sub_msg) &&
|
|
PubSubMessageUtils.compare_obj(pub_msg, msg_.pub_msg) &&
|
|
PubSubMessageUtils.compare_obj(transaction_msg, msg_.transaction_msg) &&
|
|
PubSubMessageUtils.compare_obj(unsub_msg, msg_.unsub_msg);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
//
|
|
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_string_cpp(tag);
|
|
bw.write_string_cpp(tag_type);
|
|
bw.Write(data.Length);
|
|
bw.Write(data);
|
|
}
|
|
public byte[] serialize_into_bytes() {
|
|
MemoryStream ms = new MemoryStream();
|
|
serialize_into_str(ms);
|
|
var pos = Convert.ToInt32(ms.Position);
|
|
ms.Position = 0;
|
|
BinaryReader br = new BinaryReader(ms);
|
|
return br.ReadBytes(pos);
|
|
}
|
|
public void consruct_deserialize(Stream str) {
|
|
BinaryReader br = new BinaryReader(str);
|
|
tag = br.read_string_cpp();
|
|
tag_type = br.read_string_cpp();
|
|
var data_sz = br.ReadInt32();
|
|
data = br.ReadBytes(data_sz);
|
|
}
|
|
|
|
public void consruct_deserialize(byte[] msg) {
|
|
MemoryStream str = new MemoryStream(msg);
|
|
str.Position = 0;
|
|
consruct_deserialize(str);
|
|
}
|
|
|
|
public override string ToString() {
|
|
var data_txt = StreamUtilityMethods.first_n_bytes_to_string(data, 4);
|
|
return $"PubSubServerMessage tag: {tag} tag_type: {tag_type} data_len: {data.Length} " +
|
|
$" data: {data_txt}";
|
|
}
|
|
|
|
public override bool Equals(object? obj) {
|
|
if (obj != null && obj is PubSubServerMessage) {
|
|
PubSubServerMessage msg_ = obj as PubSubServerMessage;
|
|
return PubSubMessageUtils.compare_obj(tag, msg_.tag) &&
|
|
PubSubMessageUtils.compare_obj(tag_type, msg_.tag_type) &&
|
|
PubSubMessageUtils.compare_arrays(data, msg_.data);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|