Re: [Libevent-users] Success compile libevent 1.4.3 with VC6

2008-04-23 Thread Eugene 'HMage' Bujak

Hi,

I want to submit a more complete patch for VC6.

This one makes all elements of the code compilable and this code is
being used on a production-level project at work.

The patch is in attachment. Compiles and works under VC6.
* signal_test works.
* timer_test works.
* event_test fails due to the reason that win32 select() can't work on
anything but network sockets.

Most importantly, my code that runs perfectly well on unix (linux 
freebsd) now runs on windows with little modification.

But:
* Didn't try to run any other tests since there are no project files for
them.
* Didn't test how mingw32 copes with these changes either.

The main changes are:
* INPUT and OUTPUT are defined in platform SDK, so I had to redefine
them as EVRPC_INPUT and EVRPC_OUTPUT.
* uint64_t on MSVC must be defined as unsigned __int64
* int64_t on MSVC is to be defined as signed __int64
* MSVC6 doesn't know about __func__ and the #ifdefs weren't guarding
against them good enough.
* winsock2.h sometimes was included after windows.h, that lead to
compiler errors in libevent.
* select.c can be painlessly excluded from compilation in win32, but I
still fixed it's compilation.
* Winsock library needs to be initialized and freed explicitly on win32.
* http.c was using sockaddr_storage. There isn't one in MSVC. Using
sockaddr is good enough for MSVC.
* There are tons of compiler warnings regarding do {} while(0);
* I took the liberty to add #pragma to the WIN32-section of the event.h
so that there's no need to put ws2_32 library into the linker explicitly.
* There's no NFDBITS in msvc.
* vsnprintf() is _vsnprintf() for some weird reason on win32.
* I've had to bump tv-tv_usec to 1000 to prevent eating 100% of the cpu
core in win32 dispatch when using timers with events each 10ms or so.
* Also I've provided a gettimeofday() wrapper that uses timeGetTime()
instead of much slower _ftime().



Also, I would like to ask the maintainer of the project to change
svn:eol-style property of .dsw and .dsp files on SVN repository toto
CRLF, since project loader handles these files properly only when line
endings are in windows style.


--
- Eugene 'HMage' Bujak.

diff -ur libevent-1.4.3-stable/WIN32-Code/config.h 
libevent-1.4.3-stable-win32/WIN32-Code/config.h
--- libevent-1.4.3-stable/WIN32-Code/config.h   Wed Apr 23 17:31:16 2008
+++ libevent-1.4.3-stable-win32/WIN32-Code/config.h Wed Apr 23 17:31:03 2008
@@ -205,8 +205,8 @@
 /* Version number of package */
 #define VERSION 1.3.99-trunk
 
-/* Define to appropriate substitue if compiler doesnt have __func__ */
-#if defined(_MSC_VER)  _MSC_VER  1300
+/* Define to appropriate substitute if compiler doesn't have __func__ */
+#ifdef _MSC_VER
 #define __func__ ??
 #else
 #define __func__ __FUNCTION__
diff -ur libevent-1.4.3-stable/WIN32-Code/misc.c 
libevent-1.4.3-stable-win32/WIN32-Code/misc.c
--- libevent-1.4.3-stable/WIN32-Code/misc.c Wed Apr 23 17:31:16 2008
+++ libevent-1.4.3-stable-win32/WIN32-Code/misc.c   Wed Apr 23 17:31:03 2008
@@ -1,9 +1,14 @@
 #include stdio.h
 #include string.h
+#include winsock2.h // winsock2.h must be before windows.h to have any effect
 #include windows.h
 #include sys/timeb.h
 #include time.h
 
+#ifdef _MSC_VER
+#pragma comment(lib,winmm)
+#endif
+
 #ifdef __GNUC__
 /*our prototypes for timeval and timezone are in here, just in case the above
   headers don't have them*/
@@ -16,7 +21,7 @@
  *
  * Purpose:  Get current time of day.
  *
- * Arguments: tv = Place to store the curent time of day.
+ * Arguments: tv = Place to store the current time of day.
  *tz = Ignored.
  *
  * Returns: 0 = Success.
@@ -24,6 +29,7 @@
  /
 
 #ifndef HAVE_GETTIMEOFDAY
+#ifndef _WIN32
 int gettimeofday(struct timeval *tv, struct timezone *tz) {
struct _timeb tb;
 
@@ -35,6 +41,21 @@
tv-tv_usec = ((int) tb.millitm) * 1000;
return 0;
 }
+#else
+struct timezone;
+int gettimeofday(struct timeval *tv, struct timezone *tz) {
+   DWORD ms;
+   tz;
+   if(tv == NULL)
+   return -1;
+
+   ms = timeGetTime();
+   tv-tv_sec = ms / 1000;//(long)floor(ms * 0.001);
+   tv-tv_usec = (ms - (tv-tv_sec*1000))*1000;
+
+   return 0;
+}
+#endif
 #endif
 
 #if 0
