// --------------------------------------------------------------------------------------------------------------
//
//   AmiGnut 0.9.0 List Module
//
//   Copyright (C) 2001 Jay Cornwall <jay@realfantasy.demon.co.uk>
//
//   This is free software distributed under the terms of the GNU Public License. See the file License.txt for
//   details.
//
// --------------------------------------------------------------------------------------------------------------

#include "List.h"

// --------------------------------------------------------------------------------------------------------------

void ClearConnectionList()
{
    struct ConnectionNode *connectionnode, *nextconnectionnode;
    struct Library *SocketBase;

    // Free all outstanding nodes in the ConnectionList, disconnecting if necessary

    // Retrieve the pointer to our task's copy of SocketBase

    SocketBase = GetSocketBase();

    // Clear the entries in the Connection Manager

    DoMethodMain(Gadgets[GID_CONNECTIONS_NLIST_MANAGER], MUIM_NList_Clear);

    // Cycle through the ConnectionList

    connectionnode = (struct ConnectionNode *)ConnectionList.mlh_Head;

    while ((nextconnectionnode = (struct ConnectionNode *)connectionnode->c_MinNode.mln_Succ))
    {
        // Do we need to disconnect this node?

        if ((connectionnode->c_Status == STATUS_CONNECTING) || (connectionnode->c_Status == STATUS_CONNECTED))
        {
            // Disconnect this node

            CloseSocket(connectionnode->c_Socket);
        } /* if */

        // Free the ConnectionNode

        if (connectionnode->c_RecvBuffer)
        {
            FreeVec(connectionnode->c_RecvBuffer);
        } /* if */

        if (connectionnode->c_SendBuffer)
        {
            FreeVec(connectionnode->c_SendBuffer);
        } /* if */

        FreeVec(connectionnode);

        // Go to next ConnectionNode

        connectionnode = nextconnectionnode;
    } /* while */

    // Clear the ConnectionList

    NewList((struct List *)&ConnectionList);
} /* ClearConnectionList() */

// --------------------------------------------------------------------------------------------------------------

struct ConnectionNode *FindConnectionNode(long sock)
{
    BOOL found = FALSE;
    struct ConnectionNode *connectionnode;

    // Find the ConnectionNode in ConnectionList with the given c_Socket value, and which is either
    // connecting or connected

    connectionnode = (struct ConnectionNode *)ConnectionList.mlh_Head;

    while (connectionnode->c_MinNode.mln_Succ && !(found))
    {
        if (connectionnode->c_Socket == sock)
        {
            // Check this ConnectionNode is either connecting or connected

            if ( (connectionnode->c_Status == STATUS_CONNECTING) || (connectionnode->c_Status == STATUS_CONNECTED) )
            {
                found = TRUE;
            } /* if */

            else
            {
                // Go to next ConnectionNode

                connectionnode = (struct ConnectionNode *)connectionnode->c_MinNode.mln_Succ;
            } /* else */
        } /* if */

        else
        {
            // Go to next ConnectionNode

            connectionnode = (struct ConnectionNode *)connectionnode->c_MinNode.mln_Succ;
        } /* else */
    } /* while */

    if (found)
    {
        return(connectionnode);
    } /* if */

    else
    {
        return(NULL);
    } /* else */
} /* FindConnectionNode() */

// --------------------------------------------------------------------------------------------------------------

void FreeMinList(struct MinList *minlist, UBYTE listtype)
{
    struct MinNode *minnode, *nextminnode;

    // Free the given MinList (and, based on its type, any additional data as well)

    minnode = minlist->mlh_Head;

    while ((nextminnode = minnode->mln_Succ))
    {
        // Free additional data

        if (listtype == LISTTYPE_CONNECTION)
        {
            if (((struct ConnectionNode *)minnode)->c_RecvBuffer)
            {
                FreeVec(((struct ConnectionNode *)minnode)->c_RecvBuffer);
            } /* if */

            if (((struct ConnectionNode *)minnode)->c_SendBuffer)
            {
                FreeVec(((struct ConnectionNode *)minnode)->c_RecvBuffer);
            } /* if */
        } /* if */

        // Free this MinNode

        FreeVec(minnode);

        // Go to next MinNode

        minnode = nextminnode;
    } /* while */

    // Clear the list

    NewList((struct List *)minlist);
} /* FreeMinList() */

// --------------------------------------------------------------------------------------------------------------

// End Of Text
