/*** bool WriteMsgToStream(NetworkStream targetStrm, MSG msg) * * Parameters: * * targetStrm - The Stream to write the message to * msg - msg to write to the stream. * * Return Values: * true - a message was read * false, no message was read, something went wrong * * Errors: * * * Description: * * Write out a complete message packet * * ------------------------------------------------------------ */ private static bool WriteMsgToStream(NetworkStream targetStrm, MSG msg) { byte[] rgbHeader = new byte[8]; DTBT dtbt = new DTBT(); // put the command in dtbt.i = (int)msg.cmdmsg; rgbHeader[0] = dtbt.b0; rgbHeader[1] = dtbt.b1; rgbHeader[2] = dtbt.b2; rgbHeader[3] = dtbt.b3; // and how much data is written dtbt.i = msg.rgbData.Length; rgbHeader[4] = dtbt.b0; rgbHeader[5] = dtbt.b1; rgbHeader[6] = dtbt.b2; rgbHeader[7] = dtbt.b3; try { // Set the timeout so we don't wait too long targetStrm.WriteTimeout = 10000; // set to 10 seconds for the write // write out the header targetStrm.Write(rgbHeader, 0, rgbHeader.Length); // write out the message if any if (msg.rgbData.Length > 0) { targetStrm.Write(msg.rgbData, 0, msg.rgbData.Length); } } // something bad happened, but we want to just print the exception and go back and wait // for another connection. catch (Exception e) { Console.WriteLine(e.ToString()); return(false); } return(true); }
/*** bool WriteMsgToStream(NetworkStream targetStrm, MSG msg) * * Parameters: * * targetStrm - The Stream to write the message to * msg - msg to write to the stream. * * Return Values: * true - a message was read * false, no message was read, something went wrong * * Errors: * * * Description: * * Write out a complete message packet * * ------------------------------------------------------------ */ private static bool WriteMsgToStream(NetworkStream targetStrm, MSG msg) { byte[] rgbHeader = new byte[8]; DTBT dtbt = new DTBT(); // put the command in dtbt.i = (int) msg.cmdmsg; rgbHeader[0] = dtbt.b0; rgbHeader[1] = dtbt.b1; rgbHeader[2] = dtbt.b2; rgbHeader[3] = dtbt.b3; // and how much data is written dtbt.i = msg.rgbData.Length; rgbHeader[4] = dtbt.b0; rgbHeader[5] = dtbt.b1; rgbHeader[6] = dtbt.b2; rgbHeader[7] = dtbt.b3; try { // Set the timeout so we don't wait too long targetStrm.WriteTimeout = 10000; // set to 10 seconds for the write // write out the header targetStrm.Write(rgbHeader, 0, rgbHeader.Length); // write out the message if any if (msg.rgbData.Length > 0) { targetStrm.Write(msg.rgbData, 0, msg.rgbData.Length); } } // something bad happened, but we want to just print the exception and go back and wait // for another connection. catch (Exception e) { Console.WriteLine(e.ToString()); return (false); } return (true); }
/*** MSG ReadMsgFromStream(NetworkStream targetStrm) * * Parameters: * * targetStrm - The Stream to read the message from * * Return Values: * msg - The message packet, null if there was an error * * Errors: * * * Description: * * Read a full message packet, or timesout and returns null * * ------------------------------------------------------------ */ private static MSG ReadMsgFromStream(NetworkStream targetStrm) { const int cbBufferBlock = 1024; DTBT dtbt = new DTBT(); int cbRead = 0; byte[] rgbRcv = new byte[cbBufferBlock]; bool fGotHeader = false; int cbNeed = 8; // got to get the header MSG msg = new MSG(); try { // Set the timeout so we don't wait too long targetStrm.ReadTimeout = 10000; // set to 10 seconds for the read // loop until we get the whole data packet while (cbRead < cbNeed) { // will throw and exception if this times out cbRead += targetStrm.Read(rgbRcv, cbRead, rgbRcv.Length - cbRead); // if we need more space if (cbRead >= rgbRcv.Length) { Array.Resize(ref rgbRcv, rgbRcv.Length + cbBufferBlock); } if (!fGotHeader && rgbRcv.Length >= 8) { // get the command dtbt.b0 = rgbRcv[0]; dtbt.b1 = rgbRcv[1]; dtbt.b2 = rgbRcv[2]; dtbt.b3 = rgbRcv[3]; msg.cmdmsg = (MSG.CMDMSG)dtbt.i; // find out how many more bytes must come in. dtbt.b0 = rgbRcv[4]; dtbt.b1 = rgbRcv[5]; dtbt.b2 = rgbRcv[6]; dtbt.b3 = rgbRcv[7]; cbNeed = dtbt.i + 8; // yeah, junk came in. if (cbNeed < 8) { return(null); } fGotHeader = true; } } // copy over just the data. msg.rgbData = new byte[cbRead - 8]; Array.Copy(rgbRcv, 8, msg.rgbData, 0, cbRead - 8); } // something bad happened, but we want to just print the exception and go back and wait // for another connection. catch (Exception e) { Console.WriteLine(e.ToString()); return(null); } return(msg); }
/*** MSG ReadMsgFromStream(NetworkStream targetStrm) * * Parameters: * * targetStrm - The Stream to read the message from * * Return Values: * msg - The message packet, null if there was an error * * Errors: * * * Description: * * Read a full message packet, or timesout and returns null * * ------------------------------------------------------------ */ private static MSG ReadMsgFromStream(NetworkStream targetStrm) { const int cbBufferBlock = 1024; DTBT dtbt = new DTBT(); int cbRead = 0; byte[] rgbRcv = new byte[cbBufferBlock]; bool fGotHeader = false; int cbNeed = 8; // got to get the header MSG msg = new MSG(); try { // Set the timeout so we don't wait too long targetStrm.ReadTimeout = 10000; // set to 10 seconds for the read // loop until we get the whole data packet while (cbRead < cbNeed) { // will throw and exception if this times out cbRead += targetStrm.Read(rgbRcv, cbRead, rgbRcv.Length - cbRead); // if we need more space if (cbRead >= rgbRcv.Length) { Array.Resize(ref rgbRcv, rgbRcv.Length + cbBufferBlock); } if (!fGotHeader && rgbRcv.Length >= 8) { // get the command dtbt.b0 = rgbRcv[0]; dtbt.b1 = rgbRcv[1]; dtbt.b2 = rgbRcv[2]; dtbt.b3 = rgbRcv[3]; msg.cmdmsg = (MSG.CMDMSG)dtbt.i; // find out how many more bytes must come in. dtbt.b0 = rgbRcv[4]; dtbt.b1 = rgbRcv[5]; dtbt.b2 = rgbRcv[6]; dtbt.b3 = rgbRcv[7]; cbNeed = dtbt.i + 8; // yeah, junk came in. if (cbNeed < 8) { return (null); } fGotHeader = true; } } // copy over just the data. msg.rgbData = new byte[cbRead - 8]; Array.Copy(rgbRcv, 8, msg.rgbData, 0, cbRead - 8); } // something bad happened, but we want to just print the exception and go back and wait // for another connection. catch (Exception e) { Console.WriteLine(e.ToString()); return (null); } return (msg); }