예제 #1
0
        /// <summary>
        /// Synchronously read a complete packet from a possibly-fragmented stream
        /// </summary>
        /// <remarks>
        /// Not very useful in production, but it made development and testing easier
        /// </remarks>
        public static SsmPacket ReadFromStream(Stream stream)
        {
            SsmPacketParser parser = new SsmPacketParser();

            while (!parser.IsComplete)
            {
                if (parser.bytesReceived != 0)
                {
                    System.Threading.Thread.Sleep(1);
                }

                int bufferLength = stream.Read(parser.buffer, parser.bytesReceived, parser.buffer.Length - parser.bytesReceived);
                if (bufferLength == 0)
                {
                    throw new SsmPacketFormatException("Unexpected end of stream.");
                }

                for (int i = 0; i < bufferLength; i++)
                {
                    int index = parser.bytesReceived + i;
                    parser.CheckByte(index, parser.buffer[index]);
                }
                parser.bytesReceived += bufferLength;
            }

            return(SsmPacket.ParseResponse(parser.buffer, 0, parser.bytesReceived));
        }
예제 #2
0
        /// <summary>
        /// Returns the new packet to the caller
        /// </summary>
        public SsmPacket EndReadFromStream(IAsyncResult asyncResult)
        {
            ReadFromStreamAsyncResult internalState = (ReadFromStreamAsyncResult)asyncResult;

            try
            {
                if (internalState.Exception != null)
                {
                    throw internalState.Exception;
                }

                return(SsmPacket.ParseResponse(this.buffer, 0, this.bytesReceived));
            }
            finally
            {
                internalState.Dispose();
            }
        }
예제 #3
0
        /// <summary>
        /// Synchronously sends a request and waits for the response
        /// </summary>
        public SsmPacket SyncSendReceive(SsmPacket request)
        {
            //Console.WriteLine("#### Raw request data:");
            byte[] requestBuffer = request.Data;
            SsmUtility.DumpBuffer(requestBuffer);

            stream.Write(requestBuffer, 0, requestBuffer.Length);
            Thread.Sleep(250);

            byte[] responseBuffer = new byte[2000];
            int    bytesRead      = stream.Read(responseBuffer, 0, responseBuffer.Length);

            Console.WriteLine("Bytes read: {0}", bytesRead);
            Console.WriteLine("#### Raw response data:");
            SsmUtility.DumpBuffer(responseBuffer);

            SsmPacket response = SsmPacket.ParseResponse(responseBuffer, 0, bytesRead);

            return(response);
        }