Ejemplo n.º 1
0
void ILibAsyncSocket_ConnectTo(void* socketModule, int localInterface, int remoteInterface, int remotePortNumber, void (*InterruptPtr)(void *socketModule, void *user),void *user)
{
	int flags;
	struct sockaddr_in addr;
	struct AsyncSocketModule *module = (struct AsyncSocketModule*)socketModule;
	
	module->PendingBytesToSend = 0;
	module->TotalBytesSent = 0;
	module->IsFree = 0;
	module->PAUSE = 0;
	module->user = user;
	module->OnInterrupt = InterruptPtr;
	module->buffer = (char*)realloc(module->buffer,module->InitialSize);
	module->MallocSize = module->InitialSize;
	memset((char *)&addr, 0,sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = remoteInterface;

	#ifdef _WIN32_WCE
		addr.sin_port = htons((unsigned short)remotePortNumber);
	#elif WIN32
		addr.sin_port = htons(remotePortNumber);
	#elif _POSIX
		addr.sin_port = htons(remotePortNumber);
	#endif
	
	if(module->internalSocket==-1)
	{
		#ifdef WINSOCK2
			ILibGetStreamSocket(localInterface,0,(HANDLE*)&(module->internalSocket));
		#else
			ILibGetStreamSocket(localInterface,0,&(module->internalSocket));
		#endif
	}
	
	module->FinConnect = 0;
	module->BeginPointer = 0;
	module->EndPointer = 0;
	
	#ifdef _WIN32_WCE
		flags = 1;
		ioctlsocket(module->internalSocket,FIONBIO,&flags);
	#elif WIN32
		flags = 1;
		ioctlsocket(module->internalSocket,FIONBIO,&flags);
	#elif _POSIX
		flags = fcntl(module->internalSocket,F_GETFL,0);
		fcntl(module->internalSocket,F_SETFL,O_NONBLOCK|flags);
	#endif

	connect(module->internalSocket,(struct sockaddr*)&addr,sizeof(addr));
	ILibForceUnBlockChain(module->Chain);
}
void ILibMiniWebServerModule_PreSelect(void *WebServerModule,fd_set *readset, fd_set *writeset, fd_set *errorset,int *blocktime)
{
	int i;
	struct MiniWebServerObject *module = (struct MiniWebServerObject*)WebServerModule;
	int NumFree = module->MaxConnections;
	
	if(module->PortNumber==0)
	{
		module->PortNumber = ILibGetStreamSocket(htonl(INADDR_ANY),&(module->ListenSocket));
		listen(module->ListenSocket,4);
	}
	
	/* Pre Select Connected Sockets*/
	for(i=0;i<module->MaxConnections;++i)
	{
		if(module->Readers[i].ClientSocket!=0xFFFFFFFF)
		{
			/* Already Connected, just needs reading */
			FD_SET(module->Readers[i].ClientSocket,readset);
			FD_SET(module->Readers[i].ClientSocket,errorset);
			--NumFree;
		}
	}
	
	if(NumFree!=0)
	{
		/* Pre Select Listen Socket */
		FD_SET(module->ListenSocket,readset);
	}
	else
	{
		if(*blocktime>1){*blocktime=1;}
	}
}
Ejemplo n.º 3
0
ILibAsyncServerSocket_ServerModule ILibCreateAsyncServerSocketModule(void *Chain, int MaxConnections, int PortNumber, int initialBufferSize, ILibAsyncServerSocket_OnConnect OnConnect,ILibAsyncServerSocket_OnDisconnect OnDisconnect,ILibAsyncServerSocket_OnReceive OnReceive,ILibAsyncServerSocket_OnInterrupt OnInterrupt, ILibAsyncServerSocket_OnSendOK OnSendOK)
{
    struct ILibAsyncServerSocketModule * RetVal;
    int i;

    // Instantiate a new AsyncServer module
    RetVal = (struct ILibAsyncServerSocketModule*)malloc(sizeof(struct ILibAsyncServerSocketModule));
    memset(RetVal,0,sizeof(struct ILibAsyncServerSocketModule));
    RetVal->PreSelect       = &ILibAsyncServerSocket_PreSelect;
    RetVal->PostSelect      = &ILibAsyncServerSocket_PostSelect;
    RetVal->Destroy         = &ILibAsyncServerSocket_Destroy;
    RetVal->Chain           = Chain;
    RetVal->OnConnect       = OnConnect;
    RetVal->OnDisconnect    = OnDisconnect;
    RetVal->OnInterrupt     = OnInterrupt;
    RetVal->OnSendOK        = OnSendOK;
    RetVal->OnReceive       = OnReceive;
    RetVal->MaxConnection   = MaxConnections;
    RetVal->AsyncSockets    = (void**)malloc(MaxConnections * sizeof(void*));
    RetVal->portNumber      = PortNumber;

    // Create our socket pool
    for (i = 0; i < MaxConnections; ++i) {
        RetVal->AsyncSockets[i] = ILibCreateAsyncSocketModule(Chain, initialBufferSize, &ILibAsyncServerSocket_OnData, NULL, &ILibAsyncServerSocket_OnDisconnectSink, &ILibAsyncServerSocket_OnSendOKSink);

        // We want to know about any buffer reallocations, because anything above us may want to know
        ILibAsyncSocket_SetReAllocateNotificationCallback(RetVal->AsyncSockets[i], &ILibAsyncServerSocket_OnBufferReAllocated);
    }
    ILibAddToChain(Chain,RetVal);

    // Get our listening socket
#if defined(WIN32) || defined(_WIN32_WCE)
    RetVal->portNumber = ILibGetStreamSocket(htonl(INADDR_ANY),RetVal->portNumber,(HANDLE*)&(RetVal->ListenSocket));
#else
    RetVal->portNumber = ILibGetStreamSocket(htonl(INADDR_ANY),RetVal->portNumber,&(RetVal->ListenSocket));
#endif

    return(RetVal);
}
void *ILibCreateAsyncServerSocketModule(void *Chain, int MaxConnections, int PortNumber, int initialBufferSize, void (*OnConnect)(void *AsyncServerSocketModule, void *ConnectionToken,void **user),void (*OnDisconnect)(void *AsyncServerSocketModule, void *ConnectionToken, void *user),void (*OnReceive)(void *AsyncServerSocketModule, void *ConnectionToken,char* buffer,int *p_beginPointer, int endPointer,void (**OnInterrupt)(void *AsyncServerSocketMoudle, void *ConnectionToken, void *user), void **user, int *PAUSE),void (*OnInterrupt)(void *AsyncServerSocketModule, void *ConnectionToken, void *user), void (*OnSendOK)(void *AsyncServerSocketModule, void *ConnectionToken, void *user))
{
	struct AsyncServerSocketModule *RetVal;
	int i;

	RetVal = (struct AsyncServerSocketModule*)MALLOC(sizeof(struct AsyncServerSocketModule));
	RetVal->PreSelect = &ILibAsyncServerSocket_PreSelect;
	RetVal->PostSelect = &ILibAsyncServerSocket_PostSelect;
	RetVal->Destroy = &ILibAsyncServerSocket_Destroy;
	RetVal->Chain = Chain;
	RetVal->OnConnect = OnConnect;
	RetVal->OnDisconnect = OnDisconnect;
	RetVal->OnInterrupt = OnInterrupt;
	RetVal->OnSendOK = OnSendOK;
	RetVal->OnReceive = OnReceive;
	RetVal->MaxConnection = MaxConnections;
	RetVal->AsyncSockets = (void**)MALLOC(MaxConnections*sizeof(void*));
	RetVal->portNumber = PortNumber;
	RetVal->Tag = NULL;
	RetVal->listening = 0;
	
	for(i=0;i<MaxConnections;++i)
	{
		RetVal->AsyncSockets[i] = ILibCreateAsyncSocketModule(Chain,initialBufferSize,&ILibAsyncServerSocket_OnData,NULL,&ILibAsyncServerSocket_OnDisconnect, &ILibAsyncServerSocket_OnSendOK);
		//RetVal->AsyncSockets[i] = ILibCreateAsyncSocketModule(Chain,initialBufferSize,&ILibAsyncServerSocket_OnData,NULL,&ILibAsyncServerSocket_OnDisconnect, &ILibAsyncServerSocket_OnSendOK);
	}
	ILibAddToChain(Chain,RetVal);

	#ifdef WINSOCK2
		RetVal->portNumber = ILibGetStreamSocket(htonl(INADDR_ANY),RetVal->portNumber,(HANDLE*)&(RetVal->ListenSocket));
	#else
		RetVal->portNumber = ILibGetStreamSocket(htonl(INADDR_ANY),RetVal->portNumber,&(RetVal->ListenSocket));
	#endif

	return(RetVal);
}