private void _BeginRead(Secure_Stream c) { try { var state = new MessageObject(); state.Client = c; c.Client.BeginReceive(state.HeaderBuffer, 0, MessageObject.HeaderSize, 0, new AsyncCallback(ReadHeaderCallback), state); } catch(Exception e) { if(DisconnectEvent != null) DisconnectEvent(this); Debug.WriteLine(e.Message); } }
protected void Write(Tcp_Message m) { try { using(AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) { aes.KeySize = _MySessionKey.Length * 8; aes.Key = _MySessionKey; aes.GenerateIV(); aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; using(ICryptoTransform encrypt = aes.CreateEncryptor()) { var sendbuffer = Tcp_Message.ToBuffer(m); var headermessage = new MessageObject(); headermessage.Client = this; headermessage.MessageBuffer = headermessage.HeaderBuffer; var datamessage = new MessageObject(); datamessage.Client = this; datamessage.MessageBuffer = encrypt.TransformFinalBlock(sendbuffer, 0, sendbuffer.Length); Buffer.BlockCopy(aes.IV, 0, headermessage.MessageBuffer, 0, aes.IV.Length); var len = BitConverter.GetBytes(datamessage.MessageBuffer.Length); Buffer.BlockCopy(len, 0, headermessage.MessageBuffer, aes.IV.Length, len.Length); // Debug.WriteLine("Sending "); // _SendResetEvent.WaitOne(); Client.BeginSend(headermessage.MessageBuffer, 0, headermessage.MessageBuffer.Length, 0, new AsyncCallback(SendMessageCallback), headermessage); Client.BeginSend(datamessage.MessageBuffer, 0, datamessage.MessageBuffer.Length, 0, new AsyncCallback(SendMessageCallback), datamessage); } } } catch(Exception e) { // _SendResetEvent.Set();//just in case another thread was waiting if(DisconnectEvent != null) DisconnectEvent(this); Debug.WriteLine(e.Message); } }