Advertisement
tm512

Archvile net.cpp

Oct 23rd, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. /*
  2.    Archvile
  3.    Copyright [c] 2011 tm512, All Rights Reserved
  4.  
  5.    This program is free software: you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation, either version 3 of the License, or
  8.    (at your option) any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. using namespace std;
  20.  
  21. #include <iostream>
  22. #include <fcntl.h>
  23. #include <netdb.h>
  24. #include <netinet/in.h>
  25. #include <sys/socket.h>
  26. #include <string.h>
  27.  
  28. #include "net.h"
  29.  
  30. // select () nonsense :P
  31. int highestSocket = 0;
  32. fd_set fdread, fdwrite;
  33.  
  34. int InitNet ()
  35. {
  36. //  cout << "InitNet: Initializing Network" << endl;
  37.     FD_ZERO (&fdread);
  38.     FD_ZERO (&fdwrite);
  39.  
  40.     return 0;
  41. }
  42.  
  43. CSocket::CSocket ()
  44. {
  45.     cout << "Opening a socket at " << this << endl;
  46.     fcntl (sock, F_SETFL, O_NONBLOCK);
  47. }
  48.  
  49. CSocket::CSocket (string &host, string &port)
  50. {
  51.     cout << "Opening a socket to " << host << ":" << port << " at " << this << endl;
  52.     fcntl (sock, F_SETFL, O_NONBLOCK);
  53.  
  54.     connect (host, port);
  55. }
  56.  
  57. CSocket::~CSocket ()
  58. {
  59.     cout << "Destroying socket " << this << endl;
  60. }
  61.  
  62. bool CSocket::connected ()
  63. {
  64.     return !(sock < 0);
  65. }
  66.  
  67. int CSocket::connect (string &host, string &port)
  68. {
  69.     addrinfo hints, *res, *r;
  70.     timeval conn_timeout = { 10, 0 };
  71.     fd_set fdtmp; // Temporary fd_set for timing out of select
  72.  
  73.     // util_info("connecting to %s:%s.\n", host, port);
  74.     cout << "connecting to " << host << ":" << port << "." << endl;
  75.     sock = 0;
  76.     this->host = host;
  77.     this->port = port;
  78.  
  79.     memset (&hints, 0, sizeof hints);
  80.     hints.ai_family = AF_UNSPEC;
  81.     hints.ai_socktype = SOCK_STREAM; // Tee Cee Pee
  82.  
  83.     if (getaddrinfo (host.c_str (), port.c_str (), &hints, &res) != 0)
  84.     {
  85.         // fprintf (stderr, "[error] could not resolve host %s:%s.\n", host, port);
  86.         cout << "[error] could not resolve host " << host << ":" << port << "." << endl;
  87.         return sock = -1;
  88.     }
  89.  
  90.     // Kind of messy. Keep looping until we get a good connection
  91.     for (r = res; r; r = r->ai_next)
  92.     {
  93.         if ((sock = socket (r->ai_family, r->ai_socktype, r->ai_protocol)) < 0)
  94.             continue;
  95.  
  96.         FD_ZERO (&fdtmp);
  97.         FD_SET (sock, &fdtmp);
  98.         fcntl (sock, F_SETFL, O_NONBLOCK);
  99.  
  100.         ::connect (sock, r->ai_addr, r->ai_addrlen);
  101.  
  102.         if (select (sock + 1, &fdread, &fdwrite, NULL, &conn_timeout) == 1)
  103.         {
  104.             int serror;
  105.             socklen_t slen = sizeof (serror);
  106.             getsockopt (sock, SOL_SOCKET, SO_ERROR, &serror, &slen);
  107.  
  108.             if (!serror) // Success!
  109.                 break;
  110.         }
  111.  
  112.         ::close (sock);
  113.     }
  114.  
  115.     freeaddrinfo (res);
  116.  
  117.     if (!r)
  118.     {
  119.         // fprintf (stderr, "[error] could not connect to host %s:%s.\n", host, port);
  120.         cout << "[error] could not connect to host " << host << ":" << port << "." << endl;
  121.         return sock = -1;
  122.     }
  123.  
  124.     FD_SET (sock, &fdread);
  125.     FD_SET (sock, &fdwrite);
  126.  
  127.     if (sock > highestSocket)
  128.         highestSocket = sock;
  129.  
  130.     return sock;
  131. }
  132.  
  133. int CSocket::send (char *tosend)
  134. {
  135.     int totalToSend = strlen (tosend), totalSent = 0, tempSent = 0;
  136.     cout << "sending: " << tosend << " (" << totalToSend << " bytes." << endl;
  137.  
  138.     while (totalSent < totalToSend)
  139.     {
  140.         tempSent = ::send (sock, tosend + totalSent, totalToSend - totalSent, 0);
  141.         if (tempSent < 0)
  142.         {
  143.             cout << "[error] failed on send: " << tosend << endl;
  144.             return 1;
  145.         }
  146.         totalSent += tempSent;
  147.     }
  148.  
  149.     return 0;
  150. }
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement