/// <summary> /// Analyse the result of the select() call for the specified KAS. /// </summary> private void UpdateStateAfterSelect(TbxAppServer k, SelectSockets selectSockets) { try { k.CheckNoMessageReceivedInvariant(); // We have nothing to do if we don't have an established TCP // connection. if (k.ConnStatus != TcmAppServerConStatus.Connected && k.ConnStatus != TcmAppServerConStatus.RoleReply) { return; } // Perform transfers only if the socket is ready. Debug.Assert(k.Tunnel.Sock != null); if (!selectSockets.InReadOrWrite(k.Tunnel.Sock)) { return; } // Do up to 20 transfers (the limit exists for quenching purposes). for (int i = 0; i < 20; i++) { // Send a message if possible. k.SendNextQueuedMsgIfNeeded(); // Remember if we are sending a message. bool sendingFlag = k.Tunnel.IsSendingMessage(); // Do transfers. k.Tunnel.DoXfer(); // Stop if no message has been received and no message has been sent. if (!k.Tunnel.HasReceivedMessage() && (!sendingFlag || k.Tunnel.IsSendingMessage())) { break; } // Process the message received. if (k.Tunnel.HasReceivedMessage()) { ProcessIncomingAppServerMessage(k, k.Tunnel.GetMsg()); } } k.CheckNoMessageReceivedInvariant(); } catch (Exception ex) { HandleDisconnectedKas(k, ex); } }
/// <summary> /// Add the socket of the KAS in the select sets as needed and manage /// ktlstunnel.exe processes. /// </summary> private void PrepareStateForSelect(TbxAppServer 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 == TcmAppServerConStatus.Scheduled) { // Start ktlstunnel.exe. k.ConnStatus = TcmAppServerConStatus.Connecting; k.Tunnel.BeginConnect(); } if (k.ConnStatus == TcmAppServerConStatus.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 = TcmAppServerConStatus.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 == TcmAppServerConStatus.RoleReply) { // Wait for the reply to arrive. if (!quenchFlag) { k.Tunnel.UpdateSelect(selectSockets); } } if (k.ConnStatus == TcmAppServerConStatus.Connected) { // Send the next message, if possible. k.SendNextQueuedMsgIfNeeded(); if (!quenchFlag) { k.Tunnel.UpdateSelect(selectSockets); } } k.CheckNoMessageReceivedInvariant(); } catch (Exception ex) { HandleDisconnectedKas(k, ex); } }