OSDN Git Service

Change a directory tree
[peercast-im/PeerCastIM.git] / core / win32 / wsys.cpp
1 // ------------------------------------------------
2 // File : wsys.cpp
3 // Date: 4-apr-2002
4 // Author: giles
5 // Desc: 
6 //              WSys derives from Sys to provide basic win32 functions such as starting threads.
7 //
8 // (c) 2002 peercast.org
9 // ------------------------------------------------
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
14
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 // ------------------------------------------------
20
21
22 //#include "stdafx.h"
23 #include <process.h>
24 #include <windows.h>
25 #include <time.h>
26 #include "wsys.h"
27 #include "wsocket.h"
28 #include "..\common\stats.h"
29 #include "..\common\peercast.h"
30 #include <sys/timeb.h>
31 #include <time.h>
32 #include "shellapi.h"
33 #ifdef _DEBUG
34 #include "chkMemoryLeak.h"
35 #define DEBUG_NEW new(__FILE__, __LINE__)
36 #define new DEBUG_NEW
37 #endif
38
39 // ---------------------------------
40 WSys::WSys(HWND w)
41 {
42         stats.clear();
43         
44         rndGen.setSeed(getTime());
45         
46         mainWindow = w;
47         WSAClientSocket::init();
48
49         rndSeed = rnd();
50 }
51 // ---------------------------------
52 double WSys::getDTime()
53 {
54    struct _timeb timebuffer;
55
56    _ftime( &timebuffer );
57
58    return (double)timebuffer.time+(((double)timebuffer.millitm)/1000);
59 }
60 // ---------------------------------
61 unsigned int WSys::getTime()
62 {
63         time_t ltime;
64         time( &ltime );
65         return (unsigned)ltime;
66 }
67
68 // ---------------------------------
69 ClientSocket *WSys::createSocket()
70 {
71     return new WSAClientSocket();
72 }
73 // ---------------------------------
74 void WSys::endThread(ThreadInfo *info)
75 {
76 }             
77 // ---------------------------------
78 void WSys::waitThread(ThreadInfo *info, int timeout)
79 {
80         switch(WaitForSingleObject((void *)info->handle, timeout))
81         {
82       case WAIT_TIMEOUT:
83           throw TimeoutException();
84           break;
85         }
86 }
87   
88
89 // ---------------------------------
90 bool    WSys::startThread(ThreadInfo *info)
91 {
92         info->active = true;
93
94 /*      typedef unsigned ( __stdcall *start_address )( void * );
95
96         unsigned int threadID;
97         info->handle = (unsigned int)_beginthreadex( NULL, 0, (start_address)info->func, info, 0, &threadID );
98
99     if(info->handle == 0) 
100                 return false;*/
101
102         typedef void (__cdecl *start_address)( void * );
103         info->handle = _beginthread((start_address)info->func, 0,info);
104
105     if(info->handle == -1) 
106                 return false;
107
108   return true;
109
110 }
111 // ---------------------------------
112 void    WSys::sleep(int ms)
113 {
114         Sleep(ms);
115 }
116
117 // ---------------------------------
118 void WSys::appMsg(long msg, long arg)
119 {
120         //SendMessage(mainWindow,WM_USER,(WPARAM)msg,(LPARAM)arg);
121 }
122
123 // --------------------------------------------------
124 void WSys::callLocalURL(const char *str,int port)
125 {
126         char cmd[512];
127         sprintf(cmd,"http://127.0.0.1:%d/%s",port,str);
128         ShellExecute(mainWindow, NULL, cmd, NULL, NULL, SW_SHOWNORMAL);
129 }
130
131 // ---------------------------------
132 void WSys::getURL(const char *url)
133 {
134         if (mainWindow)
135                 if (strnicmp(url,"http://",7) || strnicmp(url,"mailto:",7))
136                         ShellExecute(mainWindow, NULL, url, NULL, NULL, SW_SHOWNORMAL);
137 }
138 // ---------------------------------
139 void WSys::exit()
140 {
141         if (mainWindow)
142                 PostMessage(mainWindow,WM_CLOSE,0,0);
143         else
144                 ::exit(0);
145 }
146 // --------------------------------------------------
147 void WSys::executeFile(const char *file)
148 {
149     ShellExecute(NULL,"open",file,NULL,NULL,SW_SHOWNORMAL);  
150 }