/// <summary> /// Process a control message received from the WM. /// </summary> private void ProcessWmControlMsg(TcmControlMsg msg) { Debug.Assert(msg is TcmConnectionRequest); TcmConnectionRequest req = (TcmConnectionRequest)msg; // Handle new KAS to connect. if (req.ConnectFlag) { Debug.Assert(!m_kasTree.ContainsKey(req.KasID)); m_kasTree[req.KasID] = new TbxAppServer(req.KasID); } // Disconnect the KAS, if we didn't disconnect it yet. else { if (!m_kasTree.ContainsKey(req.KasID)) { return; } TbxAppServer kas = m_kasTree[req.KasID]; if (kas.ConnStatus == TcmAppServerConStatus.Disconnected) { return; } HandleDisconnectedKas(kas, null); } }
/// <summary> /// Handle a message received from a KAS. /// </summary> private void ProcessIncomingAppServerMessage(TbxAppServer k, AnpMsg msg) { if (k.ConnStatus == TcmAppServerConStatus.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 { HandleConnectedAppServer(k, Math.Min(msg.Minor, KAnp.Minor)); } } else { m_toWmAnpMsgList.Add(new TcmAnpMsg(msg, k.KasID)); } }
/// <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> /// 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(TbxAppServer 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 = TcmAppServerConStatus.Disconnected; k.Ex = ex; m_toWmControlMsgList.Add(new TcmDisconnectionNotice(k.KasID, k.Ex)); m_disconnectedList.Add(k); }
/// <summary> /// Process an ANP message received from the WM. /// </summary> private void ProcessWmAnpMsg(TcmAnpMsg msg) { // Ignore messages not destined to connected KASes. if (!m_kasTree.ContainsKey(msg.KasID)) { return; } TbxAppServer kas = m_kasTree[msg.KasID]; if (kas.ConnStatus != TcmAppServerConStatus.Connected) { return; } // Enqueue the message. kas.SendQueue.Enqueue(msg.Msg); }
/// <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); } }
/// <summary> /// Mark the KAS as connected and add a control message for the KAS in /// the control message list. /// </summary> private void HandleConnectedAppServer(TbxAppServer k, UInt32 minor) { k.ConnStatus = TcmAppServerConStatus.Connected; m_toWmControlMsgList.Add(new TcmConnectionNotice(k.KasID, minor)); }