home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / winsock / twnsck10 / twinsock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  7.1 KB  |  338 lines

  1. /*
  2.  *  TwinSock - "Troy's Windows Sockets"
  3.  *
  4.  *  Copyright (C) 1994  Troy Rollo <troy@cbme.unsw.EDU.AU>
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <winsock.h>
  22. #include <stdlib.h>
  23. #include "twinsock.h"
  24. #include "tx.h"
  25.  
  26. extern    RegisterManager(HWND hwnd);
  27.  
  28. #define    READ_MAX    1024
  29. #define    TIMER_ID_SEND        1
  30. #define    TIMER_ID_RECEIVE    2
  31. #define    TIMER_ID_FLUSH        3
  32.  
  33. static    int    idComm;
  34. static    HWND    hwnd;
  35. static    BOOL    bFlushing = FALSE;
  36.  
  37. extern    void PacketReceiveData(void *pvData, int iLen);
  38.  
  39. void    SetTransmitTimeout(void)
  40. {
  41.     KillTimer(hwnd, TIMER_ID_SEND);
  42.     SetTimer(hwnd, TIMER_ID_SEND, 3000, 0);
  43. }
  44.  
  45. void    KillTransmitTimeout(void)
  46. {
  47.     KillTimer(hwnd, TIMER_ID_SEND);
  48. }
  49.  
  50. void    SetReceiveTimeout(void)
  51. {
  52.     KillTimer(hwnd, TIMER_ID_RECEIVE);
  53.     SetTimer(hwnd, TIMER_ID_RECEIVE, 1500, 0);
  54. }
  55.  
  56. void    KillReceiveTimeout(void)
  57. {
  58.     KillTimer(hwnd, TIMER_ID_RECEIVE);
  59. }
  60.  
  61. void    FlushInput(void)
  62. {
  63.     KillTimer(hwnd, TIMER_ID_FLUSH);
  64.     bFlushing = TRUE;
  65.     SetTimer(hwnd, TIMER_ID_FLUSH, 1500, 0);
  66. }
  67.  
  68. static    void    DoReading(void)
  69. {
  70.     static    char    achBuffer[READ_MAX];
  71.     int    nRead;
  72.     COMSTAT    cs;
  73.  
  74.     do
  75.     {
  76.         nRead = ReadComm(idComm, achBuffer, READ_MAX);
  77.         if (nRead <= 0)
  78.         {
  79.             GetCommError(idComm, &cs);
  80.             nRead = -nRead;
  81.         }
  82.         if (nRead)
  83.         {
  84.             if (bFlushing)
  85.                 FlushInput();
  86.             else
  87.                 PacketReceiveData(achBuffer, nRead);
  88.         }
  89.     } while (nRead);
  90. }
  91.  
  92. int    SendData(void *pvData, int iDataLen)
  93. {
  94.     int    nWritten;
  95.     COMSTAT    cs;
  96.     int    iLen;
  97.  
  98.     if (bFlushing)
  99.         return iDataLen; /* Lie */
  100.     iLen = iDataLen;
  101.     do
  102.     {
  103.         nWritten = WriteComm(idComm, pvData, iLen);
  104.         if (nWritten < 0)
  105.         {
  106.             GetCommError(idComm, &cs);
  107.             nWritten = -nWritten;
  108.         }
  109.         iLen -= nWritten;
  110.         pvData = (char *) pvData + nWritten;
  111.     } while (iLen);
  112.     return iDataLen;
  113. }
  114.  
  115. LRESULT    CALLBACK _export
  116. WindowProc(    HWND    hWnd,
  117.         UINT    wMsg,
  118.         WPARAM    wParam,
  119.         LPARAM    lParam)
  120. {
  121.  
  122.     switch(wMsg)
  123.     {
  124.     case WM_SYSCOMMAND:
  125.         if (wParam == SC_MAXIMIZE ||
  126.             wParam == SC_RESTORE)
  127.             return 0;
  128.         break;
  129.  
  130.     case WM_COMMNOTIFY:
  131.         switch(LOWORD(lParam))
  132.         {
  133.         case CN_RECEIVE:
  134.             DoReading();
  135.             break;
  136.         }
  137.         break;
  138.  
  139.     case WM_TIMER:
  140.         switch(wParam)
  141.         {
  142.         case TIMER_ID_SEND:
  143.             TimeoutReceived(TIMER_ID_SEND);
  144.             break;
  145.  
  146.         case TIMER_ID_RECEIVE:
  147.             KillTimer(hWnd, TIMER_ID_RECEIVE);
  148.             PacketReceiveData(0, 0);
  149.             break;
  150.  
  151.         case TIMER_ID_FLUSH:
  152.             KillTimer(hWnd, TIMER_ID_FLUSH);
  153.             bFlushing = FALSE;
  154.             break;
  155.         }
  156.         break;
  157.  
  158.     case WM_USER:
  159.         PacketTransmitData((void *) lParam, wParam, 0);
  160.         DoReading();
  161.         break;
  162.  
  163.     case WM_CLOSE:
  164.         PostQuitMessage(0);
  165.         break;
  166.     }
  167.     return DefWindowProc(hWnd, wMsg, wParam, lParam);
  168. }
  169.  
  170. void
  171. DataReceived(void *pvData, int iLen)
  172. {
  173.     static    struct tx_request *ptxr = 0;
  174.     static    struct tx_request txrHeader;
  175.     static    int    nBytes = 0;
  176.     short    nPktLen;
  177.     enum Functions ft;
  178.     int    nCopy;
  179.  
  180.     while (iLen)
  181.     {
  182.         if (nBytes < 10)
  183.         {
  184.             nCopy = 10 - nBytes;
  185.             if (nCopy > iLen)
  186.                 nCopy = iLen;
  187.             memcpy((char *) &txrHeader + nBytes, pvData, nCopy);
  188.             nBytes += nCopy;
  189.             pvData = (char *) pvData + nCopy;
  190.             iLen -= nCopy;
  191.             if (nBytes == 10)
  192.             {
  193.                 nPktLen = ntohs(txrHeader.nLen);
  194.                 ptxr = (struct tx_request *) malloc(sizeof(struct tx_request) + nPktLen - 1);
  195.                 memcpy(ptxr, &txrHeader, 10);
  196.             }
  197.         }
  198.         if (nBytes >= 10)
  199.         {
  200.             nPktLen = ntohs(txrHeader.nLen);
  201.             ft = (enum Functions) ntohs(txrHeader.iType);
  202.             nCopy = nPktLen - nBytes;
  203.             if (nCopy > iLen)
  204.                 nCopy = iLen;
  205.             if (nCopy)
  206.             {
  207.                 memcpy((char *) ptxr + nBytes, pvData, nCopy);
  208.                 nBytes += nCopy;
  209.                 pvData = (char *) pvData + nCopy;
  210.                 iLen -= nCopy;
  211.             }
  212.             if (nBytes == nPktLen)
  213.             {
  214.                 if (ft == FN_Init)
  215.                     SetInitialised();
  216.                 else
  217.                     ResponseReceived(ptxr);
  218.                 free(ptxr);
  219.                 ptxr = 0;
  220.                 nBytes = 0;
  221.             }
  222.         }
  223.     }
  224. }
  225.  
  226. static    UINT
  227. GetConfigInt(char const *pchItem, UINT nDefault)
  228. {
  229.     return GetPrivateProfileInt("Config", pchItem, nDefault, "TWINSOCK.INI");
  230. }
  231.  
  232. static void
  233. SendInitRequest(void)
  234. {
  235.     struct    tx_request txr;
  236.  
  237.     txr.iType = htons(FN_Init);
  238.     txr.nArgs = 0;
  239.     txr.nLen = htons(10);
  240.     txr.id = -1;
  241.     txr.nError = 0;
  242.     PacketTransmitData(&txr, 10, 0);
  243. }
  244.  
  245. void
  246. Shutdown(void)
  247. {
  248.     PostQuitMessage(0);
  249. }
  250.  
  251. #pragma argsused
  252. int    far    pascal
  253. WinMain(HINSTANCE    hInstance,
  254.     HINSTANCE    hPrec,
  255.     LPSTR        lpCmdLine,
  256.     int        nShow)
  257. {
  258.     WNDCLASS wc;
  259.     MSG    msg;
  260.     DCB    dcb;
  261.     char    achProfileEntry[256];
  262.  
  263.     wc.style = 0;
  264.     wc.lpfnWndProc = WindowProc;
  265.     wc.cbClsExtra = 0;
  266.     wc.cbWndExtra = 0;
  267.     wc.hInstance = hInstance;
  268.     wc.hIcon = LoadIcon(hInstance, "TSICON");
  269.     wc.hCursor = 0;
  270.     wc.hbrBackground = 0;
  271.     wc.lpszMenuName = 0;
  272.     wc.lpszClassName = "TwinSock Communications";
  273.     RegisterClass(&wc);
  274.     hwnd = CreateWindow(    "TwinSock Communications",
  275.                 "TwinSock Communications",
  276.                 WS_OVERLAPPEDWINDOW |
  277.                  WS_MINIMIZE |
  278.                  WS_VISIBLE,
  279.                 CW_USEDEFAULT,
  280.                 0,
  281.                 CW_USEDEFAULT,
  282.                 0,
  283.                 0,
  284.                 0,
  285.                 hInstance,
  286.                 0);
  287.     ShowWindow(hwnd, SW_SHOW);
  288.     RegisterManager(hwnd);
  289.  
  290.     GetPrivateProfileString("Config", "Port", "COM1", achProfileEntry, 256, "TWINSOCK.INI");
  291.     idComm = OpenComm(achProfileEntry, 1024, 1024);
  292.     if (idComm < 0)
  293.         exit(1);
  294.     dcb.Id = idComm;
  295.     dcb.BaudRate = GetConfigInt("Speed", 19200);
  296.     dcb.ByteSize = GetConfigInt("Databits", 8);
  297.     dcb.Parity = GetConfigInt("Parity", NOPARITY);
  298.     dcb.StopBits = GetConfigInt("StopBits", ONESTOPBIT);
  299.     dcb.RlsTimeout = GetConfigInt("RlsTimeout", 0);
  300.     dcb.CtsTimeout = GetConfigInt("CtsTimeout", 0);
  301.     dcb.DsrTimeout = GetConfigInt("DsrTimeout", 0);
  302.     dcb.fBinary = TRUE;
  303.     dcb.fRtsDisable = GetConfigInt("fRtsDisable", TRUE);
  304.     dcb.fParity = GetConfigInt("fParity", FALSE);
  305.     dcb.fOutxCtsFlow = GetConfigInt("OutxCtsFlow", FALSE);
  306.     dcb.fOutxDsrFlow = GetConfigInt("OutxDsrFlow", FALSE);
  307.     dcb.fDummy = 0;
  308.     dcb.fDtrDisable = GetConfigInt("fDtrDisable", TRUE);
  309.     dcb.fOutX = GetConfigInt("fOutX", TRUE);
  310.     dcb.fInX = GetConfigInt("fInX", FALSE);
  311.     dcb.fPeChar = 0;
  312.     dcb.fNull = 0;
  313.     dcb.fChEvt = 0;
  314.     dcb.fDtrflow = GetConfigInt("fDtrFlow", FALSE);
  315.     dcb.fRtsflow = GetConfigInt("fRtsFlow", FALSE);
  316.     dcb.fDummy2 = 0;
  317.     dcb.XonChar = '\021';
  318.     dcb.XoffChar = '\023';
  319.     dcb.XonLim = 100;
  320.     dcb.XoffLim = 900;
  321.     dcb.PeChar = 0;
  322.     dcb.EofChar = 0;
  323.     dcb.EvtChar = 0;
  324.     dcb.TxDelay = 0;
  325.  
  326.     SetCommState(&dcb);
  327.     EnableCommNotification(idComm, hwnd, 1, 0);
  328.     SendInitRequest();
  329.  
  330.      while (GetMessage(&msg, 0, 0, 0))
  331.     {
  332.         {
  333.             TranslateMessage(&msg);
  334.             DispatchMessage(&msg);
  335.         }
  336.     }
  337. }
  338.