public Decode(byte[] buffer) { try { byte[] _tempbuff = new byte[buffer.Length]; EncDec endc = new EncDec(); _tempbuff = endc.Crypt(buffer); ms = new MemoryStream(_tempbuff); br = new BinaryReader(ms); dataSize = (ushort)br.ReadInt16(); tempbuff = new byte[dataSize]; Buffer.BlockCopy(_tempbuff, 0, tempbuff, 0, dataSize); br.Close(); ms.Close(); br.Dispose(); ms.Dispose(); _tempbuff = null; endc = null; } catch (Exception) { } }
public void ReceiveData(IAsyncResult ar) { Socket wSocket = (Socket)ar.AsyncState; try { if (wSocket.Connected) { int recvSize = wSocket.EndReceive(ar); // get the count of received bytes bool checkData = true; if (recvSize > 0) { if ((recvSize + bufCount) > MAX_BUFFER) // that may be a try to force buffer overflow, we don't allow that ;) { checkData = false; LocalDisconnect(wSocket); } else { // we have something in input buffer and it is not beyond our limits Buffer.BlockCopy(tmpbuf, 0, buffer, bufCount, recvSize); // copy the new data to our buffer bufCount += recvSize; // increase our buffer-counter } } else { // 0 bytes received, this should be a disconnect checkData = false; LocalDisconnect(wSocket); } while (checkData) // repeat while we have data { checkData = false; if (bufCount >= 4) // a minimum of 4 byte is required for us { byte[] _newtmp = new byte[bufCount]; Buffer.BlockCopy(buffer, 0, _newtmp, 0, bufCount); LogDebug.HexDump(_newtmp, 16, true, true); Decode de = new Decode(buffer); // only get the size first. LogConsole.Show("bufCount: {0} dataSize: {1}", bufCount, de.dataSize); if (bufCount >= (de.dataSize - 2)) // It's a complete packet. Call the handler. { de = new Decode(wSocket, de.tempbuff, this, Packets); // build up the Decode structure for next step OnReceiveData(de); // call the handling routine bufCount -= (de.dataSize); // decrease buffer-counter if (bufCount >= 0) // was the buffer greater than what the packet needs? then it may be the next packet. { Buffer.BlockCopy(buffer, 2 + de.dataSize, buffer, 0, bufCount); // move the rest to buffer start checkData = true; // loop for next packet } } else { byte[] _tempddd = new byte[bufCount]; EncDec end = new EncDec(); byte[] dddxx = end.Crypt(buffer); Buffer.BlockCopy(dddxx, 0, _tempddd, 0, de.dataSize); LogConsole.Show("bufCount: {0} dataSize: {1}", bufCount, de.dataSize); } de = null; } } // start the next async read if (wSocket != null && wSocket.Connected) { wSocket.BeginReceive(tmpbuf, 0, tmpbuf.Length, SocketFlags.None, new AsyncCallback(ReceiveData), wSocket); } } else { LocalDisconnect(wSocket); } } catch (SocketException) // explicit handling of SocketException { LocalDisconnect(wSocket); } catch (Exception) // other exceptions { LocalDisconnect(wSocket); } }
public static void HexDump(byte[] bytes, params object[] arg) { if ((bool)arg[2] == true) { EncDec crypt = new EncDec(); bytes = crypt.Crypt(bytes); } if (bytes != null) { int bytesLength = bytes.Length; char[] HexChars = "0123456789ABCDEF".ToCharArray(); int firstHexColumn = 8 // 8 characters for the address + 3; // 3 spaces int firstCharColumn = firstHexColumn + (int)arg[0] * 3 // - 2 digit for the hexadecimal value and 1 space + ((int)arg[0] - 1) / 8 // - 1 extra space every 8 characters from the 9th + 2; // 2 spaces int lineLength = firstCharColumn + (int)arg[0] // - characters to show the ascii value + Environment.NewLine.Length; // Carriage return and line feed (should normally be 2) char[] line = (new String(' ', lineLength - 2) + Environment.NewLine).ToCharArray(); int expectedLines = (bytesLength + (int)arg[0] - 1) / (int)arg[0]; StringBuilder result = new StringBuilder(expectedLines * lineLength); for (int i = 0; i < bytesLength; i += (int)arg[0]) { line[0] = HexChars[(i >> 28) & 0xF]; line[1] = HexChars[(i >> 24) & 0xF]; line[2] = HexChars[(i >> 20) & 0xF]; line[3] = HexChars[(i >> 16) & 0xF]; line[4] = HexChars[(i >> 12) & 0xF]; line[5] = HexChars[(i >> 8) & 0xF]; line[6] = HexChars[(i >> 4) & 0xF]; line[7] = HexChars[(i >> 0) & 0xF]; int hexColumn = firstHexColumn; int charColumn = firstCharColumn; for (int j = 0; j < (int)arg[0]; j++) { if (j > 0 && (j & 7) == 0) { hexColumn++; } if (i + j >= bytesLength) { line[hexColumn] = ' '; line[hexColumn + 1] = ' '; line[charColumn] = ' '; } else { byte b = bytes[i + j]; line[hexColumn] = HexChars[(b >> 4) & 0xF]; line[hexColumn + 1] = HexChars[b & 0xF]; line[charColumn] = (b < 32 ? '·' : (char)b); } hexColumn += 3; charColumn++; } result.Append(line); } string fff = result.ToString(); //file_log.WriteLine(fff); if ((bool)arg[1] == true) { Console.WriteLine(fff); } } }