/// <summary> /// Body of the thread that polls the messages. /// It will poll data EVEN IF nobody is registered on the event! /// </summary> protected void PollMessageThread(object pParam) { while (this.mPollMessages) { if (!this.mWaitingForAck) { //if not waiting for a ACK //small probabily of thread collision, but let's see like that first... (should lock the variable before) lock (mWaitingForAckSynchroKey) { if (!this.mWaitingForAck) { //synchro : lock the line lock (this.mPollingMessageSynchroKey) { try { bool vReceiveOk = false; ARCPO_Packet vPacket = new ARCPO_Packet(); vReceiveOk = vPacket.ReadPacket(this.mPort); if (vReceiveOk && this.PacketReceived != null) { //received + someone waiting for notification : raise the event ARCPO_ReceivedEventArgs vE = new ARCPO_ReceivedEventArgs(vPacket); this.PacketReceived(this, vE); } } catch (Exception ex) { //nothing (for now) } } } } } //get some rest Thread.Sleep(RECEIVE_POLLING_IN_MS); } }
/// <summary> /// Sends a packet, retrying in case it fails and returns if ok /// </summary> /// <param name="pPacket"></param> /// <returns></returns> public bool SendPacket(ARCPO_Packet pPacket) { if (!this.IsSerialConnected() && !this.ConnectSerial()) { return(false); } bool vOk = false; int vTryCount = 0; while (!vOk && vTryCount < this.RetryCount) { //force the expect acknowledge except if no retry asked pPacket.mExpectAcknowledge = (RetryCount > 0 ? true : pPacket.mExpectAcknowledge); //try to make sure we release the wait for ack flag try { if (pPacket.mExpectAcknowledge) { lock (mWaitingForAckSynchroKey) { this.mWaitingForAck = true; } } bool vSent = pPacket.SendPacket(this.mPort); if (vSent && pPacket.mExpectAcknowledge) { //locks the line lock (this.mPollingMessageSynchroKey) { #region get the acknowledgement packet //tries n times to read a acknowledge while (!vOk && vTryCount < this.RetryCount) { //wait longer and longer Thread.Sleep(100 * vTryCount); bool vAckOk = false; try { ARCPO_Packet vAck = new ARCPO_Packet(); vAckOk = vAck.ReadPacket(this.mPort); if (vAckOk && vAck.mID == pPacket.mID && vAck.ContentString != null && vAck.ContentString.StartsWith("ACK")) { vOk = true; } } catch (TimeoutException) { } vTryCount++; } #endregion } } else { //retry } } catch (InvalidOperationException) { //disconnected ? this.DisconnectSerial(); } finally { //anycases, release the line this.mWaitingForAck = false; } //count the tries vTryCount++; } return(vOk); }
/// <summary> /// Sends a packet, retrying in case it fails and returns if ok /// </summary> /// <param name="pPacket"></param> /// <returns></returns> public bool SendPacket(ARCPO_Packet pPacket) { if (!this.IsSerialConnected() && !this.ConnectSerial()) { return false; } bool vOk = false; int vTryCount = 0; while (!vOk && vTryCount < this.RetryCount) { //force the expect acknowledge except if no retry asked pPacket.mExpectAcknowledge = (RetryCount > 0 ? true : pPacket.mExpectAcknowledge); //try to make sure we release the wait for ack flag try { if (pPacket.mExpectAcknowledge) { lock (mWaitingForAckSynchroKey) { this.mWaitingForAck = true; } } bool vSent = pPacket.SendPacket(this.mPort); if (vSent && pPacket.mExpectAcknowledge) { //locks the line lock (this.mPollingMessageSynchroKey) { #region get the acknowledgement packet //tries n times to read a acknowledge while (!vOk && vTryCount < this.RetryCount) { //wait longer and longer Thread.Sleep(100 * vTryCount); bool vAckOk = false; try { ARCPO_Packet vAck = new ARCPO_Packet(); vAckOk = vAck.ReadPacket(this.mPort); if (vAckOk && vAck.mID == pPacket.mID && vAck.ContentString != null && vAck.ContentString.StartsWith("ACK")) { vOk = true; } } catch (TimeoutException) { } vTryCount++; } #endregion } } else { //retry } } catch (InvalidOperationException) { //disconnected ? this.DisconnectSerial(); } finally { //anycases, release the line this.mWaitingForAck = false; } //count the tries vTryCount++; } return vOk; }