00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "ortp/port.h"
00023 #include "ortp/ortp.h"
00024 #include "utils.h"
00025
00026 #if defined(_WIN32) && !defined(_WIN32_WCE)
00027 #include <process.h>
00028 #endif
00029
00030 void* ortp_malloc(size_t sz){
00031 return malloc(sz);
00032 }
00033
00034 void* ortp_realloc(void *ptr, size_t sz){
00035 return realloc(ptr,sz);
00036 }
00037
00038 void * ortp_malloc0(size_t size){
00039 void *ptr=malloc(size);
00040 memset(ptr,0,size);
00041 return ptr;
00042 }
00043
00044 char * ortp_strdup(const char *tmp){
00045 #if defined(_WIN32) || defined(_WIN32_WCE)
00046 return _strdup(tmp);
00047 #else
00048 return strdup(tmp);
00049 #endif
00050 }
00051
00052
00053 void ortp_free(void* ptr){
00054 free(ptr);
00055 }
00056
00057
00058
00059
00060
00061
00062 int set_non_blocking_socket (ortp_socket_t sock)
00063 {
00064
00065
00066 #if !defined(_WIN32) && !defined(_WIN32_WCE)
00067 return fcntl (sock, F_SETFL, O_NONBLOCK);
00068 #else
00069 unsigned long nonBlock = 1;
00070 return ioctlsocket(sock, FIONBIO , &nonBlock);
00071 #endif
00072 }
00073
00074
00075
00076
00077
00078
00079
00080 int close_socket(ortp_socket_t sock){
00081 #if !defined(_WIN32) && !defined(_WIN32_WCE)
00082 return close (sock);
00083 #else
00084 return closesocket(sock);
00085 #endif
00086 }
00087
00088
00089
00090 #if !defined(_WIN32) && !defined(_WIN32_WCE)
00091
00092 #else
00093 int inet_aton (const char * cp, struct in_addr * addr)
00094 {
00095 unsigned long retval;
00096
00097 retval = inet_addr (cp);
00098
00099 if (retval == INADDR_NONE)
00100 {
00101 return -1;
00102 }
00103 else
00104 {
00105 addr->S_un.S_addr = retval;
00106 return 1;
00107 }
00108 }
00109 #endif
00110
00111 char *ortp_strndup(const char *str,int n){
00112 int min=MIN((int)strlen(str),n)+1;
00113 char *ret=(char*)ortp_malloc(min);
00114 strncpy(ret,str,n);
00115 ret[min-1]='\0';
00116 return ret;
00117 }
00118
00119 #if !defined(_WIN32) && !defined(_WIN32_WCE)
00120 int __ortp_thread_join(ortp_thread_t thread, void **ptr){
00121 int err=pthread_join(thread,ptr);
00122 if (err!=0) {
00123 ortp_error("pthread_join error: %s",strerror(err));
00124 }
00125 return err;
00126 }
00127 #endif
00128 #if defined(_WIN32) || defined(_WIN32_WCE)
00129
00130 int WIN_mutex_init(ortp_mutex_t *mutex, void *attr)
00131 {
00132 *mutex=CreateMutex(NULL, FALSE, NULL);
00133 return 0;
00134 }
00135
00136 int WIN_mutex_lock(ortp_mutex_t * hMutex)
00137 {
00138 WaitForSingleObject(*hMutex, INFINITE);
00139 return 0;
00140 }
00141
00142 int WIN_mutex_unlock(ortp_mutex_t * hMutex)
00143 {
00144 ReleaseMutex(*hMutex);
00145 return 0;
00146 }
00147
00148 int WIN_mutex_destroy(ortp_mutex_t * hMutex)
00149 {
00150 CloseHandle(*hMutex);
00151 return 0;
00152 }
00153
00154 typedef struct thread_param{
00155 void * (*func)(void *);
00156 void * arg;
00157 }thread_param_t;
00158
00159 static unsigned WINAPI thread_starter(void *data){
00160 thread_param_t *params=(thread_param_t*)data;
00161 void *ret=params->func(params->arg);
00162 ortp_free(data);
00163 return (DWORD)ret;
00164 }
00165
00166 #if defined _WIN32_WCE
00167 # define _beginthreadex CreateThread
00168 # define _endthreadex ExitThread
00169 #endif
00170
00171 int WIN_thread_create(ortp_thread_t *th, void *attr, void * (*func)(void *), void *data)
00172 {
00173 thread_param_t *params=ortp_new(thread_param_t,1);
00174 params->func=func;
00175 params->arg=data;
00176 *th=(HANDLE)_beginthreadex( NULL, 0, thread_starter, params, 0, NULL);
00177 return 0;
00178 }
00179
00180 int WIN_thread_join(ortp_thread_t thread_h, void **unused)
00181 {
00182 if (thread_h!=NULL)
00183 {
00184 WaitForSingleObject(thread_h, INFINITE);
00185 CloseHandle(thread_h);
00186 }
00187 return 0;
00188 }
00189
00190 int WIN_cond_init(ortp_cond_t *cond, void *attr)
00191 {
00192 *cond=CreateEvent(NULL, FALSE, FALSE, NULL);
00193 return 0;
00194 }
00195
00196 int WIN_cond_wait(ortp_cond_t* hCond, ortp_mutex_t * hMutex)
00197 {
00198
00199 WIN_mutex_unlock(hMutex);
00200 WaitForSingleObject(*hCond, INFINITE);
00201 WIN_mutex_lock(hMutex);
00202 return 0;
00203 }
00204
00205 int WIN_cond_signal(ortp_cond_t * hCond)
00206 {
00207 SetEvent(*hCond);
00208 return 0;
00209 }
00210
00211 int WIN_cond_broadcast(ortp_cond_t * hCond)
00212 {
00213 WIN_cond_signal(hCond);
00214 return 0;
00215 }
00216
00217 int WIN_cond_destroy(ortp_cond_t * hCond)
00218 {
00219 CloseHandle(*hCond);
00220 return 0;
00221 }
00222
00223
00224 #if defined(_WIN32_WCE)
00225 #include <time.h>
00226
00227 int
00228 gettimeofday (struct timeval *tv, void *tz)
00229 {
00230 DWORD timemillis = GetTickCount();
00231 tv->tv_sec = timemillis/1000;
00232 tv->tv_usec = (timemillis - (tv->tv_sec*1000)) * 1000;
00233 return 0;
00234 }
00235
00236 #else
00237
00238 int gettimeofday (struct timeval *tv, void* tz)
00239 {
00240 union
00241 {
00242 __int64 ns100;
00243 FILETIME fileTime;
00244 } now;
00245
00246 GetSystemTimeAsFileTime (&now.fileTime);
00247 tv->tv_usec = (long) ((now.ns100 / 10LL) % 1000000LL);
00248 tv->tv_sec = (long) ((now.ns100 - 116444736000000000LL) / 10000000LL);
00249 return (0);
00250 }
00251
00252 #endif
00253
00254 const char *getWinSocketError(int error)
00255 {
00256 static char buf[80];
00257
00258 switch (error)
00259 {
00260 case WSANOTINITIALISED: return "Windows sockets not initialized : call WSAStartup";
00261 case WSAEADDRINUSE: return "Local Address already in use";
00262 case WSAEADDRNOTAVAIL: return "The specified address is not a valid address for this machine";
00263 case WSAEINVAL: return "The socket is already bound to an address.";
00264 case WSAENOBUFS: return "Not enough buffers available, too many connections.";
00265 case WSAENOTSOCK: return "The descriptor is not a socket.";
00266 case WSAECONNRESET: return "Connection reset by peer";
00267
00268 default :
00269 sprintf(buf, "Error code : %d", error);
00270 return buf;
00271 break;
00272 }
00273
00274 return buf;
00275 }
00276
00277 #ifdef _WORKAROUND_MINGW32_BUGS
00278 char * WSAAPI gai_strerror(int errnum){
00279 return (char*)getWinSocketError(errnum);
00280 }
00281 #endif
00282
00283 #endif
00284