您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

networking.cpp 43KB

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