private void ParseOneByte(byte data) { int int_data = (int)(data & 0xFF); //int int_data = data; if (int_data == PKT_START && isStart) { //System.Console.WriteLine( "ProtocolParser : PKT_START" ); counter = 0; isStart = false; } else if (int_data == PKT_END && counter == dataLength + PKT_HEADER_LEN) { Packet.Builder builder = new Packet.Builder(); // 커맨드를 뽑는다. int cmd = nbuffer.GetByteToInt(); // 길이를 뽑는다. int length = nbuffer.GetShort(); // 커맨드, 길이를 제외한 나머지 바이트를 컨텐트로 지정 byte[] content = nbuffer.GetBytes(); PacketCreated(this, new PacketEventArgs(builder.cmd(cmd).data(content).Build())); dataLength = 0; counter = 10; nbuffer.Clear(); isStart = true; } else if (counter > PKT_MAX_LEN) { //System.Console.WriteLine( "ProtocolParser : PKT_MAX_LEN" ); counter = 10; dataLength = 0; isStart = true; } else { if (counter == PKT_LENGTH_POS1) { lbuffer[0] = data; } else if (counter == PKT_LENGTH_POS2) { //System.Console.WriteLine( "ProtocolParser : PKT_LENGTH_POS2" ); lbuffer[1] = data; dataLength = ByteConverter.ByteToShort(lbuffer); } if (!isStart) { nbuffer.Put(data); counter++; } } }
public void Put(byte[] buff, int size) { byte[] test = new byte[size]; //Array.Copy( buff, 0, test, 0, size ); //System.Console.WriteLine( "Read Buffer : {0}", BitConverter.ToString( test ) ); //System.Console.WriteLine(); for (int i = 0; i < size; i++) { if (buff[i] == Const.PK_STX) { // packet start mBuffer = new ByteUtil(); IsEscape = false; } else if (buff[i] == Const.PK_ETX) { // end of packet Packet.Builder builder = new Packet.Builder(); int cmd = mBuffer.GetByteToInt(); string cmdstr = Enum.GetName(typeof(Cmd), cmd); int result_size = cmdstr != null && cmdstr.EndsWith("RESPONSE") ? 1 : 0; int result = result_size > 0 ? mBuffer.GetByteToInt() : -1; int length = mBuffer.GetUShort(); byte[] data = mBuffer.GetBytes(); //System.Console.WriteLine( "length : {0}, data : {1}", length, data.Length ); builder.cmd(cmd) .result(result) .data(data); //System.Console.WriteLine( "Read Packet : {0}", BitConverter.ToString( data ) ); //System.Console.WriteLine(); mBuffer.Clear(); mBuffer = null; PacketCreated(this, new PacketEventArgs(builder.Build())); IsEscape = false; } else if (buff[i] == Const.PK_DLE) { if (i < size - 1) { mBuffer.Put((byte)(buff[++i] ^ 0x20)); } else { IsEscape = true; } } else if (IsEscape) { mBuffer.Put((byte)(buff[i] ^ 0x20)); IsEscape = false; } else { mBuffer.Put(buff[i]); } } }