public override void AfterReceive(Buffer buffer, int length)
 {
     if (rxTransformReady)
     {
         BufferTransform.InverseTransform(buffer, length);
     }
 }
 public override bool BeforeSend(Buffer buffer, int length)
 {
     if (txTransformReady)
     {
         BufferTransform.Transform(buffer, length);
         return true;
     }
     return false;
 }
 public override void Release()
 {
     if (ReferenceEquals(BufferTransform, null))
     {
         return;
     }
     BufferTransform.Dispose();
     BufferTransform = null;
 }
        public override bool Process(Event e)
        {
            switch (e.GetTypeId())
            {
                case (int)LinkEventType.HandshakeReq:
                    {
                        var req = (HandshakeReq)e;
                        var resp = new HandshakeResp { _Transform = false };
                        byte[] response = null;
                        try
                        {
                            ManualResetEvent waitHandle =
                                SignalPool.Acquire(Session.InternalHandle);
                            waitHandle.WaitOne(new TimeSpan(0, 0, 30));
                            SignalPool.Release(Session.InternalHandle);
                            response = BufferTransform.Handshake(req.Data);
                        }
                        catch (Exception ex)
                        {
                            Trace.Error("{0} {1} error handshaking : {2}",
                                Session.Link.Name, Session.InternalHandle, ex.ToString());
                        }
                        if (response != null)
                        {
                            resp.Data = response;
                        }
                        Session.Send(resp);
                    }
                    break;
                case (int)LinkEventType.HandshakeResp:
                    {
                        var ack = new HandshakeAck { _Transform = false };
                        var resp = (HandshakeResp)e;
                        try
                        {
                            if (BufferTransform.FinalizeHandshake(resp.Data))
                            {
                                rxTransformReady = true;
                                ack.Result = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.Error("{0} {1} error finishing handshake : {2}",
                                Session.Link.Name, Session.InternalHandle, ex.ToString());
                        }
                        Session.Send(ack);
                    }
                    break;
                case (int)LinkEventType.HandshakeAck:
                    {
                        var ack = (HandshakeAck)e;
                        bool result = ack.Result;

                        if (result)
                        {
                            txTransformReady = true;
                        }

                        Session.Link.OnLinkSessionConnectedInternal(result, (result ? Session: null));
                    }
                    break;
                default:
                    return false;
            }
            return true;
        }