Skip to content

Commit

Permalink
1.08 BugFix
Browse files Browse the repository at this point in the history
  • Loading branch information
“寧々” committed Mar 29, 2022
1 parent e9b4b38 commit fafa0b3
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ReservoirServer/AMQCommunicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Initialization()
{
Dispose();
_sender = new SimpleQueueSender(queue_name, uri);
_subscriber = new SimpleTopicSubscriber(topic_name, uri, "xxx", "zzz", null);
_subscriber = new SimpleTopicSubscriber(topic_name, uri, "client_"+ platform_id, "consumer_"+ platform_id, null);
_subscriber.OnMessageReceived += _subscriber_OnMessageReceived;
}

Expand Down
15 changes: 14 additions & 1 deletion ReservoirServer/Driver/BoxTCPDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@ public Task SendDataAsync(IComClient client, byte[] data)
BoxTCPClient carclient = client as BoxTCPClient;
NetworkStream ns = carclient.Client.GetStream();
ns.WriteTimeout = TransmitTimeout;
var task = ns.WriteAsync(data, 0, data.Length);
Task task = null;
try
{
task = ns.WriteAsync(data, 0, data.Length);
}
catch(System.IO.IOException)
{
client.Disconnected();
}
catch(SocketException)
{
client.Disconnected();
}

return task;
//task.ContinueWith((t) => { this.OnComDataSent?.Invoke(client, data.Length); });

Expand Down
1 change: 1 addition & 0 deletions ReservoirServer/Driver/IComClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void Disconnect()
{
Client.Close();
IsAlive = false;
OnDisconnected?.Invoke();
}

public void Disconnected()
Expand Down
1 change: 1 addition & 0 deletions ReservoirServer/Driver/SimpleAMQSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public SimpleTopicSubscriber(string topicName, string brokerUri, string clientId
public void OnMessage(IMessage message)
{
ITextMessage textMessage = message as ITextMessage;
//Console.WriteLine(textMessage);
if (this.OnMessageReceived != null)
{
this.OnMessageReceived(textMessage.Text);
Expand Down
19 changes: 13 additions & 6 deletions ReservoirServer/Driver/TCPDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected virtual void HandleAsyncConnection(IAsyncResult res)
client.ReceiveTimeout = TransmitTimeout;
byte[] buff = new byte[client.ReceiveBufferSize];
client.GetStream().ReadTimeout = TransmitTimeout;
BoxTCPClient carclient = new BoxTCPClient(client);
BoxTCPClient carclient = new BoxTCPClient(client) { IsAlive = true };
RecState state = new RecState { Client = carclient, Buffer = buff };
this.OnComClientConnected?.Invoke(carclient);
state.Stream.BeginRead(state.Buffer, 0, state.Buffer.Length, HandleClientAsyncRec, state);
Expand All @@ -118,16 +118,18 @@ protected virtual void HandleClientAsyncRec(IAsyncResult res)
return;
RecState state = (RecState)res.AsyncState;
TcpClient client = state.Client.Client;
byte[] oldbuff = state.Buffer;
NetworkStream ns = state.Stream;
if (client == null)
return;
if(client.Connected == false)

byte[] oldbuff = state.Buffer;

if(client.Connected == false )
{
this.OnComClientDisconneted?.Invoke(state.Client);
state.Client.Disconnected();
return;
}
NetworkStream ns = state.Stream;

int b2r = 0;
try
Expand All @@ -139,6 +141,10 @@ protected virtual void HandleClientAsyncRec(IAsyncResult res)
{
b2r = 0;
}
catch(SocketException)
{
b2r = 0;
}
catch(ObjectDisposedException)
{
return;
Expand All @@ -160,7 +166,8 @@ protected virtual void HandleClientAsyncRec(IAsyncResult res)

else
{
if(AsyncRecBuffer)
state.Client.IsAlive = true;
if (AsyncRecBuffer)
{
byte[] buffer = new byte[b2r];
Buffer.BlockCopy(state.Buffer, 0, buffer, 0, b2r);
Expand Down
4 changes: 2 additions & 2 deletions ReservoirServer/Enterty/Box.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public Box(string id,IComClient client)
{
comClient.OnDisconnected += () =>
{
locker.EnterWriteLock();
//locker.EnterWriteLock();
State = BoxState.Offline;
locker.ExitWriteLock();
//locker.ExitWriteLock();
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion ReservoirServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ReservoirServer
class Program
{
//TODO: Log file, Safety(Encryption), JSON error catch, system service
public static readonly string version = "1.07";
public static readonly string version = "1.08";

#if DEBUG
public static string VERSION => version + "a";
Expand Down
5 changes: 4 additions & 1 deletion ReservoirServer/SimpleBoxAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,15 @@ public void SimpleBoxDataSubscribeHandler(string json)
Box box = _list[bid];
if (box != null)
{
bool online = false;
boxdata.DeviceN = bid;
string senddata = JsonConvert.SerializeObject(boxdata, jsonSettings);
box.locker.EnterReadLock();
var client = box.ComClient;
online = (box.State == BoxState.Online);
box.locker.ExitReadLock();
_server.SendPack(client, senddata);
if(online)
_server.SendPack(client, senddata);
}
}
}
Expand Down

0 comments on commit fafa0b3

Please sign in to comment.