home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / packet / terminal / pr4w16 / source / srvdll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-21  |  15.2 KB  |  368 lines

  1. /*****************************************************************************/
  2. /* PROJEKT PR4WIN  Service DLL                                               */
  3. /*                                                                           */
  4. /* This is a sample for a Pr4Win Service implemented as a DLL                */
  5. /*                                                                           */
  6. /*                                                                           */
  7. /*                                                                           */
  8. /*                                                                           */
  9. /*                                                                           */
  10. /* BY:                                                                       */
  11. /* ----------                                                                */
  12. /* OE8DJK Bernd M. Stroj         18.01.1996                                  */
  13. /*                                                                           */
  14. /*****************************************************************************/
  15.  
  16. #include "windows.h"
  17. #include "stdlib.h"
  18. #include "string.h"
  19. #include "srvdll.h"
  20.  
  21.  
  22.  
  23. #define  VERSION  102                             // becones 1.02 in remote conmmand //SERV
  24.  
  25. #define  CR       13
  26. #define  LF       10
  27.  
  28.  
  29. typedef struct
  30.    {
  31.      long         hCon;                           // Handle to Connection
  32.      char         szCallSign[10];                 // E.G."OE8DJK-15"
  33.      char         szInpBuffer[128];               // contains complete inputline
  34.      short        sCount;                         // Characters in szInpBuffer
  35.      void         *ptPrivat;                      // pointer to private data 
  36.      void         *ptNextCon;                     // Daisy chain
  37.    } CON_DATA;
  38.  
  39. static   CON_DATA     *ptFirstCon = NULL;
  40.  
  41.  
  42. static   FARPROC      lpfnSnd2Con;                // Service -> Connect
  43. static   FARPROC      lpfnControl;                // Service connection
  44.  
  45.  
  46. static void      vAddCon      ( CON_DATA *ptCon );
  47. static void      vDelCon      ( long hCon );
  48. static CON_DATA *ptGetCon     ( long hCon );
  49. static void      vService     ( long hCon, CON_DATA *ptCon );
  50. static void      vExitService ( long hCon, CON_DATA *ptCon );
  51.  
  52.  
  53.  
  54.  
  55. /*----------------------------------------------------------------------------*/
  56. /* Procedure:  LibMain                                                        */
  57. /*             Called by LibEntry, when DLLis loaded                          */
  58. /*                                                                            */
  59. /* Parameters: hModule          Module Handle                                 */
  60. /*             wDataSeg         Data Segment                                  */
  61. /*             cbHeapSize       Heap Size                                     */
  62. /*             lpszCmdLine      Command Line                                  */
  63. /*                                                                            */
  64. /* Returns:    1 wenn OK                                                      */
  65. /*                                                                            */
  66. /*----------------------------------------------------------------------------*/
  67.  
  68. int FAR PASCAL LibMain (HANDLE hModule, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine)
  69. {
  70.   return (1);                                      
  71. }
  72.  
  73.  
  74. /*----------------------------------------------------------------------------*/
  75. /* Procedure:  WEP                                                            */
  76. /*             Called before a DLL is removed from memory                     */
  77. /*                                                                            */
  78. /* Parameters: bSystemExit                                                    */
  79. /*                                                                            */
  80. /* Returns:    1 wenn OK                                                      */
  81. /*                                                                            */
  82. /*----------------------------------------------------------------------------*/
  83.  
  84. int FAR PASCAL WEP (int bSystemExit)
  85. {
  86.    return (1);                                      
  87. }
  88.  
  89.  
  90. /*----------------------------------------------------------------------------*/
  91. /* Procedure:  iInitDll                                                       */
  92. /*             Initialisiert den Service, meldet Callback Fkt an.             */
  93. /*                                                                            */
  94. /* Parameters: ptInitServer                                                   */
  95. /*                                                                            */
  96. /* Returns:    1 if OK                                                        */
  97. /*                                                                            */
  98. /*----------------------------------------------------------------------------*/
  99.  
  100.  
  101. int FAR PASCAL _export iInitDll ( INIT_SERVICE *ptInitSrv )
  102. {
  103.    lpfnSnd2Con = ptInitSrv->lpfnAns;
  104.    lpfnControl = ptInitSrv->lpfnCtl;
  105.    return (1);
  106. }
  107.  
  108.  
  109. /*----------------------------------------------------------------------------*/
  110. /* Procedure:  iExitDll                                                       */
  111. /*                                                                            */
  112. /* Parameters: ptInitServer                                                   */
  113. /*                                                                            */
  114. /* Returns:    1 if OK                                                        */
  115. /*                                                                            */
  116. /*----------------------------------------------------------------------------*/
  117.  
  118.  
  119. int FAR PASCAL _export iExitDll ( void )
  120. {
  121.    return (1);
  122. }
  123.  
  124.  
  125. /*----------------------------------------------------------------------------*/
  126. /* Procedure:  iOpenSrv                                                       */
  127. /*             A Station requiers a connect to a service                      */
  128. /*                                                                            */
  129. /* Parameters: ptOpenSrv                                                      */
  130. /*                                                                            */
  131. /* Returns:    1 if OK                                                        */
  132. /*             0 if conenction is refused, apply a text in ptOpenSrv->stError */
  133. /*               to explain the reason for refuse !                           */
  134. /*                                                                            */
  135. /*----------------------------------------------------------------------------*/
  136.  
  137.  
  138. int FAR PASCAL _export iOpenSrv ( OPEN_SERVICE *ptOpenSrv )
  139. {
  140.    CON_DATA *ptCon;
  141.  
  142.    ptCon = malloc(sizeof(CON_DATA));
  143.    memset ( ptCon, 0x00, sizeof(CON_DATA));
  144.  
  145.    ptCon->hCon=ptOpenSrv->hCon;
  146.    strcpy(ptCon->szCallSign,ptOpenSrv->szCallSign);
  147.    vAddCon(ptCon);
  148.  
  149.    // A maximum of 127 characters is permited in welcome text !
  150.    
  151.    strcpy(ptOpenSrv->szError,"Welcome to the demo service, ");
  152.    strcat(ptOpenSrv->szError,ptOpenSrv->szCallSign);
  153.    strcat(ptOpenSrv->szError," !\n\r");
  154.    strcat(ptOpenSrv->szError,"Enter //EXIT to exit service !");
  155.  
  156.    return (1);
  157. }
  158.  
  159.  
  160. /*----------------------------------------------------------------------------*/
  161. /* Procedure:  iCloseSrv                                                      */
  162. /*             A Connect is closed without colosing the Service before.       */
  163. /*                                                                            */
  164. /* Parameters: hCon  Connection Handle = Connect Window                       */
  165. /*                                                                            */
  166. /* Returns:    1 if OK                                                        */
  167. /*                                                                            */
  168. /*----------------------------------------------------------------------------*/
  169.  
  170.  
  171. int FAR PASCAL _export iCloseSrv( long hCon )
  172. {
  173.    vDelCon ( hCon );
  174.    return (1);
  175. }
  176.  
  177.  
  178. /*----------------------------------------------------------------------------*/
  179. /* Procedure:  iSnd2Srv                                                       */
  180. /*             The connected station sends a data package to ther service.    */
  181. /*                                                                            */
  182. /* Parameters: ptSrv contains connection handle und Daten package.            */
  183. /*                                                                            */
  184. /* Returns:    1 wenn OK                                                      */
  185. /*                                                                            */
  186. /*----------------------------------------------------------------------------*/
  187.  
  188.  
  189. int FAR PASCAL _export iSnd2Srv( RXTX_SERVICE *ptSrv )
  190. {
  191.    CON_DATA   *ptCon;
  192.    short      i;
  193.  
  194.    ptCon=ptGetCon(ptSrv->hCon);
  195.    
  196.    for ( i=0; i < ptSrv->sPacLen; i++)
  197.    {
  198.       if (ptSrv->sPacket[i] == CR || ptSrv->sPacLen >= 127 )
  199.       {
  200.          ptCon->szInpBuffer[ptCon->sCount]='\0';
  201.          vService ( ptSrv->hCon, ptCon );       // proccess a mplete Line
  202.          ptCon->sCount=0;
  203.          
  204.       }
  205.       else
  206.          if (ptSrv->sPacket[i] != LF)
  207.              ptCon->szInpBuffer[ptCon->sCount++]=ptSrv->sPacket[i];
  208.    }
  209.    return (1);
  210. }
  211.  
  212.  
  213. /*----------------------------------------------------------------------------*/
  214. /* Procedure:  iVersion                                                       */
  215. /*             Get the version ot the DLL.                                    */
  216. /*                                                                            */
  217. /* Parameters: void                                                           */
  218. /*                                                                            */
  219. /* Returns:    Version                                                        */
  220. /*                                                                            */
  221. /*----------------------------------------------------------------------------*/
  222.  
  223.  
  224. int FAR PASCAL _export iVersion ( void )
  225. {
  226.    return (VERSION);
  227. }
  228.  
  229.  
  230. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  231. /*                                                                         */
  232. /* vAddCon                                                                 */
  233. /*                                                                         */
  234. /* Appends a CON record to the linked structure.                           */
  235. /*                                                                         */
  236. /*                                                                         */
  237. /*                                                                         */
  238. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  239.  
  240. static void vAddCon ( CON_DATA  *ptCon )
  241. {
  242.    CON_DATA  *ptHlp;
  243.  
  244.    if ( ptFirstCon == NULL )
  245.       ptFirstCon=ptCon;
  246.    else
  247.    {
  248.       for ( ptHlp=ptFirstCon;
  249.             ptHlp->ptNextCon != NULL;
  250.             ptHlp = (CON_DATA *) ptHlp->ptNextCon
  251.           );
  252.  
  253.       ptHlp->ptNextCon = (void *) ptCon;
  254.    }
  255. }
  256.  
  257.  
  258. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  259. /*                                                                         */
  260. /* vDelCon                                                                 */
  261. /*                                                                         */
  262. /* Delets a CON record from the linked structure.                          */
  263. /*                                                                         */
  264. /*                                                                         */
  265. /*                                                                         */
  266. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  267.  
  268. static void vDelCon ( long hCon )
  269. {
  270.    CON_DATA   *ptHlp=NULL;
  271.    CON_DATA   *ptPrev=NULL;
  272.  
  273.    for ( ptHlp=ptFirstCon;
  274.          ptHlp!=NULL;
  275.          ptPrev=ptHlp, ptHlp=(CON_DATA *)ptHlp->ptNextCon )
  276.    {
  277.       if (ptHlp->hCon == hCon)
  278.       {
  279.         if (ptPrev)
  280.            ptPrev->ptNextCon=(void *)ptHlp->ptNextCon;
  281.         else
  282.            ptFirstCon=(void *)ptHlp->ptNextCon;
  283.  
  284.         free( (char *)ptHlp);
  285.         break;
  286.       }
  287.    }
  288. }
  289.  
  290.  
  291. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  292. /*                                                                         */
  293. /* vGetCon                                                                 */
  294. /*                                                                         */
  295. /* Gets the connection structure from a connection handle.                 */
  296. /*                                                                         */
  297. /*                                                                         */
  298. /*                                                                         */
  299. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  300.  
  301. static CON_DATA *ptGetCon ( long hCon )
  302. {
  303.    CON_DATA   *ptHlp=NULL;
  304.  
  305.    for ( ptHlp=ptFirstCon;
  306.          ptHlp!=NULL && ptHlp->hCon != hCon;
  307.          ptHlp=(CON_DATA *)ptHlp->ptNextCon );
  308.  
  309.    return ( ptHlp );
  310. }
  311.  
  312.  
  313.  
  314. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  315. /*                                                                         */
  316. /* vExitService                                                            */
  317. /*                                                                         */
  318. /* Ein Service wird con einem Connect beendet.  .                          */
  319. /*                                                                         */
  320. /*                                                                         */
  321. /*                                                                         */
  322. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  323.  
  324. static void vExitService ( long hCon, CON_DATA *ptCon )
  325. {
  326.    RXTX_SERVICE *ptData;
  327.  
  328.    ptData = malloc(sizeof(RXTX_SERVICE));
  329.    memset ( ptData, 0x00, sizeof(RXTX_SERVICE));
  330.  
  331.    ptData->hCon = hCon;
  332.    strcpy(ptData->sPacket,"Goodby from ECHO Service, ");
  333.    strcat(ptData->sPacket,ptCon->szCallSign);
  334.    strcat(ptData->sPacket," \n\r");
  335.    ptData->sPacLen=strlen(ptData->sPacket);
  336.    (*lpfnSnd2Con) (ptData);
  337.    (*lpfnControl) (ptCon->hCon, SRV_EXIT, NULL );
  338.    free ( (char *)ptData);
  339. }
  340.                          
  341.                          
  342.                          
  343. static void vService ( long hCon, CON_DATA *ptCon )
  344. {
  345.    RXTX_SERVICE *ptData;
  346.  
  347.  
  348.    if ( strstr(ptCon->szInpBuffer,"//EXIT") )
  349.    {
  350.       vExitService ( hCon, ptCon );
  351.       return;
  352.    }
  353.  
  354.    ptData = malloc(sizeof(RXTX_SERVICE));
  355.    memset ( ptData, 0x00, sizeof(RXTX_SERVICE));
  356.  
  357.    ptData->hCon = hCon;
  358.    strcpy(ptData->sPacket,"<");
  359.    strcat(ptData->sPacket,ptCon->szCallSign);
  360.    strcat(ptData->sPacket,">");
  361.    strcat(ptData->sPacket,ptCon->szInpBuffer);
  362.    strcat(ptData->sPacket,"\n\r");
  363.    
  364.    ptData->sPacLen=strlen(ptData->sPacket);
  365.    (*lpfnSnd2Con) (ptData);
  366.    free ( (char *)ptData);
  367. }
  368.