diff -ur libevent-1.4.3-stable/WIN32-Code/win32.c 
libevent-1.4.3-stable-win32/WIN32-Code/win32.c
--- libevent-1.4.3-stable/WIN32-Code/win32.cWed Apr 23 17:31:16 2008
+++ libevent-1.4.3-stable-win32/WIN32-Code/win32.c  Wed Apr 23 17:32:04 2008
@@ -32,7 +32,7 @@
 #include ../config.h
 #endif
 
-#include winsock2.h
+#include winsock2.h // winsock2.h must be before windows.h to have any effect
 #include windows.h
 #include sys/types.h
 #include sys/queue.h
@@ -234,6 +234,27 @@
 {
struct win32op *winop;
size_t size;
+
+#ifdef _WIN32
+   WORD wVersionRequested;
+   WSADATA wsaData;
+   int retval;
+
+   wVersionRequested = MAKEWORD( 2, 2 );
+
+   

Re: [Libevent-users] Success compile libevent 1.4.3 with VC6

2008-04-23 Thread Nick Mathewson
On Wed, Apr 23, 2008 at 05:34:04PM +0400, Eugene 'HMage' Bujak wrote:
 Hi,
 
 I want to submit a more complete patch for VC6.
 
 This one makes all elements of the code compilable and this code is
 being used on a production-level project at work.

 The patch is in attachment. Compiles and works under VC6.
 * signal_test works.
 * timer_test works.
 * event_test fails due to the reason that win32 select() can't work on
 anything but network sockets.

Thanks for the patch; it's good stuff!  I'd like to apply it, but
there are a few small issues or things that could be done better in
order to work well on *non*-vc6 projects.


 * INPUT and OUTPUT are defined in platform SDK, so I had to redefine
 them as EVRPC_INPUT and EVRPC_OUTPUT.

We can't do this change in mainline libevent; it'll break all existing
evprc users that use the old names.  Maybe there's some way we we can
make the old names available on non-vc code, but deprecated in favor
of the EVRPC names?  something like this:

enum EVRPC_HOOK_TYPE {
  EVRPC_INPUT,
  EVPRC_OUTPUT,
};

#ifndef _WIN32
#define INPUT EVRPC_INPUT
#define OUTPUT EVRPC_OUTPUT
#endif

 [...]
 * Winsock library needs to be initialized and freed explicitly on win32.

This is true, but it's not libevent's job to do it.  If we make this
change so that libevent starts initializing winsock, we'll break all
existing programs that use libevent and know to initialize winsock
themselves.  It's quite reasonable to call some sockets code before
calling event_init, but this part of the patch makes that result in
double-initializing winsock.

Also, I think this change will double-initialize winsock on all
programs that use multiple event bases, and double-shutdown winsock
whenever the bases are closed on those programs.

 [...]
 -/* Define to appropriate substitue if compiler doesnt have __func__ */
 -#if defined(_MSC_VER)  _MSC_VER  1300
 +/* Define to appropriate substitute if compiler doesn't have __func__ */
 +#ifdef _MSC_VER
  #define __func__ ??


I thought some versions of VC *did* have an acceptable version of
__func__ or a replacement for it.  If that's true, this will break
those other VCs.  What is _MSC_VER defined to on VC6?  Should the test
be something other than _MSC_VER  1300?


 diff -ur libevent-1.4.3-stable/event-config.h 
 libevent-1.4.3-stable-win32/event-config.h
 --- libevent-1.4.3-stable/event-config.h  Wed Apr 23 17:31:14 2008
 +++ libevent-1.4.3-stable-win32/event-config.hWed Apr 23 17:31:01 2008
 @@ -51,7 +51,9 @@
  #define _EVENT_HAVE_INET_NTOP 1
  
  /* Define to 1 if you have the inttypes.h header file. */
 +#ifndef _MSC_VER // MSVC doesn't have that
  #define _EVENT_HAVE_INTTYPES_H 1
 +#endif


This approach is the wrong way to make event-config.h work.  On all
toolsets besides VC, the file is automatically generated from
config.h, which itself is automatically generated by the autoconf
script.   The right approach for VC is just to have a separate config.h
and event-config.h script that work, ideally in the WIN32-Code
directory, and set up the compiler's search path so those get used
instead of any other ones that might be kicking around.


 [...]
 +#ifdef _WIN32 // MSVC doesn't have that

FWIW, many older C compilers don't allow C++-style comments: they
weren't added to the C standard till the C99 standard.  We need
libevent to build on older compilers, so we can't add C++-style
comments to libevent.


Otherwise, though, this looks like solid work, and I'm quite glad that
somebody has a copy of VC6 and the willingness to do it!  If you can
send in a revised patch, that'd be great.  Otherwise, I'll try to
hand-apply it with modifications as noted above, but since my x86 box
is down at the moment, I can't fire up windows to test it out now, and
things might go badly.


many thanks,
-- 
Nick Mathewson
___
Libevent-users mailing list
Libevent-users@monkey.org
http://monkeymail.org/mailman/listinfo/libevent-users