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

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