Added MtNofication message into MtClient

This commit is contained in:
Viacheslav Demydiuk
2024-01-12 21:29:46 +02:00
parent 98e3961e18
commit 919ca97101
3 changed files with 22 additions and 15 deletions
+1 -1
View File
@@ -3792,7 +3792,7 @@ namespace MtApi5
{ {
Log?.Debug("OnConnected: begin"); Log?.Debug("OnConnected: begin");
Client?.Send(new MtClientReady()); Client?.Send(new MtNotification(MtNotificationType.ClientReady));
_isBacktestingMode = IsTesting(); _isBacktestingMode = IsTesting();
+12 -8
View File
@@ -1,7 +1,4 @@
using System.ComponentModel.Design; namespace MtClient
using System.Data;
namespace MtClient
{ {
public enum MessageType public enum MessageType
{ {
@@ -11,7 +8,12 @@ namespace MtClient
ExpertList = 3, ExpertList = 3,
ExpertAdded = 4, ExpertAdded = 4,
ExpertRemoved = 5, ExpertRemoved = 5,
ClientReady = 6 Notification = 6
}
public enum MtNotificationType
{
ClientReady = 0
} }
public abstract class MtMessage public abstract class MtMessage
@@ -41,14 +43,16 @@ namespace MtClient
} }
} }
public class MtClientReady : MtMessage public class MtNotification(MtNotificationType notificationType) : MtMessage
{ {
public override MessageType MsgType => MessageType.ClientReady; public override MessageType MsgType => MessageType.Notification;
protected override string GetMessageBody() protected override string GetMessageBody()
{ {
return string.Empty; return $"{(int)NotificationType}";
} }
public MtNotificationType NotificationType { private set; get; } = notificationType;
} }
public class MtEvent(int expertHandle, int eventType, string payload) : MtMessage public class MtEvent(int expertHandle, int eventType, string payload) : MtMessage
+9 -6
View File
@@ -58,7 +58,10 @@ namespace MtClient
public void Send(MtMessage message) public void Send(MtMessage message)
{ {
pendingMessages_.Enqueue(message.Serialize()); lock (pendingMessages_)
{
pendingMessages_.Enqueue(message);
}
sendWaiter_.Set(); sendWaiter_.Set();
} }
@@ -66,7 +69,7 @@ namespace MtClient
{ {
while(ws_.State == WebSocketState.Open) while(ws_.State == WebSocketState.Open)
{ {
string? message = null; MtMessage? message = null;
lock(pendingMessages_) lock(pendingMessages_)
{ {
if (pendingMessages_.Count > 0) if (pendingMessages_.Count > 0)
@@ -81,8 +84,9 @@ namespace MtClient
try try
{ {
Log($"DoWrite: sending message: {message}"); string msgStr = message.Serialize();
byte[] bytes = Encoding.ASCII.GetBytes(message); Log($"DoWrite: sending message: {msgStr}");
byte[] bytes = Encoding.ASCII.GetBytes(msgStr);
await ws_.SendAsync(bytes, WebSocketMessageType.Text, true, CancellationToken.None); await ws_.SendAsync(bytes, WebSocketMessageType.Text, true, CancellationToken.None);
} }
catch (Exception e) catch (Exception e)
@@ -177,8 +181,7 @@ namespace MtClient
private readonly string host_; private readonly string host_;
private readonly int port_; private readonly int port_;
private readonly byte[] buf_ = new byte[10000]; private readonly byte[] buf_ = new byte[10000];
private readonly Dictionary<MessageType, Func<string, MtMessage?>> msgHandlers_ = new(); private readonly Queue<MtMessage> pendingMessages_ = [];
private readonly Queue<string> pendingMessages_ = [];
private readonly Thread receiveThread_; private readonly Thread receiveThread_;
private readonly Thread sendThread_; private readonly Thread sendThread_;