Download this source code in text format (Descriptor.cpp)
/***********************************************************
* WindowMud - Window Mud Server *
* File: Descriptor.cpp *
* Usage: Maintains a linked list of connection data *
* *
* No warranty is given or implied. *
* There is no license associated with this code. *
* There are no restrictions on the use of this code. *
* This code may be used in any way the reader wishes. *
* No credit or credits are given and none are required. *
* The reader may take this code and claim they wrote it. *
************************************************************/
/***********************************************************
* Includes *
************************************************************/
#include "stdafx.h" // precompiled headers
#include "Descriptor.h"
// Server
#include "Dnode.h"
#include "Utility.h"
/***********************************************************
* Globals *
************************************************************/
extern Dnode *pDnodeActor;
extern CString ErrorMsg;
Dnode *pDnodeCursor;
Dnode *pDnodeHead;
/***********************************************************
* Descriptor constructor *
***********************************************************/
Descriptor::Descriptor()
{
}
/***********************************************************
* Descriptor destructor *
***********************************************************/
Descriptor::~Descriptor()
{
}
////////////////////////////////////////////////////////////
// Public functions static //
////////////////////////////////////////////////////////////
/***********************************************************
* Append new connection to connection list *
***********************************************************/
void Descriptor::AppendIt()
{
pDnodeActor->pDnodePrev = pDnodeHead->pDnodePrev;
pDnodeHead->pDnodePrev->pDnodeNext = pDnodeActor;
pDnodeHead->pDnodePrev = pDnodeActor;
pDnodeActor->pDnodeNext = pDnodeHead;
}
/***********************************************************
* Clear descriptor linked list *
***********************************************************/
void Descriptor::ClearDescriptor()
{
delete pDnodeHead;
pDnodeHead = NULL;
}
/***********************************************************
* Delete connection from connection list and close socket *
***********************************************************/
bool Descriptor::DeleteNode()
{
Dnode *pDnode;
int Result;
if (!pDnodeCursor->DnodeFd)
{
return false;
}
// Close the socket (pg 70)
Result = ::closesocket(pDnodeCursor->DnodeFd);
if (Result != 0)
{ // Close socket failed
ErrorMsg = "Descriptor::DeleteNode - Error: closesocket";
Utility::BlowUp();
}
pDnodeActor = NULL;
pDnode = pDnodeCursor->pDnodePrev;
delete pDnodeCursor;
pDnodeCursor = pDnode;
return true;
}
/***********************************************************
* End of Dnode list? *
***********************************************************/
bool Descriptor::EndOfDnodeList()
{
return pDnodeCursor->DnodeFd ? false : true; // Ternary operator
}
/***********************************************************
* Get Dnode pointer *
***********************************************************/
Dnode *Descriptor::GetDnode()
{
return pDnodeCursor;
}
/***********************************************************
* Initialize descriptor pointers *
***********************************************************/
void Descriptor::InitDescriptor()
{
pDnodeHead = new Dnode(0, "");
pDnodeHead->pDnodeNext = pDnodeHead;
pDnodeHead->pDnodePrev = pDnodeHead;
pDnodeCursor = pDnodeHead;
}
/***********************************************************
* Set Dnode pointer to the first Dnode in the list *
***********************************************************/
void Descriptor::SetpDnodeCursorFirst()
{
pDnodeCursor = pDnodeHead->pDnodeNext;
}
/***********************************************************
* Set Dnode pointer to the next Dnode in the list *
***********************************************************/
void Descriptor::SetpDnodeCursorNext()
{
pDnodeCursor = pDnodeCursor->pDnodeNext;
}
/***********************************************************
* Set Dnode pointer to the previous Dnode in the list *
***********************************************************/
void Descriptor::SetpDnodeCursorPrev()
{
pDnodeCursor = pDnodeCursor->pDnodePrev;
}