/// <summary> /// Add the socket of the KAS in the select sets as needed and manage /// ktlstunnel.exe processes. /// </summary> private void PrepareStateForSelect(KcmKas k, SelectSockets selectSockets, bool quenchFlag, ref bool connWatchFlag) { // Note: the KAS should never have received a message when this function // is called. The function UpdateStateAfterSelect() is responsible for // doing all the transfers and handling any message received after these // transfers. try { k.CheckNoMessageReceivedInvariant(); if (k.ConnStatus == KcmKasConnectionStatus.Scheduled) { // Start ktlstunnel.exe. k.ConnStatus = KcmKasConnectionStatus.Connecting; k.Tunnel.BeginConnect(); } if (k.ConnStatus == KcmKasConnectionStatus.Connecting) { // The TCP connection is now open. if (k.Tunnel.CheckConnect(0)) { // Send the select role command. k.SendSelectRoleMsg(); // Wait for the reply to arrive. k.ConnStatus = KcmKasConnectionStatus.RoleReply; } // Wait for the TCP connection to be established. We busy wait // to monitor the status of ktlstunnel.exe regularly, to detect // the case where the connection fails. else connWatchFlag = true; } if (k.ConnStatus == KcmKasConnectionStatus.RoleReply) { // Wait for the reply to arrive. if (!quenchFlag) k.Tunnel.UpdateSelect(selectSockets); } if (k.ConnStatus == KcmKasConnectionStatus.Connected) { // Send the next message, if possible. k.SendNextQueuedMsgIfNeeded(); if (!quenchFlag) k.Tunnel.UpdateSelect(selectSockets); } k.CheckNoMessageReceivedInvariant(); } catch (Exception ex) { HandleDisconnectedKas(k, ex); } }