* The main message flow state machine. We start in the MSG_FLOW_UNINITED or * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and * transitions are as follows: * * MSG_FLOW_UNINITED MSG_FLOW_FINISHED * | | * +-----------------------+ * v * MSG_FLOW_WRITING <---> MSG_FLOW_READING * | * V * MSG_FLOW_FINISHED * | * V * [SUCCESS] * * We may exit at any point due to an error or NBIO event. If an NBIO event * occurs then we restart at the point we left off when we are recalled. * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them. * * In addition to the above there is also the MSG_FLOW_ERROR state. We can move * into that state at any point in the event that an irrecoverable error occurs. * * Valid return values are: * 1: Success * <=0: NBIO or error
可以明显看到消息流状态机的几个状态的转移过程,对于几个状态在此做详细释义:
MSG_FLOW_UNINITED:握手尚未开始
MSG_FLOW_ERROR:当前连接发生错误
MSG_FLOW_READING:当前正读取消息
MSG_FLOW_WRITING:当前正写入消息
MSG_FLOW_FINISHED:握手结束
3.2 读状态机 读状态机是属于消息流状态机的子状态机,这里我们单独拿出来分析
* This function implements the sub-state machine when the message flow is in * MSG_FLOW_READING. The valid sub-states and transitions are: * * READ_STATE_HEADER <--+<-------------+ * | | | * v | | * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS * | | * +----------------------------+ * v * [SUB_STATE_FINISHED] * * READ_STATE_HEADER has the responsibility for reading in the message header * and transitioning the state of the handshake state machine. * * READ_STATE_BODY reads in the rest of the message and then subsequently * processes it. * * READ_STATE_POST_PROCESS is an optional step that may occur if some post * processing activity performed on the message may block. * * Any of the above states could result in an NBIO event occurring in which case * control returns to the calling application. When this function is recalled we * will resume in the same state where we left off.
* This function implements the sub-state machine when the message flow is in * MSG_FLOW_WRITING. The valid sub-states and transitions are: * * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED] * | | * | v * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE] * | | * | v * | WRITE_STATE_SEND * | | * | v * | WRITE_STATE_POST_WORK * | | * +-------------+ * * WRITE_STATE_TRANSITION transitions the state of the handshake state machine
* WRITE_STATE_PRE_WORK performs any work necessary to prepare the later * sending of the message. This could result in an NBIO event occurring in * which case control returns to the calling application. When this function * is recalled we will resume in the same state where we left off. * * WRITE_STATE_SEND sends the message and performs any work to be done after * sending. * * WRITE_STATE_POST_WORK performs any work necessary after the sending of the * message has been completed. As for WRITE_STATE_PRE_WORK this could also * result in an NBIO event.