public IAsyncSession <DuplexMessage> OpenSession(IoSession ioSession) { IAsyncSession <DuplexMessage> session = null; using (var locker = doubleCheckLock.Accquire( () => sessionMap.TryGetValue(ioSession.RemoteEndPoint.ToString(), out session))) { if (locker.Locked) { using (var scope = ObjectHost.Host.BeginLifetimeScope()) { session = scope.Resolve <IAsyncSession <DuplexMessage> >( new NamedParameter("heartbeatTimeout", HEARTBEAT_TIMEOUT), new NamedParameter("writerIdleTime", WRITER_IDLE_TIME)); if (session.Open(ioSession)) { sessionMap[ioSession.RemoteEndPoint.ToString()] = session; } else { throw new SessionOpenException(ioSession.RemoteEndPoint as IPEndPoint); } } } else { sessionMap.TryRemove(ioSession.RemoteEndPoint.ToString(), out session); throw new SessionOpenException(ioSession.RemoteEndPoint as IPEndPoint); } } return(session); }
private IAsyncSession <DuplexMessage> OpenSession(ConnectionConfig config) { IAsyncSession <DuplexMessage> session = null; using (var locker = doubleCheckLock.Accquire( () => !sessionMap.TryGetValue(config.EndPoint.ToString(), out session))) { if (locker.Locked) { using (var scope = ObjectHost.Host.BeginLifetimeScope()) { session = scope.Resolve <IAsyncSession <DuplexMessage> >( new NamedParameter("heartbeatTimeout", HEARTBEAT_TIMEOUT), new NamedParameter("writerIdleTime", WRITER_IDLE_TIME)); session.Open(config); sessionMap[config.EndPoint.ToString()] = session; } } else { if (!session.Connected) { sessionMap.TryRemove(config.EndPoint.ToString(), out session); session = OpenSession(config); } } } return(session); }