QuickFix/J:Session状态

首先我们看下SessionState.java中关于状态的字段:

private boolean logonSent;
private boolean logonReceived;
private boolean logoutSent;
private boolean logoutReceived = false;
private boolean resetSent;
private boolean resetReceived;
如果判断fix是否正常连接,判断:logonSent && logonReceived
logonSent: 发送Logon Message置为true;在断连时会置为false,以下是Session中相关代码:
    private boolean generateLogon() throws IOException {
        //生成logon message报文
        final Message logon = messageFactory.create(sessionID.getBeginString(), MsgType.LOGON);
        //加解密,0-none
        logon.setInt(EncryptMethod.FIELD, 0);
        //心跳时间,默认为30s
        logon.setInt(HeartBtInt.FIELD, state.getHeartBeatInterval());
        //FIXT协议特殊字段处理
        if (sessionID.isFIXT()) {
            logon.setField(DefaultApplVerID.FIELD, senderDefaultApplVerID);
        }
        //配置信息:RefreshOnLogon, 如果配置为Y,则重置Session,但只对有shared state
        //比如MemoryStore,则会报错: memory store does not support refresh!
        if (refreshOnLogon) {
            refreshState();
        }
        //配置信息:ResetOnLogon, 如果配置为Y,则重置Session
        if (resetOnLogon) {
            resetState();
        }
        //判断是否要重置序列号
        if (isResetNeeded()) {
            logon.setBoolean(ResetSeqNumFlag.FIELD, true);
        }
        //上次消息的时间,当前时间
        state.setLastReceivedTime(SystemTime.currentTimeMillis());
        state.clearTestRequestCounter();
        //设置logonSent为true
        state.setLogonSent(true);
        logonAttempts++;

        if (enableNextExpectedMsgSeqNum) {
            final int nextExpectedMsgNum = getExpectedTargetNum();
            logon.setInt(NextExpectedMsgSeqNum.FIELD, nextExpectedMsgNum);
            state.setLastExpectedLogonNextSeqNum(nextExpectedMsgNum);
        }
        //设置logon其他field
        setLogonTags(logon);
        //发送logon消息
        return sendRaw(logon, 0);
    }
public void disconnect(String reason, boolean logError) throws IOException {
    try {
        final boolean logonReceived = state.isLogonReceived();
        final boolean logonSent = state.isLogonSent();

        synchronized (responderLock) {
            if (!hasResponder()) {
                if (!ENCOUNTERED_END_OF_STREAM.equals(reason)) {
                    getLog().onEvent("Already disconnected: " + reason);
                }
                return;
            }
            final String msg = "Disconnecting: " + reason;
            if (logError) {
                getLog().onErrorEvent(msg);
            } else {
                getLog().onEvent(msg);
            }
            responder.disconnect();
            setResponder(null);
        }
        
        //如果已经发起了logon,则发起logout退出登录
        if (logonReceived || logonSent) {
            try {
                application.onLogout(sessionID);
            } catch (final Throwable t) {
                logApplicationException("onLogout()", t);
            }

            stateListener.onLogout(sessionID);
        }
    } finally {
        //最后将以下状态置为false
        state.setLogonReceived(false);
        state.setLogonSent(false);
        state.setLogoutSent(false);
        state.setLogoutReceived(false);
        state.setResetReceived(false);
        state.setResetSent(false);
        state.getMessageQueue().clear();
        state.clearLogoutReason();
        state.setResendRange(0, 0);

        //如果设置了ResetOnDisconnect,则断连后重置Session,序列号重1开始
        if (resetOnDisconnect || state.isResetStatePending()) {
            resetState();
        }
    }
}

logonReceived:发起nextLogon时,会置为true;断连时会置为false(源码如上);

logoutSent:发起Logout时,置为true;当nextLogon或disconnect时,置为false;

logoutReceived:发起nextLogout时,会置为true;当nextLogon或disconnect时,置为false;

resetSent:发送消息前,会检查tag:resetSeqNumFlag的值;发起nextLogon或disconnect时,会置为false;

resetReceived:发送logon前,会检查tag:resetSeqNumFlag的值,或当resetSent=Y且序号为1时,会置为ture;发起nextLogon或disconnect时,会置为false;

你可能感兴趣的:(QuickFix/J,quickfix/j,Session)