NewNet/util.h

00001 /*  NewNet - A networking framework in C++
00002     Copyright (C) 2006 Ingmar K. Steen (iksteen@gmail.com)
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018  */
00019 
00020 #ifndef NEWNET_UTIL_H
00021 #define NEWNET_UTIL_H
00022 
00023 #include "platform.h"
00024 
00025 static inline long
00026 difftime(const struct timeval & a, const struct timeval & b)
00027 {
00028   long diff_s = a.tv_sec - b.tv_sec;
00029   long diff_u;
00030   diff_u = a.tv_usec - b.tv_usec;
00031   if(diff_u < 0)
00032   {
00033     diff_s -= 1;
00034     diff_u += 1000000;
00035   }
00036   else if(diff_u >= 1000000)
00037   {
00038     diff_s += 1;
00039     diff_u -= 1000000;
00040   }
00041   return (diff_s * 1000) + (diff_u / 1000);
00042 }
00043 
00044 #ifdef WIN32
00045 // NOTE: This piece of code came from a post in the CURL mailing list
00046 static inline int
00047 gettimeofday(struct timeval * tv, void * tz)
00048 {
00049   union
00050   {
00051     LONGLONG ns100;
00052     FILETIME ft;
00053   } now;
00054   GetSystemTimeAsFileTime(&now.ft);
00055   tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL);
00056   tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 1000000LL);
00057   return 0;
00058 }
00059 #endif // WIN32
00060 
00061 inline static void
00062 setnonblocking(int sock)
00063 {
00064 #ifndef WIN32
00065   int mode = fcntl(sock, F_GETFL, 0);
00066   fcntl(sock, F_SETFL, mode|O_NONBLOCK);
00067 #else
00068   u_long ioctlArg = 1;
00069   ioctlsocket(sock, FIONBIO, &ioctlArg);
00070 #endif // ! WIN32
00071 }
00072 
00073 #endif // NEWNET_UTIL_H

Generated on Sun Jan 7 14:00:01 2007 for NewNet by  doxygen 1.5.1