home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
mandelc.zip
/
server.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-01
|
7KB
|
301 lines
#include "server.h"
serverMainThread::serverMainThread(serverProcess *pProcessNew)
:thread(FALSE, pProcessNew)
{ pProcess = pProcessNew;
}
void serverMainThread::doSomething(void)
{ serverThread *pThread;
if (!(pThread = new serverThread(pProcess)))
return;
else
if (!pThread->isSuccessfull())
{ delete pThread;
return;
}
pThread->startThread();
while (1)
DosSleep(1000*60);
}
serverThread::serverThread(serverProcess *pProcessNew)
:thread(TRUE, pProcessNew)
{ pProcess = pProcessNew;
}
Boolean serverThread::create(void)
{ APIRET iRet;
if (iRet = DosCreateNPipe((unsigned char*)pProcess->pPipeName,
&hPipe,
NP_NOWRITEBEHIND | NP_NOINHERIT | NP_ACCESS_DUPLEX,
NP_WAIT | NP_TYPE_MESSAGE | NP_READMODE_MESSAGE | NP_UNLIMITED_INSTANCES,
1024*16,
1024*16,
0))
{ fprintf(stderr,
"DosCreateNPipe() returned error code %u!\n",
iRet);
return FALSE;
}
DosSetPriority(PRTYS_THREAD,
PRTYC_IDLETIME,
-pTIB->tib_ptib2->tib2_ulpri,
pTIB->tib_ptib2->tib2_ultid);
return TRUE;
}
void serverThread::destruct(void)
{ DosClose(hPipe);
}
Boolean serverThread::calculate(structRequest *pStructR)
{ unsigned int iSize;
unsigned long iBytesWritten;
APIRET iRet;
unsigned char *paiData;
if (pStructR->iSizeX > 16384)
return FALSE;
if (!(paiData
= (unsigned char*)alloca(iSize = pStructR->iSizeX*sizeof*paiData)))
return FALSE;
(*pFctCalculate)(
pStructR->iSizeX,
pStructR->iMaxColors,
pStructR->iMaxIterations,
paiData,
pStructR->dLimit,
pStructR->dXdx,
pStructR->dYdx,
pStructR->dXa,
pStructR->dYa);
if (iRet = DosWrite(hPipe, paiData, iSize, &iBytesWritten))
{ fprintf(stderr,
"DosRead() returned error code %u!\n",
iRet);
return FALSE;
}
if (iBytesWritten != iSize)
{ fprintf(stderr,
"Wrong number of bytes written (%u)!\n",
iBytesWritten);
return FALSE;
}
return TRUE;
}
void serverThread::doSomething(void)
{ structRequest structR;
unsigned long iBytesRead;
unsigned int iRet;
do
{ serverThread *pThread = new serverThread(pProcess);
if (iRet = DosConnectNPipe(hPipe))
{ fprintf(stderr,
"DosConnectNPipe() returned error code %u!\n",
iRet);
continue;
}
if (pThread)
if (pThread->isSuccessfull())
{ pThread->startThread();
break;
}
else
{ delete pThread;
fprintf(stderr,
"Could not successfull create thread!\n");
if (iRet = DosDisConnectNPipe(hPipe))
fprintf(stderr,
"DosDisConnectNPipe() returned error code %u!\n",
iRet);
continue;
} else
{ fprintf(stderr, "Could not create thread!\n");
if (iRet = DosDisConnectNPipe(hPipe))
fprintf(stderr,
"DosDisConnectNPipe() returned error code %u!\n",
iRet); continue;
}
} while (1);
{ char acBuffer[1024];
unsigned long iBytes;
dLLName *pDLLName;
if (DosRead(hPipe, acBuffer, sizeof acBuffer, &iBytes)
|| iBytes < 2)
{ fprintf(stderr, "Could`t read dll name!");
if (iRet = DosDisConnectNPipe(hPipe))
fprintf(stderr,
"DosDisConnectNPipe() returned error code %u!\n",
iRet);
return; }
pDLLName = (dLLName*)acBuffer;
if (!pProcess->chainDLLNames.foreach(
idForeachDLLNameGetThisByDescription,
(void*)&pDLLName))
{ fprintf(stderr,
"Could`t find matching DLL for description %s!\n",
acBuffer);
if (iRet = DosDisConnectNPipe(hPipe))
fprintf(stderr,
"DosDisConnectNPipe() returned error code %u!\n",
iRet);
return; }
pFctCalculate = pDLLName->pFctCalculate;
}
while (!(iRet = DosRead(hPipe,
&structR,
sizeof structR,
&iBytesRead))
&& iBytesRead == sizeof structR)
if (!calculate(&structR))
break;
if (iRet)
fprintf(stderr,
"DosRead() returned error code %u!\n",
iRet);
else
if (iBytesRead != sizeof structR)
fprintf(stderr,
"Invalid number of bytes in request!\n");
if (iRet = DosDisConnectNPipe(hPipe))
fprintf(stderr,
"DosDisConnectNPipe() returned error code %u!\n",
iRet);
}
serverProcess::serverProcess(int argc, char **argv):process(argc, argv)
{ if (argc == 3)
pPipeName = argv[2];
else
{ fprintf(stderr, "Usage: %s dllFile \\pipe\\myPipeName", argv[0]);
setNoSuccess();
}
}
Boolean serverProcess::create(void)
{ { FILE *pFile;
char acLine[1024];
if (!(pFile = fopen(argv[1], "r")))
{ fprintf(stderr, "Cannot open %s!\n", argv[1]);
return FALSE;
}
acLine[sizeof acLine - 1] = 0;
while (fgets(acLine, sizeof acLine - 1, pFile))
{ dLLName *p;
char *pPos;
if (pPos = strchr(acLine, '\n'))
*pPos = 0;
if (p = new dLLName(acLine, &chainDLLNames))
if (p->isSuccessfull())
continue;
else
delete p;
fprintf(stderr, "Error loading DLL %s!\n", acLine);
return FALSE;
}
fclose(pFile);
} if (!(pProcessThread = new serverMainThread(this)))
return FALSE;
else
if (pProcessThread->isSuccessfull())
return TRUE;
else
{ delete pProcessThread;
pProcessThread = (thread*)0;
return FALSE;
}
}
int main(int argc, char **argv)
{ serverProcess *pProcess = new serverProcess(argc, argv);
if (pProcess)
pProcess->run();
delete pProcess;
DosExit(EXIT_PROCESS, 0);
return 0;
}
dLLName::dLLName(char *pString, chain *pParent)
:stringChainElement(pString, pParent)
{ char acBuffer[1024];
char *(*pFctGetName)(void);
APIRET iRet;
if (iRet = DosLoadModule((unsigned char*)acBuffer, sizeof acBuffer,
(unsigned char*)pString, &hDLL))
{ hDLL = (HMODULE)0;
setNoSuccess();
fprintf(stderr, "Error returned from DosLoadModule() == %u\n",
iRet);
return;
}
/*
@getName$qv @1
@calculateFractal$quiuiuipucddddd @2
*/
if (iRet = DosQueryProcAddr(hDLL, 0, (unsigned char*)"_getName",
(PFN*)&pFctGetName))
{ setNoSuccess();
fprintf(stderr, "Error returned from DosQueryProcAddr(%s) == %u\n",
"getName",
iRet);
return;
}
if (!(pDescription = (*pFctGetName)()))
{ setNoSuccess();
return;
}
if (iRet = DosQueryProcAddr(hDLL,
0,
(unsigned char*)"_calculateFractal",
(PFN*)&pFctCalculate))
{ setNoSuccess();
fprintf(stderr, "Error returned from DosQueryProcAddr(%s) == %u\n",
"calculateFractal",
iRet);
return;
}
}
dLLName::~dLLName(void)
{ if (hDLL)
DosFreeModule(hDLL);
}
int dLLName::toBeCalledForeachElement(unsigned int iMsg, void *pDummy)
{ switch (iMsg)
{ default:
return stringChainElement
::toBeCalledForeachElement(iMsg, pDummy);
case idForeachDLLNameGetThisByDescription:
if (!strcmp(*(char**)pDummy, pDescription))
{ *(dLLName**)pDummy = this;
return -1;
}
else
return 0;
}
}