From da9d3184a146f53f6fc072af495993c831a2d127 Mon Sep 17 00:00:00 2001 From: shinichi Date: Mon, 19 Oct 2020 16:41:38 +0200 Subject: [PATCH] token and volatile --- Server/Client.cs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/Server/Client.cs b/Server/Client.cs index b0468ac..16dddac 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Net.Sockets; @@ -23,10 +24,11 @@ namespace Server private DateTime sessionStart; private string fileName; private Timer timer; - private byte[] BikeDataBuffer; - private byte[] BPMDataBuffer; + private volatile byte[] BikeDataBuffer; + private volatile byte[] BPMDataBuffer; private bool BPMdata = false; private bool Bikedata = false; + private object token = new object { }; public Client(Communication communication, TcpClient tcpClient) { @@ -39,8 +41,9 @@ namespace Server this.BikeDataBuffer = new byte[16]; this.BPMDataBuffer = new byte[2]; this.timer.Interval = 1000; - this.timer.AutoReset = true; + this.timer.AutoReset = false; this.timer.Elapsed += SendDataToDoctor; + Console.WriteLine("token is " + token); stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); } @@ -158,15 +161,21 @@ namespace Server { //Bikedata = true; saveData?.WriteDataRAWBike(payloadbytes); - Array.Copy(this.BikeDataBuffer, 0, this.BikeDataBuffer, 8, 8); - Array.Copy(payloadbytes, 0, this.BikeDataBuffer, 0, 8); + lock (token) + { + Array.Copy(this.BikeDataBuffer, 0, this.BikeDataBuffer, 8, 8); + Array.Copy(payloadbytes, 0, this.BikeDataBuffer, 0, 8); + } //this.communication.Doctor?.sendMessage(DataParser.GetRawBikeDataDoctor(payloadbytes, this.username)); } else if (DataParser.isRawDataBPMServer(message)) { //BPMdata = true; saveData?.WriteDataRAWBPM(payloadbytes); - Array.Copy(payloadbytes, 0, this.BPMDataBuffer, 0, 2); + lock (token) + { + Array.Copy(payloadbytes, 0, this.BPMDataBuffer, 0, 2); + } //this.communication.Doctor?.sendMessage(DataParser.GetRawBPMDataDoctor(payloadbytes, this.username)); } @@ -270,10 +279,13 @@ namespace Server private void SendDataToDoctor(object sender, ElapsedEventArgs e) { - this.communication.Doctor?.sendMessage(DataParser.GetRawBikeDataDoctor(this.BikeDataBuffer.Take(8).ToArray(), this.username)); - this.communication.Doctor?.sendMessage(DataParser.GetRawBikeDataDoctor(this.BikeDataBuffer.Skip(8).ToArray(), this.username)); - this.communication.Doctor?.sendMessage(DataParser.GetRawBPMDataDoctor(this.BPMDataBuffer, this.username)); - + lock (token) + { + this.communication.Doctor?.sendMessage(DataParser.GetRawBikeDataDoctor(this.BikeDataBuffer.Take(8).ToArray(), this.username)); + this.communication.Doctor?.sendMessage(DataParser.GetRawBikeDataDoctor(this.BikeDataBuffer.Skip(8).ToArray(), this.username)); + this.communication.Doctor?.sendMessage(DataParser.GetRawBPMDataDoctor(this.BPMDataBuffer, this.username)); + } + this.timer.Start(); } } }