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.

networking.cpp 43KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. // networking.cpp
  2. // Copyright (C) 2006-2009 MicroNeil Research Corporation.
  3. //
  4. // This program is part of the MicroNeil Research Open Library Project. For
  5. // more information go to http://www.microneil.com/OpenLibrary/index.html
  6. //
  7. // This program is free software; you can redistribute it and/or modify it
  8. // under the terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 2 of the License, or (at your
  10. // option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful, but WITHOUT
  13. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. // more details.
  16. //
  17. // You should have received a copy of the GNU General Public License along with
  18. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  19. // Place, Suite 330, Boston, MA 02111-1307 USA
  20. //==============================================================================
  21. // See networking.hpp for notes.
  22. // See networking.inline.hpp for inlined methods & functions.
  23. #include "networking.hpp"
  24. Networking Network; // Finally creating the Network instance.
  25. //// Platform Specific Stuff ///////////////////////////////////////////////////
  26. #if defined(WIN32) || defined(WIN64)
  27. ////////////////////////////////////////////////////////////////////////////////
  28. //// Being Windows specific code
  29. WSADATA WSSTartData; // Socket library data structure.
  30. // Error description handling for humans.
  31. string Networking::DescriptiveError(string Msg, int Errno) { // Form a descriptive error w/ errno.
  32. string s = ""; // Message string.
  33. switch(Errno) { // Assign the appropriate message.
  34. case WSA_INVALID_HANDLE: s = "WSA_INVALID_HANDLE"; break;
  35. case WSA_NOT_ENOUGH_MEMORY: s = "WSA_NOT_ENOUGH_MEMORY"; break;
  36. case WSA_INVALID_PARAMETER: s = "WSA_INVALID_PARAMETER"; break;
  37. case WSA_OPERATION_ABORTED: s = "WSA_OPERATION_ABORTED"; break;
  38. case WSA_IO_INCOMPLETE: s = "WSA_IO_INCOMPLETE"; break;
  39. case WSA_IO_PENDING: s = "WSA_IO_PENDING"; break;
  40. case WSAEINTR: s = "WSAEINTR"; break;
  41. case WSAEBADF: s = "WSAEBADF"; break;
  42. case WSAEACCES: s = "WSAEACCES"; break;
  43. case WSAEFAULT: s = "WSAEFAULT"; break;
  44. case WSAEINVAL: s = "WSAEINVAL"; break;
  45. case WSAEMFILE: s = "WSAEMFILE"; break;
  46. case WSAEWOULDBLOCK: s = "WSAEWOULDBLOCK"; break;
  47. case WSAEINPROGRESS: s = "WSAEINPROGRESS"; break;
  48. case WSAEALREADY: s = "WSAEALREADY"; break;
  49. case WSAENOTSOCK: s = "WSAENOTSOCK"; break;
  50. case WSAEDESTADDRREQ: s = "WSAEDESTADDRREQ"; break;
  51. case WSAEMSGSIZE: s = "WSAEMSGSIZE"; break;
  52. case WSAEPROTOTYPE: s = "WSAEPROTOTYPE"; break;
  53. case WSAENOPROTOOPT: s = "WSAENOPROTOOPT"; break;
  54. case WSAEPROTONOSUPPORT: s = "WSAEPROTONOSUPPORT"; break;
  55. case WSAESOCKTNOSUPPORT: s = "WSAESOCKTNOSUPPORT"; break;
  56. case WSAEOPNOTSUPP: s = "WSAEOPNOTSUPP"; break;
  57. case WSAEPFNOSUPPORT: s = "WSAEPFNOSUPPORT"; break;
  58. case WSAEAFNOSUPPORT: s = "WSAEAFNOSUPPORT"; break;
  59. case WSAEADDRINUSE: s = "WSAEADDRINUSE"; break;
  60. case WSAEADDRNOTAVAIL: s = "WSAEADDRNOTAVAIL"; break;
  61. case WSAENETDOWN: s = "WSAENETDOWN"; break;
  62. case WSAENETUNREACH: s = "WSAENETUNREACH"; break;
  63. case WSAENETRESET: s = "WSAENETRESET"; break;
  64. case WSAECONNABORTED: s = "WSAECONNABORTED"; break;
  65. case WSAECONNRESET: s = "WSAECONNRESET"; break;
  66. case WSAENOBUFS: s = "WSAENOBUFS"; break;
  67. case WSAEISCONN: s = "WSAEISCONN"; break;
  68. case WSAENOTCONN: s = "WSAENOTCONN"; break;
  69. case WSAESHUTDOWN: s = "WSAESHUTDOWN"; break;
  70. case WSAETOOMANYREFS: s = "WSAETOOMANYREFS"; break;
  71. case WSAETIMEDOUT: s = "WSAETIMEDOUT"; break;
  72. case WSAECONNREFUSED: s = "WSAECONNREFUSED"; break;
  73. case WSAELOOP: s = "WSAELOOP"; break;
  74. case WSAENAMETOOLONG: s = "WSAENAMETOOLONG"; break;
  75. case WSAEHOSTDOWN: s = "WSAEHOSTDOWN"; break;
  76. case WSAEHOSTUNREACH: s = "WSAEHOSTUNREACH"; break;
  77. case WSAENOTEMPTY: s = "WSAENOTEMPTY"; break;
  78. case WSAEPROCLIM: s = "WSAEPROCLIM"; break;
  79. case WSAEUSERS: s = "WSAEUSERS"; break;
  80. case WSAEDQUOT: s = "WSAEDQUOT"; break;
  81. case WSAESTALE: s = "WSAESTALE"; break;
  82. case WSAEREMOTE: s = "WSAEREMOTE"; break;
  83. case WSASYSNOTREADY: s = "WSASYSNOTREADY"; break;
  84. case WSAVERNOTSUPPORTED: s = "WSAVERNOTSUPPORTED"; break;
  85. case WSANOTINITIALISED: s = "WSANOTINITIALISED"; break;
  86. case WSAEDISCON: s = "WSAEDISCON"; break;
  87. case WSAENOMORE: s = "WSAENOMORE"; break;
  88. case WSAECANCELLED: s = "WSAECANCELLED"; break;
  89. case WSAEINVALIDPROCTABLE: s = "WSAEINVALIDPROCTABLE"; break;
  90. case WSAEINVALIDPROVIDER: s = "WSAEINVALIDPROVIDER"; break;
  91. case WSAEPROVIDERFAILEDINIT: s = "WSAEPROVIDERFAILEDINIT"; break;
  92. case WSASYSCALLFAILURE: s = "WSASYSCALLFAILURE"; break;
  93. case WSASERVICE_NOT_FOUND: s = "WSASERVICE_NOT_FOUND"; break;
  94. case WSATYPE_NOT_FOUND: s = "WSATYPE_NOT_FOUND"; break;
  95. case WSA_E_NO_MORE: s = "WSA_E_NO_MORE"; break;
  96. case WSA_E_CANCELLED: s = "WSA_E_CANCELLED"; break;
  97. case WSAEREFUSED: s = "WSAEREFUSED"; break;
  98. case WSAHOST_NOT_FOUND: s = "WSAHOST_NOT_FOUND"; break;
  99. case WSATRY_AGAIN: s = "WSATRY_AGAIN"; break;
  100. case WSANO_RECOVERY: s = "WSANO_RECOVERY"; break;
  101. case WSANO_DATA: s = "WSANO_DATA"; break;
  102. case WSA_QOS_RECEIVERS: s = "WSA_QOS_RECEIVERS"; break;
  103. case WSA_QOS_SENDERS: s = "WSA_QOS_SENDERS"; break;
  104. case WSA_QOS_NO_SENDERS: s = "WSA_QOS_NO_SENDERS"; break;
  105. case WSA_QOS_NO_RECEIVERS: s = "WSA_QOS_NO_RECEIVERS"; break;
  106. case WSA_QOS_REQUEST_CONFIRMED: s = "WSA_QOS_REQUEST_CONFIRMED"; break;
  107. case WSA_QOS_ADMISSION_FAILURE: s = "WSA_QOS_ADMISSION_FAILURE"; break;
  108. case WSA_QOS_POLICY_FAILURE: s = "WSA_QOS_POLICY_FAILURE"; break;
  109. case WSA_QOS_BAD_STYLE: s = "WSA_QOS_BAD_STYLE"; break;
  110. case WSA_QOS_BAD_OBJECT: s = "WSA_QOS_BAD_OBJECT"; break;
  111. case WSA_QOS_TRAFFIC_CTRL_ERROR: s = "WSA_QOS_TRAFFIC_CTRL_ERROR"; break;
  112. case WSA_QOS_GENERIC_ERROR: s = "WSA_QOS_GENERIC_ERROR"; break;
  113. case WSA_QOS_ESERVICETYPE: s = "WSA_QOS_ESERVICETYPE"; break;
  114. case WSA_QOS_EFLOWSPEC: s = "WSA_QOS_EFLOWSPEC"; break;
  115. case WSA_QOS_EPROVSPECBUF: s = "WSA_QOS_EPROVSPECBUF"; break;
  116. case WSA_QOS_EFILTERSTYLE: s = "WSA_QOS_EFILTERSTYLE"; break;
  117. case WSA_QOS_EFILTERTYPE: s = "WSA_QOS_EFILTERTYPE"; break;
  118. case WSA_QOS_EFILTERCOUNT: s = "WSA_QOS_EFILTERCOUNT"; break;
  119. case WSA_QOS_EOBJLENGTH: s = "WSA_QOS_EOBJLENGTH"; break;
  120. case WSA_QOS_EFLOWCOUNT: s = "WSA_QOS_EFLOWCOUNT"; break;
  121. case WSA_QOS_EPOLICYOBJ: s = "WSA_QOS_EPOLICYOBJ"; break;
  122. case WSA_QOS_EFLOWDESC: s = "WSA_QOS_EFLOWDESC"; break;
  123. case WSA_QOS_EPSFLOWSPEC: s = "WSA_QOS_EPSFLOWSPEC"; break;
  124. case WSA_QOS_EPSFILTERSPEC: s = "WSA_QOS_EPSFILTERSPEC"; break;
  125. case WSA_QOS_ESDMODEOBJ: s = "WSA_QOS_ESDMODEOBJ"; break;
  126. case WSA_QOS_ESHAPERATEOBJ: s = "WSA_QOS_ESHAPERATEOBJ"; break;
  127. case WSA_QOS_RESERVED_PETYPE: s = "WSA_QOS_RESERVED_PETYPE"; break;
  128. #ifdef WSA_QOS_EUNKOWNPSOBJ
  129. case WSA_QOS_EUNKOWNPSOBJ: s = "WSA_QOS_EUNKOWNPSOBJ"; break;
  130. #endif
  131. }
  132. Msg.append(" "); // Add a space to the existing message.
  133. if(0 < s.length()) { // If we know the message for Errno
  134. Msg.append(s); // then append it.
  135. }
  136. else { // If we don't know what Errno means
  137. ostringstream ErrNoMsg; // then say so and pass on Errno as
  138. ErrNoMsg << " UNKNOWN ErrorNumber = " << Errno; // well so someone can figure it out.
  139. Msg.append(ErrNoMsg.str());
  140. }
  141. return Msg;
  142. };
  143. // Networking Constructor //////////////////////////////////////////////////////
  144. // Handles any necessary setup of Network Module resources.
  145. Networking::Networking() { // Upon initialization,
  146. if(0 != WSAStartup(MAKEWORD (2,0), &WSSTartData)) { // startup the Winsock2.0 DLL.
  147. throw InitializationError( // If that fails then throw!
  148. "Networking::Networking() if(0 != WSAStartup(MAKEWORD (2,0), &WSSTartData))"
  149. );
  150. }
  151. }
  152. // Networking Destructor ///////////////////////////////////////////////////////
  153. // Handles any necessary cleanup of Network Module resources.
  154. Networking::~Networking() { // Upon shutdown,
  155. WSACleanup(); // shutdown the Winsock DLL.
  156. }
  157. //// Emd Windows specific code
  158. ////////////////////////////////////////////////////////////////////////////////
  159. #else
  160. ////////////////////////////////////////////////////////////////////////////////
  161. //// Begin GNU specific code
  162. // Error description handling for humans.
  163. string Networking::DescriptiveError(string Msg, int Errno) { // Form a descriptive error w/ errno.
  164. Msg.append(" "); Msg.append(strerror(Errno));
  165. return Msg;
  166. };
  167. // Networking Constructor //////////////////////////////////////////////////////
  168. // Handles any necessary setup of Network Module resources.
  169. Networking::Networking() { // Upon initialization,
  170. // Nothing So Far... // nothing special required.
  171. }
  172. // Networking Destructor ///////////////////////////////////////////////////////
  173. // Handles any necessary cleanup of Network Module resources.
  174. Networking::~Networking() { // GNU sockets cleanup,
  175. // Nothing So Far... // nothing specail to required.
  176. }
  177. //// End GNU specific code
  178. ////////////////////////////////////////////////////////////////////////////////
  179. #endif
  180. ////////////////////////////////////////////////////////////////////////////////
  181. //// Platform Agnostic Stuff
  182. //// Useful Internal Bits & Pieces /////////////////////////////////////////////
  183. const int LowestOctetMask = 0x000000FF; // The bits to look at.
  184. const int OneOctetInBits = 8; // The bits to shift.
  185. void splitIP( // Split an IP into octets.
  186. unsigned long A, // The address in host format.
  187. int& a0, // Reference to the first octet.
  188. int& a1, // Reference to the second octet.
  189. int& a2, // Reference to the third octet.
  190. int& a3 // Reference to the forth octet.
  191. ){
  192. a3 = A & LowestOctetMask; A >>= OneOctetInBits; // Get the lowest order octet & move.
  193. a2 = A & LowestOctetMask; A >>= OneOctetInBits; // Get the next lowest octet & move.
  194. a1 = A & LowestOctetMask; A >>= OneOctetInBits; // Get the next lowest octet & move.
  195. a0 = A & LowestOctetMask; // Get the highest octet. That's IT!
  196. }
  197. //// IP4Address methods ////////////////////////////////////////////////////////
  198. IP4Address::operator unsigned long int() const { // Assign to unsigned long int.
  199. return IP; // Return it.
  200. }
  201. IP4Address::operator string() const { // Assign to a string.
  202. char stringbfr[IPStringBufferSize]; // Grab a temporary buffer.
  203. memset(stringbfr, 0, sizeof(stringbfr)); // Null out it's space.
  204. int a0, a1, a2, a3; // Grab some integers.
  205. splitIP(IP, a0, a1, a2, a3); // Split the IP in the IP4Address.
  206. sprintf(stringbfr, "%d.%d.%d.%d", a0, a1, a2, a3); // Format the octets.
  207. return string(stringbfr); // Return a string.
  208. }
  209. //// SocketAddress methods /////////////////////////////////////////////////////
  210. // getAddress(str, len)
  211. const char* SocketAddress::getAddress(char* str) { // Get the IP address into a cstring.
  212. if(NULL == str) { // If the caller did not provide a
  213. str = IPStringBuffer; // buffer to use then we will use ours.
  214. }
  215. int a0, a1, a2, a3; // Grab a bunch of handy integers.
  216. getAddress(a0, a1, a2, a3); // Get the address as octets.
  217. sprintf(str, "%d.%d.%d.%d", a0, a1, a2, a3); // Format as dotted decimal notation.
  218. return str; // Return the output buffer.
  219. }
  220. // getAddress(int& a0, int& a1, int& a2, int& a3)
  221. void SocketAddress::getAddress(int& a0, int& a1, int& a2, int& a3) { // Get the IP address into 4 ints
  222. unsigned long A = getAddress(); // Get the address.
  223. splitIP(A, a0, a1, a2, a3); // Split it into octets.
  224. }
  225. //// TCPListener methods ///////////////////////////////////////////////////////
  226. TCPListener::TCPListener(unsigned short Port) { // Set up localhost on this Port.
  227. LocalAddress.setPort(Port); // Establish the port.
  228. LocalAddress.setAddress(LOCALHOST); // Set the address to LOCALHOST.
  229. MaxPending = DefaultMaxPending; // Use the default inbound queue size.
  230. ReuseAddress = true; // ReuseAddress on by default.
  231. OpenStage1Complete = false; // This stage of open() not yet done.
  232. OpenStage2Complete = false; // This stage of open() not yet done.
  233. // Create a socket...
  234. LastError = 0;
  235. Handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // Create the socket.
  236. if(INVALID_SOCKET == Handle) { // If that operation failed then
  237. LastError = Network.getLastError(); // grab the error code and
  238. throw Networking::SocketCreationError( // throw.
  239. Network.DescriptiveError(
  240. "TCPListener::TCPListener().socket()", LastError));
  241. }
  242. }
  243. TCPListener::TCPListener(SocketAddress& WhereToBind) { // Set up specific "name" for listening.
  244. LocalAddress = WhereToBind; // Make my Local address as provided.
  245. MaxPending = DefaultMaxPending; // Use the default inbound queue size.
  246. ReuseAddress = true; // ReuseAddress on by default.
  247. OpenStage1Complete = false; // This stage of open() not yet done.
  248. OpenStage2Complete = false; // This stage of open() not yet done.
  249. // Create a socket...
  250. LastError = 0;
  251. Handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // Create the socket.
  252. if(INVALID_SOCKET == Handle) { // If that operation failed then
  253. LastError = Network.getLastError(); // grab the error code and
  254. throw Networking::SocketCreationError( // throw.
  255. Network.DescriptiveError(
  256. "TCPListener::TCPListener().socket()", LastError));
  257. }
  258. }
  259. // open()
  260. void TCPListener::open() { // Open when ready.
  261. if(OpenSucceeded) return; // If open already, we're done.
  262. LastError = 0; // Clear the last error.
  263. bool SuccessFlag = true; // Start optimistically.
  264. // Set SO_REUSEADDR if turned on
  265. if(!OpenStage1Complete) { // Do this stage only once.
  266. int ReuseAddress_Flag = (ReuseAddress? 1:0); // Setup an appropriate integer flag.
  267. int result = // Set SO_REUSEADDR before bind().
  268. setsockopt(
  269. Handle,
  270. SOL_SOCKET,
  271. SO_REUSEADDR,
  272. (char*) &ReuseAddress_Flag,
  273. sizeof(ReuseAddress_Flag));
  274. if(0 > result) { // If there was an error then
  275. SuccessFlag = false; // we did not succeed.
  276. LastError = Network.getLastError(); // Capture the error information and
  277. throw Networking::SocketSetSockOptError( // throw.
  278. Network.DescriptiveError(
  279. "TCPListener::open().setsockopt(SO_REUSEADDR)", LastError));
  280. }
  281. OpenStage1Complete = true; // Stage 1 complete now.
  282. } // End of open() stage 1
  283. // Next we bind it...
  284. if(!OpenStage2Complete) { // Do this stage only once.
  285. int result = // Bind our socket to the LocalAddress.
  286. ::bind(
  287. Handle,
  288. LocalAddress.getPtr_sockaddr(),
  289. LocalAddress.getAddressSize());
  290. if(0 > result) { // If there was an error then
  291. SuccessFlag = false; // we did not succeed.
  292. LastError = Network.getLastError(); // Capture the error information and
  293. throw Networking::SocketBindError( // throw.
  294. Network.DescriptiveError(
  295. "TCPListener::open().bind()", LastError));
  296. }
  297. OpenStage2Complete = true; // Stage 2 complete now.
  298. } // End of open() stage 2
  299. // Then we put it in a listening state...
  300. int result = listen(Handle, MaxPending); // Listen for up to MaxPending at once.
  301. if(0 > result) { // If an error occurred then
  302. SuccessFlag = false; // we did not succeed.
  303. LastError = Network.getLastError(); // Capture the error information and
  304. throw Networking::SocketListenError( // throw.
  305. Network.DescriptiveError(
  306. "TCPListener::open().listen()", LastError));
  307. }
  308. OpenSucceeded = SuccessFlag; // So, did we succeed?
  309. }
  310. // acceptClient()
  311. TCPClient* TCPListener::acceptClient() { // Accept a client connection.
  312. LastError = 0; // Clear the last error.
  313. socklen_t rsize = RemoteAddress.getAddressSize(); // Size as an int for accept().
  314. hSocket NewHandle = // Accept a new connection if available.
  315. accept(
  316. Handle, // use our handle, of course,...
  317. RemoteAddress.getPtr_sockaddr(), // and store the remote hosts
  318. &rsize); // address for us.
  319. if(INVALID_SOCKET == NewHandle) { // If there was an error then
  320. LastError = Network.getLastError(); // capture the error value.
  321. if(!Network.WouldBlock(LastError)) { // If it's not a EWOULDBLOCK error
  322. throw Networking::SocketAcceptError( // then we need to throw.
  323. Network.DescriptiveError(
  324. "TCPListener::acceptClient().accept()", LastError));
  325. } else { // EWOULDBLOCK errors are normal in
  326. return NULL; // non blocking mode so we return
  327. } // NULL when we see them.
  328. }
  329. // Set SO_NOSIGPIPE if needed
  330. if( // On some systems we may have to
  331. 0 != SO_NOSIGPIPE && // use SO_NOSIPIPE but if they offer
  332. 0 == MSG_NOSIGNAL // MSG_NOSIGNAL we prefer that instead.
  333. ) {
  334. int TurnedOn = 1; // Prepare to turn this option on.
  335. int result = // Set SO_NOSIGPIPE.
  336. setsockopt(
  337. NewHandle,
  338. SOL_SOCKET,
  339. SO_NOSIGPIPE,
  340. (char*) &TurnedOn,
  341. sizeof(TurnedOn));
  342. if(0 > result) { // If there was an error then
  343. LastError = Network.getLastError(); // Capture the error information
  344. Network.closeSocket(NewHandle); // close the handle (avoid leaks)
  345. throw Networking::SocketSetSockOptError( // and throw a descriptive exception.
  346. Network.DescriptiveError(
  347. "TCPListener::acceptClient().setsockopt(SO_NOSIGPIPE)", LastError));
  348. }
  349. }
  350. // If things have gone well we can do what we came for.
  351. return new TCPClient(*this, NewHandle, RemoteAddress); // Create the new TCPClient object.
  352. }
  353. //// TCPClient methods /////////////////////////////////////////////////////////
  354. int TCPClient::transmit(const char* bfr, int size) { // How to send a buffer of data.
  355. if(0 == size) return 0; // Nothing to send, send nothing.
  356. if(0 == bfr) // Watch out for null buffers.
  357. throw Networking::SocketWriteError("TCPClient::transmit() NULL Bfr!");
  358. if(0 > size) // Watch out for bad sizes.
  359. throw Networking::SocketWriteError("TCPClient::transmit() 0 > size!");
  360. LastError = 0; // No errors yet.
  361. int ByteCount = 0; // No bytes sent yet this pass.
  362. ByteCount = send(Handle, bfr, size, MSG_NOSIGNAL); // Try to send and capture the count.
  363. LastError = Network.getLastError(); // Grab any error code.
  364. bool AnErrorOccurred = (0 > ByteCount); // How to know if an error occurred.
  365. const int NoBytesSent = 0; // This is our "Would Block" result.
  366. if(AnErrorOccurred) { // If there was an error check it out.
  367. if(Network.WouldBlock(LastError)) { // If the error was "Would Block" then
  368. return NoBytesSent; // return no bytes sent (try again).
  369. } else { // If this was a different kind of error
  370. throw Networking::SocketWriteError( // then throw!
  371. Network.DescriptiveError(
  372. "TCPClient::transmit().send()", LastError));
  373. }
  374. }
  375. return ByteCount; // Usually: return the sent byte count.
  376. }
  377. int TCPClient::receive(char* bfr, int size) { // How to receive a buffer of data.
  378. if(ReadBufferIsEmpty()) { // If the read buffer is empty then
  379. fillReadBuffer(); // fill it first.
  380. } // Optimize our transfer to the smaller
  381. if(DataLength < size) { // of what we have or the size of the
  382. size = DataLength; // provided buffer. This way we ony check
  383. } // one value in our copy loop ;-)
  384. int RemainingDataLength = size; // Capture the length of data to xfer.
  385. while(0 < RemainingDataLength) { // While we have work to do
  386. *bfr = *ReadPointer; // copy each byte from our ReadBuffer,
  387. bfr++; ReadPointer++; // move the pointers to the next byte,
  388. DataLength--; // update our ReadBuffers's DataLength,
  389. RemainingDataLength--; // and count down the bytes left to xfer.
  390. }
  391. return size; // When done, say how much we moved.
  392. }
  393. int TCPClient::delimited_receive(char* bfr, int size, char delimiter) { // How to receive delimited data.
  394. if(ReadBufferIsEmpty()) { // If the read buffer is empty then
  395. fillReadBuffer(); // fill it first.
  396. } // Optimize our transfer to the smaller
  397. if(DataLength < size) { // of what we have or the size of the
  398. size = DataLength; // provided buffer. This way we ony check
  399. } // one value in our copy loop ;-)
  400. int Count = 0; // Keep our byte count in scope.
  401. bool DelimiterNotReached = true; // Watching for our deliimiter.
  402. while((Count < size) && DelimiterNotReached) { // While there is work to do...
  403. *bfr = *ReadPointer; // copy each byte from our ReadBuffer,
  404. DelimiterNotReached = (delimiter != (*bfr)); // check for the delimiter character,
  405. bfr++; ReadPointer++; // move the pointers to the next byte,
  406. DataLength--; // update our ReadBuffers's DataLength,
  407. Count++; // and count up the bytes we have moved.
  408. }
  409. return Count; // When done, say how much we moved.
  410. }
  411. //// TCPHost methods ///////////////////////////////////////////////////////////
  412. // Constructors...
  413. TCPHost::TCPHost(unsigned short Port) { // Will connect to localhost on Port.
  414. RemoteAddress.setPort(Port); // Connect to Port on
  415. RemoteAddress.setAddress(LOCALHOST); // Localhost.
  416. ReadPointer = ReadBuffer; // Set the read position to zero.
  417. DataLength = 0; // There is no data yet.
  418. ReuseAddress = false; // ReuseAddress off by default.
  419. OpenStage1Complete = false; // Stage 1 of open() not done yet.
  420. // Create a socket to use.
  421. LastError = 0; // Clear our last error value.
  422. Handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // Create the socket.
  423. if(0 > Handle) { // If that operation failed then
  424. LastError = Network.getLastError(); // grab the error code and
  425. throw Networking::SocketCreationError( // throw.
  426. Network.DescriptiveError(
  427. "TCPHost::TCPHost().socket()", LastError));
  428. }
  429. }
  430. TCPHost::TCPHost(SocketAddress& Remote) { // Will connect to Remote address/port.
  431. RemoteAddress = Remote; // Capture the provided address.
  432. ReadPointer = ReadBuffer; // Set the read position to zero.
  433. DataLength = 0; // There is no data yet.
  434. ReuseAddress = false; // ReuseAddress off by default.
  435. OpenStage1Complete = false; // Stage 1 of open() not done yet.
  436. // Create a socket to use.
  437. LastError = 0; // Clear our last error value.
  438. Handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // Create the socket.
  439. if(0 > Handle) { // If that operation failed then
  440. LastError = Network.getLastError(); // grab the error code and
  441. throw Networking::SocketCreationError( // throw.
  442. Network.DescriptiveError(
  443. "TCPHost::TCPHost().socket()", LastError));
  444. }
  445. }
  446. // Methods...
  447. void TCPHost::open() { // We provide open().
  448. if(OpenSucceeded) return; // If open already, we're done.
  449. LastError = 0; // Clear our LastError value.
  450. bool SuccessFlag = true; // Begin optimistically.
  451. // Set Socket Options
  452. if(!OpenStage1Complete) { // If we haven't done this yet:
  453. // Set SO_REUSEADDR if turned on
  454. int ReuseAddress_Flag = (ReuseAddress? 1:0); // Setup an appropriate integer flag.
  455. int result = // Set SO_REUSEADDR before bind().
  456. setsockopt(
  457. Handle,
  458. SOL_SOCKET,
  459. SO_REUSEADDR,
  460. (char*) &ReuseAddress_Flag,
  461. sizeof(ReuseAddress_Flag));
  462. if(0 > result) { // If there was an error then
  463. SuccessFlag = false; // we did not succeed.
  464. LastError = Network.getLastError(); // Capture the error information and
  465. throw Networking::SocketSetSockOptError( // throw.
  466. Network.DescriptiveError(
  467. "TCPHost::open().setsockopt(SO_REUSEADDR)", LastError));
  468. }
  469. // Set SO_NOSIGPIPE if needed
  470. if( // On some systems we may have to
  471. 0 != SO_NOSIGPIPE && // use SO_NOSIPIPE but if they offer
  472. 0 == MSG_NOSIGNAL // MSG_NOSIGNAL we prefer that instead.
  473. ) {
  474. int TurnedOn = 1; // Prepare to turn this option on.
  475. int result = // Set SO_NOSIGPIPE.
  476. setsockopt(
  477. Handle,
  478. SOL_SOCKET,
  479. SO_NOSIGPIPE,
  480. (char*) &TurnedOn,
  481. sizeof(TurnedOn));
  482. if(0 > result) { // If there was an error then
  483. SuccessFlag = false; // we did not succeed.
  484. LastError = Network.getLastError(); // Capture the error information and
  485. throw Networking::SocketSetSockOptError( // throw.
  486. Network.DescriptiveError(
  487. "TCPHost::open().setsockopt(SO_NOSIGPIPE)", LastError));
  488. }
  489. }
  490. OpenStage1Complete = true; // Skip this section from now on.
  491. } // Done with stage 1.
  492. // Connect the socekt to the Host.
  493. int result = // Connect to the remote host
  494. connect( // using the socket we just
  495. Handle, // stored in Handle and
  496. RemoteAddress.getPtr_sockaddr(), // the Remote address.
  497. RemoteAddress.getAddressSize());
  498. if(0 > result) { // If there was an error then
  499. SuccessFlag = false; // we did not succeed.
  500. LastError = Network.getLastError(); // Record the error data.
  501. if(Network.IsConnected(LastError)) { // If we actually did succeed then
  502. SuccessFlag = true; // say so. (Silly Winsock!)
  503. } else // But if that's not the case check
  504. if( // to see if something bad happened -
  505. !Network.WouldBlock(LastError) && // not just would-block, or
  506. !Network.InProgress(LastError) // in progress...
  507. ) { // If it was something other than
  508. throw Networking::SocketConnectError( // WouldBlock or InProgress then
  509. Network.DescriptiveError( // throw!
  510. "TCPHost::open().connect()", LastError));
  511. } // If it was WouldBlock then it's
  512. } // considered to be ok.
  513. OpenSucceeded = SuccessFlag; // So, are we open now?
  514. }
  515. int TCPHost::transmit(const char* bfr, int size) { // How to send a buffer of data.
  516. LastError = 0; // No errors yet.
  517. if(0 == size) return 0; // Nothing to send, send nothing.
  518. if(0 == bfr) // Watch out for null buffers.
  519. throw Networking::SocketWriteError("TCPHost::transmit() NULL Bfr!");
  520. if(0 > size) // Watch out for bad sizes.
  521. throw Networking::SocketWriteError("TCPHost::transmit() 0 > size!");
  522. int ByteCount = send(Handle, bfr, size, MSG_NOSIGNAL); // Try to send and capture the count.
  523. if(0 > ByteCount) ByteCount = 0; // Mask error results as 0 bytes sent.
  524. if(size > ByteCount) { // If we didn't send it all check it out.
  525. LastError = Network.getLastError(); // Grab the error code.
  526. if(Network.WouldBlock(LastError)) { // If the error was WouldBlock then
  527. return ByteCount; // it was a partial snd - return.
  528. } else { // If this was a different kind of error
  529. throw Networking::SocketWriteError( // then throw!
  530. Network.DescriptiveError(
  531. "TCPHost::transmit().send()", LastError));
  532. }
  533. }
  534. return ByteCount; // Ultimately return the byte count.
  535. }
  536. int TCPHost::receive(char* bfr, int size) { // How to receive a buffer of data.
  537. if(ReadBufferIsEmpty()) { // If the read buffer is empty then
  538. fillReadBuffer(); // fill it first.
  539. } // Optimize our transfer to the smaller
  540. if(DataLength < size) { // of what we have or the size of the
  541. size = DataLength; // provided buffer. This way we ony check
  542. } // one value in our copy loop ;-)
  543. int RemainingDataLength = size; // Capture the length of data to xfer.
  544. while(0 < RemainingDataLength) { // While we have work to do
  545. *bfr = *ReadPointer; // copy each byte from our ReadBuffer,
  546. bfr++; ReadPointer++; // move the pointers to the next byte,
  547. DataLength--; // update our ReadBuffers's DataLength,
  548. RemainingDataLength--; // and count down the bytes left to xfer.
  549. }
  550. return size; // When done, say how much we moved.
  551. }
  552. int TCPHost::delimited_receive(char* bfr, int size, char delimiter) { // How to receive delimited data.
  553. if(ReadBufferIsEmpty()) { // If the read buffer is empty then
  554. fillReadBuffer(); // fill it first.
  555. } // Optimize our transfer to the smaller
  556. if(DataLength < size) { // of what we have or the size of the
  557. size = DataLength; // provided buffer. This way we ony check
  558. } // one value in our copy loop ;-)
  559. int Count = 0; // Keep our byte count in scope.
  560. bool DelimiterNotReached = true; // Watching for our deliimiter.
  561. while((Count < size) && DelimiterNotReached) { // While there is work to do...
  562. *bfr = *ReadPointer; // copy each byte from our ReadBuffer,
  563. DelimiterNotReached = (delimiter != (*bfr)); // check for the delimiter character,
  564. bfr++; ReadPointer++; // move the pointers to the next byte,
  565. DataLength--; // update our ReadBuffers's DataLength,
  566. Count++; // and count up the bytes we have moved.
  567. }
  568. return Count; // When done, say how much we moved.
  569. }
  570. // End Platform Agnostic Stuff
  571. ////////////////////////////////////////////////////////////////////////////////