Ejemplo n.º 1
0
static inline bool connectInternal(int &fd, int domain, sockaddr* address, size_t addressSize, int maxTime)
{
    StopWatch timer;

    while (true) {
        fd = ::socket(domain, SOCK_STREAM, 0);
        if (fd == -1)
            return false;

        addFlags(fd, O_NONBLOCK);
        errno = 0;
        int ret;
        eintrwrap(ret, ::connect(fd, address, addressSize));
        if (!ret) {
#ifdef HAVE_NOSIGPIPE
            ret = 1;
            ::setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&ret, sizeof(int));
#endif
            return true;
        }

        int left = -1;
        if (maxTime >= 0) {
            left = maxTime - static_cast<int>(timer.elapsed());
            if (left <= 0)
                break;
        }

        if (errno == EINPROGRESS) {
            fd_set write;
            FD_ZERO(&write);
            FD_SET(fd, &write);
            timeval timeout;
            if (left != -1) {
                timeout.tv_sec = left / 1000;
                timeout.tv_usec = (left % 1000) * 1000;
            }
            eintrwrap(ret, select(fd + 1, 0, &write, 0, left == -1 ? 0 : &timeout));
            if (ret > 0 && FD_ISSET(fd, &write)) {
                int error;
                socklen_t len = sizeof(error);
                if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) && !error)
                    return true;
            }
            break;
        } else if (errno == EAGAIN) {
            eintrwrap(ret, ::close(fd));
            fd = -1;
            usleep(100000);
        } else {
            break;
        }
    }
    if (fd != -1) {
        int ret;
        eintrwrap(ret, ::close(fd));
        fd = -1;
    }
    return false;
}
Ejemplo n.º 2
0
static inline bool setupUdp(int& fd)
{
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd != -1)
        addFlags(fd, O_NONBLOCK);
    return fd != -1;
}
Ejemplo n.º 3
0
//-------------------------------------------------------------------------------------
void EntityCoordinateNode::update()
{
	// 在这里做一下更新的原因是,很可能在CoordinateNode::update()的过程中导致实体位置被移动
	// 而导致次数update被调用,在某种情况下会出现问题
	// 例如:// A->B, B-A(此时old_*是B), A->B(此时old_*是B,而xx等目的地就是B),此时update中会误判为没有移动。
	// https://github.com/kbengine/kbengine/issues/407
	old_xx(x());
	old_yy(y());
	old_zz(z());

	CoordinateNode::update();

	addFlags(COORDINATE_NODE_FLAG_ENTITY_NODE_UPDATING);
	++entityNodeUpdating_;

	// 此处必须使用watcherNodes_.size()而不能使用迭代器遍历,防止在update中导致增加了watcherNodes_数量而破坏迭代器
	for (std::vector<CoordinateNode*>::size_type i = 0; i < watcherNodes_.size(); ++i)
	{
		CoordinateNode* pCoordinateNode = watcherNodes_[i];
		if (!pCoordinateNode)
			continue;

		pCoordinateNode->update();
	}

	--entityNodeUpdating_;
	if (entityNodeUpdating_ == 0)
		removeFlags(COORDINATE_NODE_FLAG_ENTITY_NODE_UPDATING);

	clearDelWatcherNodes();
}
Ejemplo n.º 4
0
static int private_link_archive( const void* buildParameters, const void* providerContext, const void* targetIDirectory )
{
    int status = 1;

    const BuildParameters* parameters = (BuildParameters*) buildParameters;
    const ProviderContext* context = (ProviderContext*) providerContext;
    const IDirectory*      target = (IDirectory*) targetIDirectory;

    const char* target_location = Path_getCondensed( Directory_getRealPath( target ) );

    //	Linux: ar rcs <LIBNAME> <OBJECTS>...
    //	Win32: lib /OUT:<LIBNAME> <OBJECTS>...

    char* name = CharString_between( context->package_name, "", "-" );
    if ( !name )
    {
        name = new_CharString( context->package_name );
    }
    {
        char* libname           = CharString_cat2( name, ".a" );
        char* full_lib_location = CharString_cat3( target_location, "/lib/", libname );
        char* obj_dir           = CharString_cat2( target_location, "/obj/" );

        if ( 0 < List_count( context->objectFiles ) )
        {
            Command* command;
            IList* arguments = new_List();
            IList* native_arguments;

            List_copyItem( arguments, context->archiver );
            List_copyItem( arguments, "rcs" );
            List_copyItem( arguments, full_lib_location );
            addFlags( arguments, obj_dir, context->objectFiles );


            native_arguments = Path_ConvertListToNative( arguments );
            command = new_Command( context->archiver, (const char**) native_arguments->items );
            Command_print( command, stderr );

//			if ( !BuildParameters_isNo( parameters ) && Command_run( command ) && Command_waitFor( command ) )
//			{
//				status &= Command_getResult( command );
//			} else {
//				fprintf( stderr, "failed: " );
//				Command_print( command, stderr );
//			}
            free_Command( command );
            free_List( native_arguments );
            free_List( arguments );
        }

        free_CharString( obj_dir );
        free_CharString( full_lib_location );
        free_CharString( libname );
    }
    free_CharString( name );

    return status;
}
Ejemplo n.º 5
0
SocketClient::SocketClient(Mode mode, int fd)
    : mMode(mode), mFd(fd), mBufferIdx(0), mReadBufferPos(0)
{
    pthread_once(&sigPipeHandler, initSigPipe);
    addFlags(mFd, O_NONBLOCK);
#ifdef HAVE_NOSIGPIPE
    int flags = 1;
    ::setsockopt(mFd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&flags, sizeof(int));
#endif
    EventLoop::instance()->addFileDescriptor(mFd, EventLoop::Read, dataCallback, this);
}
Ejemplo n.º 6
0
//-------------------------------------------------------------------------------------
void EntityCoordinateNode::update()
{
	CoordinateNode::update();

	addFlags(COORDINATE_NODE_FLAG_ENTITY_NODE_UPDATING);
	++entityNodeUpdating_;

	// 此处必须使用watcherNodes_.size()而不能使用迭代器遍历,防止在update中导致增加了watcherNodes_数量而破坏迭代器
	for (std::vector<CoordinateNode*>::size_type i = 0; i < watcherNodes_.size(); ++i)
	{
		CoordinateNode* pCoordinateNode = watcherNodes_[i];
		if (!pCoordinateNode)
			continue;

		pCoordinateNode->update();
	}

	--entityNodeUpdating_;
	if (entityNodeUpdating_ == 0)
		removeFlags(COORDINATE_NODE_FLAG_ENTITY_NODE_UPDATING);

	clearDelWatcherNodes();
}
Ejemplo n.º 7
0
static int private_compile( const void* buildParameters, const void* providerContext, const void* targetIDirectory )
{
    bool status = 1;
    const BuildParameters* parameters = (BuildParameters*) buildParameters;
    const ProviderContext* context = (ProviderContext*) providerContext;
    const IDirectory*      target = (IDirectory*) targetIDirectory;

    //IList* cppflags = translateArguments( context->FULL_CPPFLAGS );

    unsigned int i;
    unsigned int count = List_count( context->sourceFiles );
//	fprintf( stdout, "ISLabs::linux-gnu::gnu::compile( %s )\n", Path_getCondensed( Directory_getRealPath( target ) ) );
    for ( i = 0; (i < count) && status; i++ )
    {
        //	cc $CPPFLAGS $LANGFLAGS $CFLAGS $SRC_FILE -o $OUTPUT

        IPath* p = new_Path( context->sourceFiles->items[i] );
        char* target_file = CharString_cat2( Path_getBasename( p ), ".obj" );
        char* target_path = CharString_cat3( Path_getCondensed( Directory_getRealPath( target ) ), "/obj/", target_file );

        Command* command;
        IList*   arguments = new_List();
        IList*   native_arguments;

        // Compiler
        List_copyItem( arguments, context->compiler );

        // C-preprocessor flags
        addFlags( arguments, "-I", context->includeDirs );
        List_addList( arguments, BuildParameters_getGlobal( parameters )->CPPFLAGS );

        // C flags
        List_copyItem( arguments, "-Wall" );
        List_copyItem( arguments, "-fPIC" );
        List_copyItem( arguments, "-c" );

        if ( BuildParameters_isRelease( parameters ) )
        {
            //List_copyItem( arguments, "-O2" );
        } else {
            List_copyItem( arguments, "-g" );
        }
        List_addList( arguments, BuildParameters_getGlobal( parameters )->CFLAGS );

        // Source files
        List_copyItem( arguments, context->sourceFiles->items[i] );

        // Output files
        List_copyItem( arguments, "-o" );
        List_copyItem( arguments, target_path );

        native_arguments = Path_ConvertListToNative( arguments );
        command = new_Command( context->compiler, (const char**) native_arguments->items );
        Command_print( command, stderr );
//		if ( !BuildParameters_isNo( parameters ) && Command_run( command ) && Command_waitFor( command ) )
//		{
//			status &= Command_getResult( command );
//		} else {
//			fprintf( stderr, "failed: " );
//			Command_print( command, stderr );
//		}
        free_Command( command );
        free_CharString( target_file );
        free_Path( p );
    }
    return status;
}
Ejemplo n.º 8
0
static int private_link_executable( const void* buildParameters, const void* providerContext, const void* targetIDirectory )
{
//	fprintf( stdout, "ISLabs::linux-gnu::gnu::link()\n" );
    bool status = 1;
    const BuildParameters* parameters = (BuildParameters*) buildParameters;
    const ProviderContext* context = (ProviderContext*) providerContext;
    const IDirectory*      target = (IDirectory*) targetIDirectory;

    SoName* so_name = generateSoName( context->package_name, context->isLib );
    char* target_location_obj       = CharString_cat2( Path_getCondensed( Directory_getRealPath( target ) ), "/obj/" );
    char* target_location_bin_short = CharString_cat3( Path_getCondensed( Directory_getRealPath( target ) ), "/bin/", so_name->short_name );

    Command* command;
    IList* arguments = new_List();
    IList* native_arguments;

    //  Linker
    List_copyItem( arguments, context->linker );

    //  Linker flags
    //if ( !BuildParameters_isRelease( parameters ) )
    //{
    //	List_copyItem( arguments, "-Wl,-G" );
    //}
    //List_copyItem( arguments, "-shared" );

    //  Object files: need to be before archive libraries so
    //                symbols can be found.
    addFlags( arguments, target_location_obj, context->objectFiles );

    //  System library directories
    //
    addFlags( arguments, "-L", context->systemLibraryDirs );

    if ( BuildParameters_isMonolithic( parameters ) )
    {
        addFlags( arguments, "", context->unix_archives );
        addFlags( arguments, "-l", context->system_libraries );
    } else {
        addFlags( arguments, "-L", context->libraryDirs );
        addFlags( arguments, "-l", context->libraries );
    }

    List_addList( arguments, BuildParameters_getGlobal( parameters )->LFLAGS );
    List_addList( arguments, BuildParameters_getGlobal( parameters )->LDFLAGS );

    List_copyItem( arguments, "-Wl,-R$ORIGIN" );
    List_copyItem( arguments, "-Wl,-R$ORIGIN/../lib" );

    if ( BuildParameters_isDeep( parameters ) )
    {
        addFlags( arguments, "-Wl,-R$ORIGIN/../", context->runtimeLibraryDirs );
    }


    //List_copyItem( arguments, "-Wl,-soname" );
    //List_addItem( arguments, CharString_cat2( "-Wl,", so_name->long_name ) );

    List_copyItem( arguments, "-o" );
    List_copyItem( arguments, target_location_bin_short );

    native_arguments = Path_ConvertListToNative( arguments );
    command = new_Command( context->linker, (const char**) native_arguments->items );
    Command_print( command, stderr );

//	if ( BuildParameters_getVerbosity( parameters ) )
//	{
//		Command_print( command, stderr );
//	}
//
//	if ( !BuildParameters_isNo( parameters ) && Command_run( command ) && Command_waitFor( command ) )
//	{
//		status &= Command_getResult( command );
//	} else {
//		fprintf( stderr, "failed: " );
//		Command_print( command, stderr );
//	}

    free_Command( command );
    free_List( native_arguments );

    free_List( arguments );
    free_CharString( target_location_obj );
    free_CharString( target_location_bin_short );

    return status;
}