/// <summary> /// Analyse the result of the select() call for the specified KAS. /// </summary> private void UpdateStateAfterSelect(KcmKas k, SelectSockets selectSockets) { try { k.CheckNoMessageReceivedInvariant(); // We have nothing to do if we don't have an established TCP // connection. if (k.ConnStatus != KcmKasConnectionStatus.Connected && k.ConnStatus != KcmKasConnectionStatus.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()) ProcessIncomingKasMessage(k, k.Tunnel.GetMsg()); } k.CheckNoMessageReceivedInvariant(); } catch (Exception ex) { HandleDisconnectedKas(k, ex); } }
/// <summary> /// Handle a message received from a KAS. /// </summary> private void ProcessIncomingKasMessage(KcmKas k, AnpMsg msg) { if (k.ConnStatus == KcmKasConnectionStatus.RoleReply) { if (msg.Type == KAnpType.KANP_RES_FAIL_MUST_UPGRADE) throw new Exception("The KWM is too old, it needs to be upgraded to communicate with " + k.KasID.Host + " which has protocol version " + msg.Minor); else if (msg.Type != KAnpType.KANP_RES_OK) throw new Exception(msg.Elements[1].String); else if (msg.Minor < KAnp.LastCompMinor) throw new Exception("The KCD at " + k.KasID.Host + " is too old and needs to be upgraded."); else HandleConnectedKas(k, Math.Min(msg.Minor, KAnp.Minor)); } else m_toWmAnpMsgList.Add(new KcmAnpMsg(msg, k.KasID)); }
/// <summary> /// Mark the KAS as disconnected, add a control message for the KAS in /// the control message list and add the KAS to the disconnected list. /// </summary> private void HandleDisconnectedKas(KcmKas k, Exception ex) { if (ex != null) Logging.Log(2, "KAS " + k.KasID.Host + " exception: " + ex.Message); if (k.Tunnel != null) k.Tunnel.Disconnect(); k.ConnStatus = KcmKasConnectionStatus.Disconnected; k.Ex = ex; m_toWmControlMsgList.Add(new KcmDisconnectionNotice(k.KasID, k.Ex)); m_disconnectedList.Add(k); }
/// <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); } }
/// <summary> /// Mark the KAS as connected and add a control message for the KAS in /// the control message list. /// </summary> private void HandleConnectedKas(KcmKas k, UInt32 minor) { k.ConnStatus = KcmKasConnectionStatus.Connected; m_toWmControlMsgList.Add(new KcmConnectionNotice(k.KasID, minor)); }