ArraySegment <byte> Output_WaitForAuth(DeliveryConnection output, ArraySegment <byte> data) { //(x.1)读取数据 var byteList = output.ext as List <byte>; if (byteList == null) { output.ext = byteList = new List <byte>(); } byteList.AddRange(data); if (byteList.Count < authTokenBytes.Length) { return(null); } #region (x.2)收到数据的长度 等于 token长度 if (byteList.Count == authTokenBytes.Length) { if (authTokenBytes.SequenceEqual(byteList)) { //权限认证通过 Logger.Info("发起连接--成功-权限认证通过"); output.ext = null; output.OnGetFrame = Output_WaitForStartMsg; return(null); } else { //权限认证不通过 Logger.Info("收到连接-失败-权限认证不通过"); output.ext = null; output.OnGetFrame = null; output.Close(); return(null); } } #endregion #region (x.3)收到数据的长度 大于 token长度 if (authTokenBytes.SequenceEqual(byteList.Take(authTokenBytes.Length))) { //权限认证通过 且已经接受到开始标志 Logger.Info("发起连接--成功-权限认证通过"); output.ext = null; output.OnGetFrame = null; Output_OnReceiveStartMsg(output, byteList.Skip(authTokenBytes.Length + 1).ToArray()); return(null); } else { //权限认证不通过 Logger.Info("收到连接-失败-权限认证不通过"); output.ext = null; output.OnGetFrame = null; output.Close(); return(null); } #endregion }
ArraySegment <byte> Output_OnGetFrame(DeliveryConnection output, ArraySegment <byte> data) { //(x.1)读取数据 var byteList = output.ext as List <byte>; if (byteList == null) { output.ext = byteList = new List <byte>(); } byteList.AddRange(data); if (byteList.Count < authTokenBytes.Length) { return(null); } //(x.2)匹配不通过 if (byteList.Count != authTokenBytes.Length || !authTokenBytes.SequenceEqual(byteList)) { Logger.Info($" [{output.GetHashCode()}] 权限认证-不通过"); output.Close(); return(null); } //(x.3)匹配通过 Logger.Info($" [{output.GetHashCode()}] 权限认证-通过"); output.ext = null; output.OnGetFrame = null; //(x.4)发送验证通过标志 output.SendFrameAsync(authTokenBytes); #region (x.5)进行桥接或放入连接池 DeliveryConnection input; #region get input while (true) { if (!inputQueue.TryDequeue(out input)) { break; } if (input.IsConnected) { break; } } #endregion if (input == null) { outputQueue.Enqueue(output); } else { input.Bind(output); //OutPut_SendStartMsg output.SendFrameAsync(StartMsg); Logger.Info($" [{output.GetHashCode()}] 转发成功"); } #endregion return(null); }