/// <summary> /// What to do when a complete block has arrived. /// Provided by derived class. /// </summary> /// <returns></returns> protected virtual bool BlockCompleted() { try { CNXLog.InfoFormat("Testing {0} length {1}", (Block)mBlockId, mMemStream.Length); if (mSize == 0) { mCRCOk = true; } else if (mMemStream.Length >= mSize && mBytesWritten >= mSize) { mMemStream.Seek(0, SeekOrigin.Begin); ushort crc = CRC16.CRCStream(mMemStream, (int)mSize); CNXLog.InfoFormat("CRC check calculated {0:X4} should be {1:X4}.", crc, mCRC); mCRCOk = (crc == mCRC); } if (mCRCOk) { CNXLog.InfoFormat("{0} complete.", (Block)mBlockId); } SendBlockQueryResponce(); } catch (Exception e) { CNXLog.ErrorFormat("BlockComplete {0} {1}.", (Block)mBlockId, e.Message); } return(mCRCOk); }
/// <summary> /// Creates a CAN client ready for sending and recieving CAN frames. /// </summary> /// <param name="ifaceName">The CAN interfave name to bind to.</param> /// <param name="bufferSize">recieve buffer size to set.</param> /// <remarks>This is a Linux only implementation and requires a native library.</remarks> public CANNativeClient(string ifaceName, uint bufferSize) { if (bufferSize == 0) { mCanFd = can_open(ifaceName); } else { mCanFd = can_buffer_open(ifaceName, bufferSize); } if (mCanFd < 0) { CNXLog.FatalFormat("CANNativeClient can_buffer_open({0}, 0x1000) returned {1}", ifaceName, mCanFd); throw new SocketException(mCanFd); } // organise a decent buffersize for the frames int buffersize = can_getbuffersize(mCanFd); CNXLog.InfoFormat("CAN buffer size {0}.", buffersize); mRxThread = new Thread(new ThreadStart(ReceiveFrame)) { Priority = ThreadPriority.AboveNormal, IsBackground = true }; mRxThread.Start(); }
protected override bool BlockCompleted() { bool result = false; try { if (base.BlockCompleted()) { mBlockData = new byte[mSize]; if (mSize > 0) { Array.Copy(mMemStream.ToArray(), mBlockData, mSize); } else { OnRaiseBlockStatusEvent(new BlockStatusEventArgs(mBlockId, BlockState.COMPLETE)); } CNXLog.InfoFormat("TransientBlockReciever {0} Completed:\n\r{1}", (Block)mBlockId, HexDump.Dump(mBlockData)); result = true; } } catch (Exception e) { CNXLog.Error("TransientBlockReciever.BlockCompleted:", e); } return(result); }
/// <summary> /// Creates a client connection to the CAN bridge which passes CAN frames across an IP socket as datagrams. /// </summary> /// <param name="address">Server address.</param> /// <param name="inPort">Port to receive frames on.</param> /// <param name="outPort">Port to transmit frames on.</param> public CANBridgeClient(string address, int inPort, int outPort) { mInPort = inPort; mOutPort = outPort; mAddress = address; // associate with the CAN Bridge mTxEndPoint = new IPEndPoint(IPAddress.Parse(address), outPort); mRxEndPoint = new IPEndPoint(IPAddress.Any, inPort); mRxSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); mRxSocket.Bind(mRxEndPoint); mTxSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // set a decent number of threads in the thread pool int worker, completion; ThreadPool.GetMinThreads(out worker, out completion); CNXLog.InfoFormat("Workers {0} + 16, Completion {1}", worker, completion); ThreadPool.SetMinThreads(worker + 16, completion); mRxThread = new Thread(new ThreadStart(ReceiveFrames)); //mRxThread.IsBackground = true; mRxThread.Start(); }