Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

networking.cpp 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. char* s = 0;
  33. switch(Errno) {
  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(" ");
  133. if(s) {
  134. Msg.append(s);
  135. }
  136. else {
  137. ostringstream ErrNoMsg;
  138. ErrNoMsg << " UNKNOWN ErrorNumber = " << Errno;
  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. LastError = 0; // No errors yet.
  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. int ByteCount = send(Handle, bfr, size, MSG_NOSIGNAL); // Try to send and capture the count.
  362. if(0 > ByteCount) ByteCount = 0; // Mask error results as 0 bytes sent.
  363. if(size > ByteCount) { // If we didn't send it all check it out.
  364. LastError = Network.getLastError(); // Grab the error code.
  365. if(Network.WouldBlock(LastError)) { // If the error was WouldBlock then
  366. return ByteCount; // it was a partial send - return.
  367. } else { // If this was a different kind of error
  368. throw Networking::SocketWriteError( // then throw!
  369. Network.DescriptiveError(
  370. "TCPClient::transmit().send()", LastError));
  371. }
  372. }
  373. return ByteCount; // Ultimately return the byte count.
  374. }
  375. int TCPClient::receive(char* bfr, int size) { // How to receive a buffer of data.
  376. if(ReadBufferIsEmpty()) { // If the read buffer is empty then
  377. fillReadBuffer(); // fill it first.
  378. } // Optimize our transfer to the smaller
  379. if(DataLength < size) { // of what we have or the size of the
  380. size = DataLength; // provided buffer. This way we ony check
  381. } // one value in our copy loop ;-)
  382. int RemainingDataLength = size; // Capture the length of data to xfer.
  383. while(0 < RemainingDataLength) { // While we have work to do
  384. *bfr = *ReadPointer; // copy each byte from our ReadBuffer,
  385. bfr++; ReadPointer++; // move the pointers to the next byte,
  386. DataLength--; // update our ReadBuffers's DataLength,
  387. RemainingDataLength--; // and count down the bytes left to xfer.
  388. }
  389. return size; // When done, say how much we moved.
  390. }
  391. int TCPClient::delimited_receive(char* bfr, int size, char delimiter) { // How to receive delimited data.
  392. if(ReadBufferIsEmpty()) { // If the read buffer is empty then
  393. fillReadBuffer(); // fill it first.
  394. } // Optimize our transfer to the smaller
  395. if(DataLength < size) { // of what we have or the size of the
  396. size = DataLength; // provided buffer. This way we ony check
  397. } // one value in our copy loop ;-)
  398. int Count = 0; // Keep our byte count in scope.
  399. bool DelimiterNotReached = true; // Watching for our deliimiter.
  400. while((Count < size) && DelimiterNotReached) { // While there is work to do...
  401. *bfr = *ReadPointer; // copy each byte from our ReadBuffer,
  402. DelimiterNotReached = (delimiter != (*bfr)); // check for the delimiter character,
  403. bfr++; ReadPointer++; // move the pointers to the next byte,
  404. DataLength--; // update our ReadBuffers's DataLength,
  405. Count++; // and count up the bytes we have moved.
  406. }
  407. return Count; // When done, say how much we moved.
  408. }
  409. //// TCPHost methods ///////////////////////////////////////////////////////////
  410. // Constructors...
  411. TCPHost::TCPHost(unsigned short Port) { // Will connect to localhost on Port.
  412. RemoteAddress.setPort(Port); // Connect to Port on
  413. RemoteAddress.setAddress(LOCALHOST); // Localhost.
  414. ReadPointer = ReadBuffer; // Set the read position to zero.
  415. DataLength = 0; // There is no data yet.
  416. ReuseAddress = false; // ReuseAddress off by default.
  417. OpenStage1Complete = false; // Stage 1 of open() not done yet.
  418. // Create a socket to use.
  419. LastError = 0; // Clear our last error value.
  420. Handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // Create the socket.
  421. if(0 > Handle) { // If that operation failed then
  422. LastError = Network.getLastError(); // grab the error code and
  423. throw Networking::SocketCreationError( // throw.
  424. Network.DescriptiveError(
  425. "TCPHost::TCPHost().socket()", LastError));
  426. }
  427. }
  428. TCPHost::TCPHost(SocketAddress& Remote) { // Will connect to Remote address/port.
  429. RemoteAddress = Remote; // Capture the provided address.
  430. ReadPointer = ReadBuffer; // Set the read position to zero.
  431. DataLength = 0; // There is no data yet.
  432. ReuseAddress = false; // ReuseAddress off by default.
  433. OpenStage1Complete = false; // Stage 1 of open() not done yet.
  434. // Create a socket to use.
  435. LastError = 0; // Clear our last error value.
  436. Handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // Create the socket.
  437. if(0 > Handle) { // If that operation failed then
  438. LastError = Network.getLastError(); // grab the error code and
  439. throw Networking::SocketCreationError( // throw.
  440. Network.DescriptiveError(
  441. "TCPHost::TCPHost().socket()", LastError));
  442. }
  443. }
  444. // Methods...
  445. void TCPHost::open() { // We provide open().
  446. if(OpenSucceeded) return; // If open already, we're done.
  447. LastError = 0; // Clear our LastError value.
  448. bool SuccessFlag = true; // Begin optimistically.
  449. // Set Socket Options
  450. if(!OpenStage1Complete) { // If we haven't done this yet:
  451. // Set SO_REUSEADDR if turned on
  452. int ReuseAddress_Flag = (ReuseAddress? 1:0); // Setup an appropriate integer flag.
  453. int result = // Set SO_REUSEADDR before bind().
  454. setsockopt(
  455. Handle,
  456. SOL_SOCKET,
  457. SO_REUSEADDR,
  458. (char*) &ReuseAddress_Flag,
  459. sizeof(ReuseAddress_Flag));
  460. if(0 > result) { // If there was an error then
  461. SuccessFlag = false; // we did not succeed.
  462. LastError = Network.getLastError(); // Capture the error information and
  463. throw Networking::SocketSetSockOptError( // throw.
  464. Network.DescriptiveError(
  465. "TCPHost::open().setsockopt(SO_REUSEADDR)", LastError));
  466. }
  467. // Set SO_NOSIGPIPE if needed
  468. if( // On some systems we may have to
  469. 0 != SO_NOSIGPIPE && // use SO_NOSIPIPE but if they offer
  470. 0 == MSG_NOSIGNAL // MSG_NOSIGNAL we prefer that instead.
  471. ) {
  472. int TurnedOn = 1; // Prepare to turn this option on.
  473. int result = // Set SO_NOSIGPIPE.
  474. setsockopt(
  475. Handle,
  476. SOL_SOCKET,
  477. SO_NOSIGPIPE,
  478. (char*) &TurnedOn,
  479. sizeof(TurnedOn));
  480. if(0 > result) { // If there was an error then
  481. SuccessFlag = false; // we did not succeed.
  482. LastError = Network.getLastError(); // Capture the error information and
  483. throw Networking::SocketSetSockOptError( // throw.
  484. Network.DescriptiveError(
  485. "TCPHost::open().setsockopt(SO_NOSIGPIPE)", LastError));
  486. }
  487. }
  488. OpenStage1Complete = true; // Skip this section from now on.
  489. } // Done with stage 1.
  490. // Connect the socekt to the Host.
  491. int result = // Connect to the remote host
  492. connect( // using the socket we just
  493. Handle, // stored in Handle and
  494. RemoteAddress.getPtr_sockaddr(), // the Remote address.
  495. RemoteAddress.getAddressSize());
  496. if(0 > result) { // If there was an error then
  497. SuccessFlag = false; // we did not succeed.
  498. LastError = Network.getLastError(); // Record the error data.
  499. if(Network.IsConnected(LastError)) { // If we actually did succeed then
  500. SuccessFlag = true; // say so. (Silly Winsock!)
  501. } else // But if that's not the case check
  502. if( // to see if something bad happened -
  503. !Network.WouldBlock(LastError) && // not just would-block, or
  504. !Network.InProgress(LastError) // in progress...
  505. ) { // If it was something other than
  506. throw Networking::SocketConnectError( // WouldBlock or InProgress then
  507. Network.DescriptiveError( // throw!
  508. "TCPHost::open().connect()", LastError));
  509. } // If it was WouldBlock then it's
  510. } // considered to be ok.
  511. OpenSucceeded = SuccessFlag; // So, are we open now?
  512. }
  513. int TCPHost::transmit(const char* bfr, int size) { // How to send a buffer of data.
  514. LastError = 0; // No errors yet.
  515. if(0 == size) return 0; // Nothing to send, send nothing.
  516. if(0 == bfr) // Watch out for null buffers.
  517. throw Networking::SocketWriteError("TCPHost::transmit() NULL Bfr!");
  518. if(0 > size) // Watch out for bad sizes.
  519. throw Networking::SocketWriteError("TCPHost::transmit() 0 > size!");
  520. int ByteCount = send(Handle, bfr, size, MSG_NOSIGNAL); // Try to send and capture the count.
  521. if(0 > ByteCount) ByteCount = 0; // Mask error results as 0 bytes sent.
  522. if(size > ByteCount) { // If we didn't send it all check it out.
  523. LastError = Network.getLastError(); // Grab the error code.
  524. if(Network.WouldBlock(LastError)) { // If the error was WouldBlock then
  525. return ByteCount; // it was a partial snd - return.
  526. } else { // If this was a different kind of error
  527. throw Networking::SocketWriteError( // then throw!
  528. Network.DescriptiveError(
  529. "TCPHost::transmit().send()", LastError));
  530. }
  531. }
  532. return ByteCount; // Ultimately return the byte count.
  533. }
  534. int TCPHost::receive(char* bfr, int size) { // How to receive a buffer of data.
  535. if(ReadBufferIsEmpty()) { // If the read buffer is empty then
  536. fillReadBuffer(); // fill it first.
  537. } // Optimize our transfer to the smaller
  538. if(DataLength < size) { // of what we have or the size of the
  539. size = DataLength; // provided buffer. This way we ony check
  540. } // one value in our copy loop ;-)
  541. int RemainingDataLength = size; // Capture the length of data to xfer.
  542. while(0 < RemainingDataLength) { // While we have work to do
  543. *bfr = *ReadPointer; // copy each byte from our ReadBuffer,
  544. bfr++; ReadPointer++; // move the pointers to the next byte,
  545. DataLength--; // update our ReadBuffers's DataLength,
  546. RemainingDataLength--; // and count down the bytes left to xfer.
  547. }
  548. return size; // When done, say how much we moved.
  549. }
  550. int TCPHost::delimited_receive(char* bfr, int size, char delimiter) { // How to receive delimited data.
  551. if(ReadBufferIsEmpty()) { // If the read buffer is empty then
  552. fillReadBuffer(); // fill it first.
  553. } // Optimize our transfer to the smaller
  554. if(DataLength < size) { // of what we have or the size of the
  555. size = DataLength; // provided buffer. This way we ony check
  556. } // one value in our copy loop ;-)
  557. int Count = 0; // Keep our byte count in scope.
  558. bool DelimiterNotReached = true; // Watching for our deliimiter.
  559. while((Count < size) && DelimiterNotReached) { // While there is work to do...
  560. *bfr = *ReadPointer; // copy each byte from our ReadBuffer,
  561. DelimiterNotReached = (delimiter != (*bfr)); // check for the delimiter character,
  562. bfr++; ReadPointer++; // move the pointers to the next byte,
  563. DataLength--; // update our ReadBuffers's DataLength,
  564. Count++; // and count up the bytes we have moved.
  565. }
  566. return Count; // When done, say how much we moved.
  567. }
  568. // End Platform Agnostic Stuff
  569. ////////////////////////////////////////////////////////////////////////////////