00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 #include "CallStack.h"
00026
00027 #include "Utility.h"
00028
00029 #ifdef USING_STREAMS
00030 #include <stropts.h>
00031 #include <sys/conf.h>
00032 #endif
00033 #include <math.h>
00034 #include <stdio.h>
00035
00036 namespace FIX
00037 {
00038 void string_replace( const std::string& oldValue,
00039 const std::string& newValue,
00040 std::string& value )
00041 { QF_STACK_PUSH(string_replace)
00042
00043 for( std::string::size_type pos = value.find(oldValue);
00044 pos != std::string::npos;
00045 pos = value.find(oldValue, pos) )
00046 {
00047 value.replace( pos, oldValue.size(), newValue );
00048 pos += newValue.size();
00049 }
00050
00051 QF_STACK_POP
00052 }
00053
00054 void socket_init()
00055 { QF_STACK_PUSH(socket_init)
00056
00057 #ifdef _MSC_VER
00058 WORD version = MAKEWORD( 2, 2 );
00059 WSADATA data;
00060 WSAStartup( version, &data );
00061 #else
00062 struct sigaction sa;
00063 sa.sa_handler = SIG_IGN;
00064 sigemptyset( &sa.sa_mask );
00065 sa.sa_flags = 0;
00066 sigaction( SIGPIPE, &sa, 0 );
00067 #endif
00068
00069 QF_STACK_POP
00070 }
00071
00072 void socket_term()
00073 { QF_STACK_PUSH(socket_term)
00074
00075 #ifdef _MSC_VER
00076 WSACleanup();
00077 #endif
00078
00079 QF_STACK_POP
00080 }
00081
00082 int socket_createAcceptor(int port, bool reuse)
00083 { QF_STACK_PUSH(socket_createAcceptor)
00084
00085 int socket = ::socket( PF_INET, SOCK_STREAM, 0 );
00086 if ( socket < 0 ) return -1;
00087
00088 sockaddr_in address;
00089 socklen_t socklen;
00090
00091 address.sin_family = PF_INET;
00092 address.sin_port = htons( port );
00093 address.sin_addr.s_addr = INADDR_ANY;
00094 socklen = sizeof( address );
00095 if( reuse )
00096 socket_setsockopt( socket, SO_REUSEADDR );
00097
00098 int result = bind( socket, reinterpret_cast < sockaddr* > ( &address ),
00099 socklen );
00100 if ( result < 0 ) return -1;
00101 result = listen( socket, SOMAXCONN );
00102 if ( result < 0 ) return -1;
00103 return socket;
00104
00105 QF_STACK_POP
00106 }
00107
00108 int socket_createConnector()
00109 { QF_STACK_PUSH(socket_createConnector)
00110 return ::socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
00111 QF_STACK_POP
00112 }
00113
00114 int socket_connect( int socket, const char* address, int port )
00115 { QF_STACK_PUSH(socket_connect)
00116
00117 const char* hostname = socket_hostname( address );
00118 if( hostname == 0 ) return -1;
00119
00120 sockaddr_in addr;
00121 addr.sin_family = PF_INET;
00122 addr.sin_port = htons( port );
00123 addr.sin_addr.s_addr = inet_addr( hostname );
00124
00125 int result = connect( socket, reinterpret_cast < sockaddr* > ( &addr ),
00126 sizeof( addr ) );
00127
00128 return result;
00129
00130 QF_STACK_POP
00131 }
00132
00133 int socket_accept( int s )
00134 { QF_STACK_PUSH(socket_accept)
00135
00136 if ( !socket_isValid( s ) ) return -1;
00137 return accept( s, 0, 0 );
00138
00139 QF_STACK_POP
00140 }
00141
00142 int socket_send( int s, const char* msg, int length )
00143 { QF_STACK_PUSH(socket_send)
00144 return send( s, msg, length, 0 );
00145 QF_STACK_POP
00146 }
00147
00148 void socket_close( int s )
00149 { QF_STACK_PUSH(socket_close)
00150
00151 shutdown( s, 2 );
00152 #ifdef _MSC_VER
00153 closesocket( s );
00154 #else
00155 close( s );
00156 #endif
00157
00158 QF_STACK_POP
00159 }
00160
00161 bool socket_fionread( int s, int& bytes )
00162 { QF_STACK_PUSH(socket_fionread)
00163
00164 bytes = 0;
00165 #if defined(_MSC_VER)
00166 return ::ioctlsocket( s, FIONREAD, &( ( unsigned long& ) bytes ) ) == 0;
00167 #elif defined(USING_STREAMS)
00168 return ::ioctl( s, I_NREAD, &bytes ) >= 0;
00169 #else
00170 return ::ioctl( s, FIONREAD, &bytes ) == 0;
00171 #endif
00172
00173 QF_STACK_POP
00174 }
00175
00176 bool socket_disconnected( int s )
00177 { QF_STACK_PUSH(socket_disconnected)
00178
00179 char byte;
00180 return ::recv (s, &byte, sizeof (byte), MSG_PEEK) <= 0;
00181
00182 QF_STACK_POP
00183 }
00184
00185 int socket_setsockopt( int s, int opt )
00186 { QF_STACK_PUSH(socket_setsockopt)
00187
00188 int level = SOL_SOCKET;
00189 if( opt == TCP_NODELAY )
00190 level = IPPROTO_TCP;
00191
00192 #ifdef _MSC_VER
00193 BOOL optval = TRUE;
00194 return ::setsockopt( s, level, opt,
00195 ( char* ) & optval, sizeof( optval ) );
00196 #else
00197 int optval = 1;
00198 return ::setsockopt( s, level, opt,
00199 &optval, sizeof( optval ) );
00200 #endif
00201
00202 QF_STACK_POP
00203 }
00204
00205 int socket_getsockopt( int s, int opt, int& optval )
00206 { QF_STACK_PUSH(socket_getsockopt)
00207
00208 int level = SOL_SOCKET;
00209 if( opt == TCP_NODELAY )
00210 level = IPPROTO_TCP;
00211
00212 #ifdef _MSC_VER
00213 int length;
00214 #else
00215 socklen_t length;
00216 #endif
00217
00218 return ::getsockopt( s, level, opt,
00219 ( char* ) & optval, & length );
00220
00221 QF_STACK_POP
00222 }
00223
00224 #ifndef _MSC_VER
00225 int socket_fcntl( int s, int opt, int arg )
00226 { QF_STACK_PUSH(socket_fcntl)
00227 return ::fcntl( s, opt, arg );
00228 QF_STACK_POP
00229 }
00230
00231 int socket_getfcntlflag( int s, int arg )
00232 { QF_STACK_PUSH(socket_getfcntlflag)
00233 return socket_fcntl( s, F_GETFL, arg );
00234 QF_STACK_POP
00235 }
00236
00237 int socket_setfcntlflag( int s, int arg )
00238 { QF_STACK_PUSH(socket_setfcntlflag)
00239
00240 int oldValue = socket_getfcntlflag( s, arg );
00241 oldValue |= arg;
00242 return socket_fcntl( s, F_SETFL, arg );
00243
00244 QF_STACK_POP
00245 }
00246 #endif
00247
00248 void socket_setnonblock( int socket )
00249 { QF_STACK_PUSH(socket_setnonblock)
00250
00251 #ifdef _MSC_VER
00252 u_long opt = 1;
00253 ::ioctlsocket( socket, FIONBIO, &opt );
00254 #else
00255 socket_setfcntlflag( socket, O_NONBLOCK );
00256 #endif
00257
00258 QF_STACK_POP
00259 }
00260 bool socket_isValid( int socket )
00261 { QF_STACK_PUSH(socket_isValid)
00262
00263 #ifdef _MSC_VER
00264 return socket != INVALID_SOCKET;
00265 #else
00266 return socket >= 0;
00267 #endif
00268
00269 QF_STACK_POP
00270 }
00271
00272 #ifndef _MSC_VER
00273 bool socket_isBad( int s )
00274 { QF_STACK_PUSH(socket_isBad)
00275
00276 struct stat buf;
00277 fstat( s, &buf );
00278 return errno == EBADF;
00279
00280 QF_STACK_POP
00281 }
00282 #endif
00283
00284 void socket_invalidate( int& socket )
00285 { QF_STACK_PUSH(socket_invalidate)
00286
00287 #ifdef _MSC_VER
00288 socket = INVALID_SOCKET;
00289 #else
00290 socket = -1;
00291 #endif
00292
00293 QF_STACK_POP
00294 }
00295
00296 short socket_hostport( int socket )
00297 { QF_STACK_PUSH(socket_hostport)
00298
00299 struct sockaddr_in addr;
00300 socklen_t len = sizeof(addr);
00301 if( getsockname(socket, (struct sockaddr*)&addr, &len) < 0 )
00302 return 0;
00303
00304 return ntohs( addr.sin_port );
00305
00306 QF_STACK_POP
00307 }
00308
00309 const char* socket_hostname( int socket )
00310 { QF_STACK_PUSH(socket_hostname)
00311
00312 struct sockaddr_in addr;
00313 socklen_t len = sizeof(addr);
00314 if( getsockname(socket, (struct sockaddr*)&addr, &len) < 0 )
00315 return 0;
00316
00317 return inet_ntoa( addr.sin_addr );
00318
00319 QF_STACK_POP
00320 }
00321
00322 const char* socket_hostname( const char* name )
00323 { QF_STACK_PUSH(socket_hostname)
00324
00325 struct hostent* host_ptr = 0;
00326 struct in_addr** paddr;
00327 struct in_addr saddr;
00328
00329 #if( GETHOSTBYNAME_R_INPUTS_RESULT || GETHOSTBYNAME_R_RETURNS_RESULT )
00330 hostent host;
00331 char buf[1024];
00332 int error;
00333 #endif
00334
00335 saddr.s_addr = inet_addr( name );
00336 if ( saddr.s_addr != ( unsigned ) - 1 ) return name;
00337
00338 #if GETHOSTBYNAME_R_INPUTS_RESULT
00339 gethostbyname_r( name, &host, buf, sizeof(buf), &host_ptr, &error );
00340 #elif GETHOSTBYNAME_R_RETURNS_RESULT
00341 host_ptr = gethostbyname_r( name, &host, buf, sizeof(buf), &error );
00342 #else
00343 host_ptr = gethostbyname( name );
00344 #endif
00345
00346 if ( host_ptr == 0 ) return 0;
00347
00348 paddr = ( struct in_addr ** ) host_ptr->h_addr_list;
00349 return inet_ntoa( **paddr );
00350
00351 QF_STACK_POP
00352 }
00353
00354 const char* socket_peername( int socket )
00355 { QF_STACK_PUSH(socket_peername)
00356
00357 struct sockaddr_in addr;
00358 socklen_t len = sizeof(addr);
00359 if( getpeername( socket, (struct sockaddr*)&addr, &len ) < 0 )
00360 return 0;
00361 hostent* host = gethostbyaddr( (char*)&addr.sin_addr, sizeof(addr.sin_addr), AF_INET );
00362 if( host == NULL )
00363 return 0;
00364 return host->h_name;
00365
00366 QF_STACK_POP
00367 }
00368
00369 std::pair<int, int> socket_createpair()
00370 { QF_STACK_PUSH(socket_createpair)
00371
00372 #ifdef _MSC_VER
00373 int acceptor = socket_createAcceptor(0, true);
00374 const char* host = socket_hostname( acceptor );
00375 short port = socket_hostport( acceptor );
00376 int client = socket_createConnector();
00377 socket_connect( client, "localhost", port );
00378 int server = socket_accept( acceptor );
00379 return std::pair<int, int>( client, server );
00380 #else
00381 int pair[2];
00382 socketpair( AF_UNIX, SOCK_STREAM, 0, pair );
00383 return std::pair<int, int>( pair[0], pair[1] );
00384 #endif
00385
00386 QF_STACK_POP
00387 }
00388
00389 tm time_gmtime( const time_t* t )
00390 { QF_STACK_PUSH(time_gmtime)
00391
00392 #ifdef _MSC_VER
00393 #if( _MSC_VER >= 1400 )
00394 tm result;
00395 gmtime_s( &result, t );
00396 return result;
00397 #else
00398 return *gmtime( t );
00399 #endif
00400 #else
00401 tm result;
00402 return *gmtime_r( t, &result );
00403 #endif
00404
00405 QF_STACK_POP
00406 }
00407
00408 tm time_localtime( const time_t* t)
00409 { QF_STACK_PUSH(time_localtime)
00410
00411 #ifdef _MSC_VER
00412 #if( _MSC_VER >= 1400 )
00413 tm result;
00414 localtime_s( &result, t );
00415 return result;
00416 #else
00417 return *localtime( t );
00418 #endif
00419 #else
00420 tm result;
00421 return *localtime_r( t, &result );
00422 #endif
00423
00424 QF_STACK_POP
00425 }
00426
00427 bool thread_spawn( THREAD_START_ROUTINE func, void* var, unsigned& thread )
00428 {
00429 #ifdef _MSC_VER
00430 unsigned int result = 0;
00431 unsigned int id = 0;
00432 result = _beginthreadex( NULL, 0, &func, var, 0, &id );
00433 if ( result == 0 ) return false;
00434 #else
00435 pthread_t result = 0;
00436 if( pthread_create( &result, 0, func, var ) != 0 ) return false;
00437 #endif
00438 thread = (unsigned)result;
00439 return true;
00440 }
00441
00442 bool thread_spawn( THREAD_START_ROUTINE func, void* var )
00443 { unsigned thread = 0;
00444 return thread_spawn( func, var, thread );
00445 }
00446
00447 void thread_join( unsigned thread )
00448 { QF_STACK_PUSH(thread_join)
00449
00450 #ifdef _MSC_VER
00451 WaitForSingleObject( ( void* ) thread, INFINITE );
00452 CloseHandle((HANDLE)thread);
00453 #else
00454 pthread_join( ( pthread_t ) thread, 0 );
00455 #endif
00456
00457 QF_STACK_POP
00458 }
00459
00460 void thread_detach( unsigned thread )
00461 { QF_STACK_PUSH(thread_detach)
00462
00463 #ifdef _MSC_VER
00464 CloseHandle((HANDLE)thread);
00465 #else
00466 pthread_t t = (pthread_t)thread;
00467 pthread_detach( t );
00468 #endif
00469
00470 QF_STACK_POP
00471 }
00472
00473 unsigned thread_self()
00474 {
00475 #ifdef _MSC_VER
00476 return ( unsigned ) GetCurrentThread();
00477 #else
00478 return ( unsigned ) pthread_self();
00479 #endif
00480 }
00481
00482 void process_sleep( double s )
00483 { QF_STACK_PUSH(process_sleep)
00484
00485 #ifdef _MSC_VER
00486 Sleep( (long)(s * 1000) );
00487 #else
00488 timespec time, remainder;
00489 double intpart;
00490 time.tv_nsec = (long)(modf(s, &intpart) * 1e9);
00491 time.tv_sec = (int)intpart;
00492 while( nanosleep(&time, &remainder) == -1 )
00493 time = remainder;
00494 #endif
00495
00496 QF_STACK_POP
00497 }
00498
00499 std::string file_separator()
00500 { QF_STACK_PUSH(file_separator)
00501
00502 #ifdef _MSC_VER
00503 return "\\";
00504 #else
00505 return "/";
00506 #endif
00507
00508 QF_STACK_POP
00509 }
00510
00511 void file_mkdir( const char* path )
00512 { QF_STACK_PUSH(file_mkdir)
00513
00514 #ifdef _MSC_VER
00515 _mkdir( path );
00516 #else
00517
00518 mkdir( path, 0777 );
00519 #endif
00520
00521 QF_STACK_POP
00522 }
00523
00524 FILE* file_fopen( const char* path, const char* mode )
00525 { QF_STACK_PUSH(file_fopen)
00526
00527 #if( _MSC_VER >= 1400 )
00528 FILE* result = 0;
00529 fopen_s( &result, path, mode );
00530 return result;
00531 #else
00532 return fopen( path, mode );
00533 #endif
00534
00535 QF_STACK_POP
00536 }
00537
00538 void file_unlink( const char* path )
00539 { QF_STACK_PUSH(file_unlink)
00540
00541 #ifdef _MSC_VER
00542 _unlink( path );
00543 #else
00544 unlink( path );
00545 #endif
00546
00547 QF_STACK_POP
00548 }
00549
00550 std::string file_appendpath( const std::string& path, const std::string& file )
00551 { QF_STACK_PUSH(file_appendpath)
00552
00553 const char last = path[path.size()-1];
00554 if( last == '/' || last == '\\' )
00555 return std::string(path) + file;
00556 else
00557 return std::string(path) + file_separator() + file;
00558
00559 QF_STACK_POP
00560 }
00561 }