Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1994 - 1999
  6. //
  7. // File: sccomn.c
  8. //
  9. //--------------------------------------------------------------------------
  10. /////////////////////////////////////////////////////////////////////////
  11. //
  12. // Filename: sccomn.c
  13. //
  14. // Description: This file contains common routines for socket I/O
  15. // routines for use with IPC raw network performance
  16. // tests.
  17. // This module is written using win32 API calls.
  18. //
  19. // Authors: Scott Holden (Translator from NT API to win32 API)
  20. // Mahesh Keni (Mahesh wrote this application using mostly
  21. // NT native API calls)
  22. //
  23. /////////////////////////////////////////////////////////////////////////
  24. #include "rawcom.h"
  25. #include "sccomn.h"
  26. /************************************************************************/
  27. // Socket specific functions
  28. /************************************************************************/
  29. INTEGER
  30. Socket_Listen(
  31. IN USHORT CliIndx)
  32. {
  33. INTEGER RetCode;
  34. // now post listen on this handle
  35. RetCode = listen(Clients[CliIndx].c_Sock.c_Listenid, 1);
  36. if (RetCode == SOCKET_ERROR) {
  37. //DbgPrint("Error: Listen:%ld\n",WSAGetLastError());
  38. }
  39. //MyDbgPrint("Sock: Listen success for %d\n", CliIndx);
  40. return(RetCode);
  41. }
  42. /************************************************************************/
  43. INTEGER
  44. Socket_Accept(
  45. IN USHORT CliIndx)
  46. {
  47. INTEGER RetCode;
  48. SOCKADDR saddr; // socket address
  49. INTEGER saddrlen;
  50. saddrlen = sizeof(saddr);
  51. ClearSocket(&saddr); // cleanup the structure
  52. // accept the connection
  53. RetCode = (INTEGER)accept(Clients[CliIndx].c_Sock.c_Listenid, &saddr, &saddrlen);
  54. if (RetCode == SOCKET_ERROR) {
  55. //DbgPrint("Error: Accept:%ld\n", WSAGetLastError());
  56. }
  57. else {
  58. Clients[CliIndx].c_Sock.c_Sockid = RetCode; // valid socket id
  59. }
  60. return(RetCode);
  61. }
  62. /************************************************************************/
  63. INTEGER
  64. Socket_Recv(
  65. IN USHORT CliIndx,
  66. IN OUT PVOID PReadBuf,
  67. IN OUT PULONG rpdatalen)
  68. {
  69. INTEGER RetCode;
  70. ULONG Buflen;
  71. PCHAR Bufp;
  72. Bufp = PReadBuf;
  73. Buflen = *rpdatalen; // use this for getting request packet length
  74. *rpdatalen = 0; // total data received
  75. // we have make sure that the data received is same as the data
  76. // requested
  77. while (Buflen) { // till we receive all the data
  78. RetCode = recv(Clients[CliIndx].c_Sock.c_Sockid, Bufp, Buflen, 0);
  79. if (RetCode == SOCKET_ERROR) {
  80. //DbgPrint("Error: Receive:%ld\n",WSAGetLastError());
  81. break;
  82. }
  83. else {
  84. if (RetCode){
  85. *rpdatalen += (ULONG)RetCode; // bytes received
  86. Buflen -= (ULONG)RetCode; // bytes yet to receive
  87. Bufp += (USHORT)RetCode; // Move the buffer pointer
  88. }
  89. else { break; }
  90. }
  91. } // check to see if it's 0
  92. // DbgPrint("Recvd %ld bytes\n", *rpdatalen);
  93. return(RetCode);
  94. }
  95. /************************************************************************/
  96. INTEGER
  97. Socket_Send(
  98. IN USHORT CliIndx,
  99. IN OUT PVOID PWriteBuf,
  100. IN OUT PULONG spdatalen)
  101. {
  102. INTEGER RetCode;
  103. ULONG Buflen;
  104. Buflen = *spdatalen; // total data to be sent
  105. *spdatalen = 0;
  106. // Could use write also
  107. RetCode = send(Clients[CliIndx].c_Sock.c_Sockid, PWriteBuf, Buflen, 0);
  108. if (RetCode == SOCKET_ERROR) {
  109. //DbgPrint("Error: Send:%ld\n",WSAGetLastError());
  110. }
  111. else {
  112. *spdatalen = (ULONG) RetCode;
  113. // DbgPrint("Sent %ld bytes\n", *spdatalen);
  114. } // check to see if it's 0
  115. return(RetCode);
  116. }
  117. /************************************************************************/
  118. INTEGER
  119. Socket_Close(
  120. IN USHORT CliIndx)
  121. {
  122. INTEGER RetCode;
  123. RetCode = closesocket(Clients[CliIndx].c_Sock.c_Sockid);
  124. if (RetCode == SOCKET_ERROR) {
  125. //DbgPrint("Error: Close Socket Id \n");
  126. return(RetCode);
  127. }
  128. return(RetCode);
  129. }
  130. /************************************************************************/