home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / NPIPE.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  13KB  |  482 lines

  1.  
  2. // LoraBBS Version 2.99 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include "_ldefs.h"
  20. #include "combase.h"
  21.  
  22. TPipe::TPipe (void)
  23. {
  24.    EndRun = FALSE;
  25.    TxBytes = 0;
  26.    CtlConnect = PipeConnect = FALSE;
  27. }
  28.  
  29. TPipe::~TPipe (void)
  30. {
  31.    Name[0] = City[0] = Level[0] = '\0';
  32.    TimeLeft = Time = 0L;
  33.  
  34. #if defined(__OS2__)
  35.    if (hFileCtl != NULL) {
  36.       DosDisConnectNPipe (hFileCtl);
  37.       DosClose (hFileCtl);
  38.    }
  39.    if (hFile != NULL) {
  40.       DosDisConnectNPipe (hFile);
  41.       DosClose (hFile);
  42.    }
  43. #elif defined(__NT__)
  44.    if (hFileCtl != INVALID_HANDLE_VALUE)
  45.       CloseHandle (hFileCtl);
  46.    if (hFile != INVALID_HANDLE_VALUE)
  47.       CloseHandle (hFile);
  48. #endif
  49. }
  50.  
  51. VOID TPipe::BufferByte (UCHAR byte)
  52. {
  53.    TxBuffer[TxBytes++] = byte;
  54.    if (TxBytes >= TSIZE)
  55.       UnbufferBytes ();
  56. }
  57.  
  58. VOID TPipe::BufferBytes (UCHAR *bytes, USHORT len)
  59. {
  60.    while (len > 0 && EndRun == FALSE) {
  61.       TxBuffer[TxBytes++] = *bytes++;
  62.       if (TxBytes >= TSIZE)
  63.          UnbufferBytes ();
  64.       len--;
  65.    }
  66. }
  67.  
  68. USHORT TPipe::BytesReady (VOID)
  69. {
  70.    USHORT RetVal = FALSE;
  71. #if defined(__OS2__)
  72.    CHAR c, *p;
  73.    ULONG data, Temp, pipeState;
  74.    struct _AVAILDATA Available;
  75.  
  76.    if (hFile != NULLHANDLE) {
  77.       data = 0L;
  78.       DosPeekNPipe (hFile, (PVOID)&Temp, sizeof (Temp), &data, &Available, &pipeState);
  79.       if (data > 0L)
  80.          RetVal = TRUE;
  81.       EndRun = FALSE;
  82.       if (pipeState == NP_STATE_CLOSING)
  83.          EndRun = TRUE;
  84.    }
  85.  
  86.    if (hFileCtl != NULLHANDLE) {
  87.       data = 0L;
  88.       DosPeekNPipe (hFileCtl, (PVOID)&Temp, sizeof (Temp), &data, &Available, &pipeState);
  89.       if (data >= 2L) {
  90.          c = 0;
  91.          DosRead (hFileCtl, (PVOID)&c, 1L, &data);
  92.          switch (c) {
  93.             case 1:
  94.                p = Name;
  95.                do {
  96.                   c = '\0';
  97.                   DosRead (hFileCtl, (PVOID)&c, 1L, &data);
  98.                   *p++ = c;
  99.                } while (c != '\0');
  100.                break;
  101.             case 2:
  102.                p = City;
  103.                do {
  104.                   c = '\0';
  105.                   DosRead (hFileCtl, (PVOID)&c, 1L, &data);
  106.                   *p++ = c;
  107.                } while (c != '\0');
  108.                break;
  109.             case 3:
  110.                p = Level;
  111.                do {
  112.                   c = '\0';
  113.                   DosRead (hFileCtl, (PVOID)&c, 1L, &data);
  114.                   *p++ = c;
  115.                } while (c != '\0');
  116.                break;
  117.             case 4:
  118.                DosRead (hFileCtl, (PVOID)&TimeLeft, sizeof (ULONG), &data);
  119.                break;
  120.             case 5:
  121.                DosRead (hFileCtl, (PVOID)&Time, sizeof (ULONG), &data);
  122.                break;
  123.          }
  124.       }
  125.    }
  126.  
  127.    if (RetVal == FALSE)
  128.       DosSleep (1L);
  129.  
  130. #elif defined(__NT__)
  131.    ULONG Available;
  132.  
  133.    if (hFile != INVALID_HANDLE_VALUE) {
  134.       EndRun = FALSE;
  135.       if (PeekNamedPipe (hFile, NULL, 0L, NULL, &Available, NULL) == FALSE)
  136.          EndRun = TRUE;
  137.       if (Available > 0)
  138.          RetVal = TRUE;
  139.    }
  140.  
  141.    if (RetVal == FALSE)
  142.       Sleep (1L);
  143. #endif
  144.  
  145.    return (RetVal);
  146. }
  147.  
  148. USHORT TPipe::Carrier (VOID)
  149. {
  150.    USHORT RetVal = TRUE;
  151. #if defined(__OS2__)
  152.    ULONG data, Temp, pipeState;
  153.    struct _AVAILDATA Available;
  154.  
  155.    if (hFileCtl != NULLHANDLE) {
  156.       data = 0L;
  157.       DosPeekNPipe (hFileCtl, (PVOID)&Temp, sizeof (Temp), &data, &Available, &pipeState);
  158.       if (pipeState == NP_STATE_CLOSING)
  159.          RetVal = FALSE;
  160.    }
  161.    if (hFile != NULLHANDLE) {
  162.       data = 0L;
  163.       DosPeekNPipe (hFile, (PVOID)&Temp, sizeof (Temp), &data, &Available, &pipeState);
  164.       if (pipeState == NP_STATE_CLOSING)
  165.          RetVal = FALSE;
  166.    }
  167.  
  168. #elif defined(__NT__)
  169.    ULONG Available;
  170.  
  171.    if (hFileCtl != INVALID_HANDLE_VALUE) {
  172.       if (PeekNamedPipe (hFileCtl, NULL, 0L, NULL, &Available, NULL) == FALSE)
  173.          RetVal = FALSE;
  174.    }
  175.    if (hFile != INVALID_HANDLE_VALUE) {
  176.       if (PeekNamedPipe (hFile, NULL, 0L, NULL, &Available, NULL) == FALSE)
  177.          RetVal = FALSE;
  178.    }
  179. #endif
  180.  
  181.    return (RetVal);
  182. }
  183.  
  184. VOID TPipe::ClearInbound (VOID)
  185. {
  186. }
  187.  
  188. VOID TPipe::ClearOutbound (VOID)
  189. {
  190.    TxBytes = 0;
  191. }
  192.  
  193. USHORT TPipe::Initialize (PSZ pszPipeName, PSZ pszCtlName, USHORT usInstances)
  194. {
  195.    USHORT RetVal = FALSE;
  196.    CHAR TempFile[128];
  197.  
  198.    CtlConnect = PipeConnect = FALSE;
  199.  
  200. #if defined(__OS2__)
  201.    hFileCtl = NULLHANDLE;
  202.    if (!strncmp (pszCtlName, "\\\\.", 3)) {
  203.       strcpy (TempFile, &pszCtlName[3]);
  204.       pszCtlName = TempFile;
  205.    }
  206.    if (DosCreateNPipe (pszCtlName, &hFileCtl, NP_ACCESS_DUPLEX, NP_NOWAIT|usInstances, TSIZE, RSIZE, 1000) != 0) {
  207.       hFileCtl = NULLHANDLE;
  208.       CtlConnect = TRUE;
  209.    }
  210.  
  211.    hFile = NULLHANDLE;
  212.    if (!strncmp (pszPipeName, "\\\\.", 3)) {
  213.       strcpy (TempFile, &pszPipeName[3]);
  214.       pszPipeName = TempFile;
  215.    }
  216.    if (DosCreateNPipe (pszPipeName, &hFile, NP_ACCESS_DUPLEX, NP_NOWAIT|usInstances, TSIZE, RSIZE, 1000) == 0)
  217.       RetVal = TRUE;
  218.  
  219. #elif defined(__NT__)
  220.    hFileCtl = INVALID_HANDLE_VALUE;
  221.    if (strncmp (pszCtlName, "\\\\.", 3)) {
  222.       sprintf (TempFile, "\\\\.%s", pszCtlName);
  223.       pszCtlName = TempFile;
  224.    }
  225.    hFileCtl = CreateNamedPipe (pszCtlName, PIPE_ACCESS_DUPLEX|FILE_FLAG_WRITE_THROUGH, PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_NOWAIT, usInstances, TSIZE * 2, RSIZE, 1000, NULL);
  226.  
  227.    hFile = INVALID_HANDLE_VALUE;
  228.    if (strncmp (pszPipeName, "\\\\.", 3)) {
  229.       sprintf (TempFile, "\\\\.%s", pszPipeName);
  230.       pszPipeName = TempFile;
  231.    }
  232.    if ((hFile = CreateNamedPipe (pszPipeName, PIPE_ACCESS_DUPLEX|FILE_FLAG_WRITE_THROUGH, PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_NOWAIT, usInstances, TSIZE * 2, RSIZE, 1000, NULL)) != INVALID_HANDLE_VALUE)
  233.       RetVal = TRUE;
  234. #endif
  235.  
  236.    return (RetVal);
  237. }
  238.  
  239. USHORT TPipe::ConnectServer (PSZ pszPipeName, PSZ pszCtlName)
  240. {
  241.    USHORT RetVal = FALSE;
  242.    CHAR TempFile[128];
  243. #if defined(__OS2__)
  244.    ULONG Action;
  245. #endif
  246.  
  247. #if defined(__OS2__)
  248.    hFileCtl = NULLHANDLE;
  249.    if (!strncmp (pszCtlName, "\\\\.", 3)) {
  250.       strcpy (TempFile, &pszCtlName[3]);
  251.       pszCtlName = TempFile;
  252.    }
  253.    if (DosOpen (pszCtlName, &hFileCtl, &Action, 0, FILE_NORMAL, FILE_OPEN, OPEN_ACCESS_READWRITE|OPEN_SHARE_DENYNONE, NULL) != 0)
  254.       hFileCtl = NULLHANDLE;
  255.  
  256.    hFile = NULLHANDLE;
  257.    if (!strncmp (pszPipeName, "\\\\.", 3)) {
  258.       strcpy (TempFile, &pszPipeName[3]);
  259.       pszPipeName = TempFile;
  260.    }
  261.    if (DosOpen (pszPipeName, &hFile, &Action, 0, FILE_NORMAL, FILE_OPEN, OPEN_ACCESS_READWRITE|OPEN_SHARE_DENYNONE, NULL) == 0)
  262.       RetVal = TRUE;
  263. #elif defined(__NT__)
  264.    hFileCtl = INVALID_HANDLE_VALUE;
  265.    if (strncmp (pszCtlName, "\\\\.", 3)) {
  266.       sprintf (TempFile, "\\\\.%s", pszCtlName);
  267.       pszCtlName = TempFile;
  268.    }
  269.    hFileCtl = CreateFile (pszCtlName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH, NULL);
  270.  
  271.    hFile = INVALID_HANDLE_VALUE;
  272.    if (strncmp (pszPipeName, "\\\\.", 3)) {
  273.       sprintf (TempFile, "\\\\.%s", pszPipeName);
  274.       pszPipeName = TempFile;
  275.    }
  276.    if ((hFile = CreateFile (pszPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH, NULL)) != INVALID_HANDLE_VALUE)
  277.       RetVal = TRUE;
  278. #endif
  279.  
  280.    return (RetVal);
  281. }
  282.  
  283. UCHAR TPipe::ReadByte (VOID)
  284. {
  285.    UCHAR c = 0;
  286.    ULONG bytesRead;
  287.  
  288. #if defined(__OS2__)
  289.    if (hFile != NULLHANDLE)
  290.       DosRead (hFile, (PVOID)&c, 1L, &bytesRead);
  291. #elif defined(__NT__)
  292.    if (hFile != INVALID_HANDLE_VALUE)
  293.       ReadFile (hFile, (PVOID)&c, 1L, &bytesRead, NULL);
  294. #endif
  295.  
  296.    return ((UCHAR)c);
  297. }
  298.  
  299. USHORT TPipe::ReadBytes (UCHAR *bytes, USHORT len)
  300. {
  301.    ULONG bytesRead;
  302.  
  303. #if defined(__OS2__)
  304.    if (hFile != NULLHANDLE)
  305.       DosRead (hFile, (PVOID)bytes, len, &bytesRead);
  306. #elif defined(__NT__)
  307.    if (hFile != INVALID_HANDLE_VALUE)
  308.       ReadFile (hFile, (PVOID)bytes, len, &bytesRead, NULL);
  309. #endif
  310.  
  311.    return ((USHORT)bytesRead);
  312. }
  313.  
  314. VOID TPipe::SendByte (UCHAR byte)
  315. {
  316.    ULONG written;
  317.  
  318. #if defined(__OS2__)
  319.    if (hFile != NULLHANDLE && EndRun == FALSE) {
  320.       DosSetNPHState (hFile, NP_WAIT|NP_READMODE_BYTE);
  321.       DosWrite (hFile, (PVOID)&byte, (long)1, &written);
  322.       DosSetNPHState (hFile, NP_NOWAIT|NP_READMODE_BYTE);
  323.    }
  324. #elif defined(__NT__)
  325.    if (hFile != INVALID_HANDLE_VALUE && EndRun == FALSE)
  326.       do {
  327.          WriteFile (hFile, (LPCVOID)&byte, 1L, &written, NULL);
  328.       } while (written != 1L);
  329. #endif
  330. }
  331.  
  332. VOID TPipe::SendBytes (UCHAR *bytes, USHORT len)
  333. {
  334.    ULONG written;
  335.  
  336. #if defined(__OS2__)
  337.    if (hFile != NULLHANDLE) {
  338.       DosSetNPHState (hFile, NP_WAIT|NP_READMODE_BYTE);
  339.       DosWrite (hFile, (PVOID)bytes, (long)len, &written);
  340.       DosSetNPHState (hFile, NP_NOWAIT|NP_READMODE_BYTE);
  341.    }
  342. #elif defined(__NT__)
  343.    if (hFile != INVALID_HANDLE_VALUE && EndRun == FALSE)
  344.       do {
  345.          WriteFile (hFile, (LPCVOID)bytes, (long)len, &written, NULL);
  346.          bytes += written;
  347.          len -= written;
  348.       } while (len > 0 && EndRun == FALSE);
  349. #endif
  350. }
  351.  
  352. VOID TPipe::UnbufferBytes (VOID)
  353. {
  354.    ULONG Written;
  355. #if defined(__NT__)
  356.    UCHAR *p;
  357. #endif
  358.  
  359. #if defined(__OS2__)
  360.    if (hFile != NULLHANDLE && TxBytes > 0 && EndRun == FALSE) {
  361.       DosSetNPHState (hFile, NP_WAIT|NP_READMODE_BYTE);
  362.       DosWrite (hFile, (PVOID)TxBuffer, (long)TxBytes, &Written);
  363.       TxBytes = 0;
  364.       DosSetNPHState (hFile, NP_NOWAIT|NP_READMODE_BYTE);
  365.    }
  366. #elif defined(__NT__)
  367.    if (hFile != INVALID_HANDLE_VALUE && TxBytes > 0 && EndRun == FALSE) {
  368.       p = TxBuffer;
  369.       do {
  370.          WriteFile (hFile, (LPCVOID)p, (long)TxBytes, &Written, NULL);
  371.          p += Written;
  372.          if (Written < TxBytes)
  373.             Sleep (10L);
  374.          TxBytes -= (USHORT)Written;
  375.       } while (TxBytes > 0 && EndRun == FALSE);
  376.    }
  377. #endif
  378. }
  379.  
  380. USHORT TPipe::WaitClient (VOID)
  381. {
  382.    USHORT RetVal = FALSE;
  383.  
  384. #if defined(__OS2__)
  385.    if (hFileCtl != NULLHANDLE && CtlConnect == FALSE) {
  386.       if (DosConnectNPipe (hFileCtl) == 0)
  387.          CtlConnect = TRUE;
  388.    }
  389.    if (hFile != NULLHANDLE && PipeConnect == FALSE) {
  390.       if (DosConnectNPipe (hFile) == 0)
  391.          PipeConnect = TRUE;
  392.    }
  393.    if (CtlConnect == TRUE && PipeConnect == TRUE)
  394.       RetVal = TRUE;
  395. #elif defined(__NT__)
  396.    if (hFileCtl != INVALID_HANDLE_VALUE)
  397.       ConnectNamedPipe (hFileCtl, NULL);
  398.    ConnectNamedPipe (hFile, NULL);
  399.    if (GetLastError () == ERROR_PIPE_CONNECTED)
  400.       RetVal = TRUE;
  401. #endif
  402.  
  403.    return (RetVal);
  404. }
  405.  
  406. VOID TPipe::SetName (PSZ name)
  407. {
  408.    ULONG written;
  409.  
  410. #if defined(__OS2__)
  411.    if (hFileCtl != NULLHANDLE) {
  412.       DosSetNPHState (hFileCtl, NP_WAIT|NP_READMODE_BYTE);
  413.       DosWrite (hFileCtl, (PVOID)"\x01", 1L, &written);
  414.       DosWrite (hFileCtl, (PVOID)name, (long)(strlen (name) + 1), &written);
  415.       DosSetNPHState (hFileCtl, NP_NOWAIT|NP_READMODE_BYTE);
  416.    }
  417. #elif defined(__NT__)
  418. #endif
  419. }
  420.  
  421. VOID TPipe::SetCity (PSZ name)
  422. {
  423.    ULONG written;
  424.  
  425. #if defined(__OS2__)
  426.    if (hFileCtl != NULLHANDLE) {
  427.       DosSetNPHState (hFileCtl, NP_WAIT|NP_READMODE_BYTE);
  428.       DosWrite (hFileCtl, (PVOID)"\x02", 1L, &written);
  429.       DosWrite (hFileCtl, (PVOID)name, (long)(strlen (name) + 1), &written);
  430.       DosSetNPHState (hFileCtl, NP_NOWAIT|NP_READMODE_BYTE);
  431.    }
  432. #elif defined(__NT__)
  433. #endif
  434. }
  435.  
  436. VOID TPipe::SetLevel (PSZ level)
  437. {
  438.    ULONG written;
  439.  
  440. #if defined(__OS2__)
  441.    if (hFileCtl != NULLHANDLE) {
  442.       DosSetNPHState (hFileCtl, NP_WAIT|NP_READMODE_BYTE);
  443.       DosWrite (hFileCtl, (PVOID)"\x03", 1L, &written);
  444.       DosWrite (hFileCtl, (PVOID)level, (long)(strlen (level) + 1), &written);
  445.       DosSetNPHState (hFileCtl, NP_NOWAIT|NP_READMODE_BYTE);
  446.    }
  447. #elif defined(__NT__)
  448. #endif
  449. }
  450.  
  451. VOID TPipe::SetTimeLeft (ULONG seconds)
  452. {
  453.    ULONG written;
  454.  
  455. #if defined(__OS2__)
  456.    if (hFileCtl != NULLHANDLE) {
  457.       DosSetNPHState (hFileCtl, NP_WAIT|NP_READMODE_BYTE);
  458.       DosWrite (hFileCtl, (PVOID)"\x04", 1L, &written);
  459.       DosWrite (hFileCtl, (PVOID)&seconds, sizeof (ULONG), &written);
  460.       DosSetNPHState (hFileCtl, NP_NOWAIT|NP_READMODE_BYTE);
  461.    }
  462. #elif defined(__NT__)
  463. #endif
  464. }
  465.  
  466. VOID TPipe::SetTime (ULONG seconds)
  467. {
  468.    ULONG written;
  469.  
  470. #if defined(__OS2__)
  471.    if (hFileCtl != NULLHANDLE) {
  472.       DosSetNPHState (hFileCtl, NP_WAIT|NP_READMODE_BYTE);
  473.       DosWrite (hFileCtl, (PVOID)"\x05", 1L, &written);
  474.       DosWrite (hFileCtl, (PVOID)&seconds, sizeof (ULONG), &written);
  475.       DosSetNPHState (hFileCtl, NP_NOWAIT|NP_READMODE_BYTE);
  476.    }
  477. #elif defined(__NT__)
  478. #endif
  479. }
  480.  
  481.  
  482.