void IAleTunnelObserver.OnAleFrameArrival(AleTunnel theConnection, AleFrame theFrame) { try { // 只处理ConnectionRequest帧。 if (theFrame.FrameType != AleFrameType.ConnectionRequest) { throw new Exception(string.Format("在临时AleTunnel上收到了非CR帧,关闭此连接!本地终结点={0},远程终结点={1}。", theConnection.LocalEndPoint, theConnection.RemoteEndPoint)); } // 确认此连接是“AleServerTunnel对象”。 var tunnel = theConnection as AleServerTunnel; if (tunnel == null) { throw new Exception(string.Format("在非AleServerTunnel上收到了CR帧!本地终结点={0},远程终结点={1}。", theConnection.LocalEndPoint, theConnection.RemoteEndPoint)); } // 处理ALE帧。 var aleCrData = theFrame.UserData as AleConnectionRequest; var key = this.BuildSaiConnectionID(aleCrData.ResponderID, aleCrData.InitiatorID); SaiConnectionServer saiConnection; lock (_saiConnectionsLock) { saiConnection = this.GetSaiConnection(key) as SaiConnectionServer; if (saiConnection == null) { saiConnection = this.CreateSaiConnectionServer(aleCrData.InitiatorID); this.AddSaiConnection(key, saiConnection); saiConnection.Open(); } } // 交给SaiConnection处理。 saiConnection.HandleAleConnectionRequestFrame(tunnel, theFrame); // 从临时链表中移除。 _serverTunnels.Remove(tunnel); } catch (System.Exception ex) { LogUtility.Error(string.Format("{0}", ex)); _serverTunnels.Remove(theConnection as AleServerTunnel); theConnection.Close(); } }
void IAleTunnelObserver.OnTcpDisconnected(AleTunnel theConnection, string reason) { try { // 进入此函数表示客户端连接后没有发送任何数据就关闭(可能是客户端主动关闭,也可能是在规定时间里没有发送CR帧而被服务器关闭)。 _serverTunnels.Remove(theConnection as AleServerTunnel); theConnection.Close(); } catch (System.Exception ex) { LogUtility.Error(ex.ToString()); } }