public Decode(byte[] buffer) { try { byte[] _tempbuff = new byte[buffer.Length]; EncDcd endc = new EncDcd(); _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 { 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); Decode de = new Decode(buffer); // only get get the size first LogConsole.Show("bufCount: {0} dataSize: {1}", bufCount, de.dataSize); if (bufCount >= (de.dataSize - 2)) // that's a complete packet, lets 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 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]; Utily.EncDcd end = new Utily.EncDcd(); byte[] dddxx = end.Crypt(buffer); Buffer.BlockCopy(dddxx, 0, _tempddd, 0, bufCount); LogDebug.HexDump(_tempddd, 16, true); } 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 void SendC(byte[] buff) { try { if (buff != null && buff.Length > 0 && clientSocket.Connected) { EncDcd encoded = new Utily.EncDcd(); clientSocket.Send(encoded.Crypt(buff)); encoded = null; } } catch (Exception) { } }