📄 ilibasyncserversocket.c
字号:
if(FD_ISSET(module->ListenSocket,readset)!=0) { // // There are pending TCP connection requests // for(i=0;i<module->MaxConnection;++i) { // // Check to see if we have available resources to handle this connection request // if(ILibAsyncSocket_IsFree(module->AsyncSockets[i])!=0) { addrlen = sizeof(addr); NewSocket = accept(module->ListenSocket,(struct sockaddr*)&addr,&addrlen); if(NewSocket!= ~0) { switch(module->scope) { case ILibServerScope_LocalLoopback: // Check that the caller ip address is the same as the receive IP address getsockname(NewSocket,(struct sockaddr*)&receivingAddress,&receivingAddressLength); if(receivingAddress.sin_addr.s_addr!=addr.sin_addr.s_addr) { #if defined(WIN32) || defined(_WIN32_WCE) closesocket(NewSocket); #else close(NewSocket); #endif NewSocket=~0; } break; case ILibServerScope_LocalSegment: getsockname(NewSocket,(struct sockaddr*)&receivingAddress,&receivingAddressLength); break; default: break; } } if (NewSocket != ~0) { // // Set this new socket to non-blocking mode, so we can play nice and share thread // #ifdef _WIN32_WCE flags = 1; ioctlsocket(NewSocket,FIONBIO,&flags); #elif defined(WIN32) flags = 1; ioctlsocket(NewSocket,FIONBIO,&flags); #elif defined(_POSIX) flags = fcntl(NewSocket,F_GETFL,0); fcntl(NewSocket,F_SETFL,O_NONBLOCK|flags); #endif // // Instantiate a module to contain all the data about this connection // data = (struct AsyncServerSocket_Data*)malloc(sizeof(struct AsyncServerSocket_Data)); memset(data,0,sizeof(struct AsyncServerSocket_Data)); data->module = socketModule; ILibAsyncSocket_UseThisSocket(module->AsyncSockets[i],&NewSocket,&ILibAsyncServerSocket_OnInterrupt,data); ILibAsyncSocket_SetRemoteAddress(module->AsyncSockets[i],addr.sin_addr.s_addr); // // Notify the user about this new connection // if(module->OnConnect!=NULL) { module->OnConnect(module,module->AsyncSockets[i],&(data->user)); } } else {break;} } } }}/// <summary>/// Chain Destroy handler/// </summary>/// <param name="socketModule"></param>static void ILibAsyncServerSocket_Destroy(void *socketModule){ struct AsyncServerSocketModule *module =(struct AsyncServerSocketModule*)socketModule; free(module->AsyncSockets); #ifdef _WIN32_WCE closesocket(module->ListenSocket); #elif defined(WIN32) closesocket(module->ListenSocket); #elif defined(_POSIX) close(module->ListenSocket); #endif }/// <summary>/// Internal method dispatched by the OnData event of the underlying ILibAsyncSocket/// </summary>/// <param name="socketModule"></param>/// <param name="buffer"></param>/// <param name="p_beginPointer"></param>/// <param name="endPointer"></param>/// <param name="OnInterrupt"></param>/// <param name="user"></param>/// <param name="PAUSE"></param>static void ILibAsyncServerSocket_OnData(void* socketModule,char* buffer,int *p_beginPointer, int endPointer,void (**OnInterrupt)(void *AsyncSocketMoudle, void *user),void **user, int *PAUSE){ struct AsyncServerSocket_Data *data = (struct AsyncServerSocket_Data*)(*user); int bpointer = *p_beginPointer; // // Pass the received data up // if(data->module->OnReceive!=NULL) { data->module->OnReceive(data->module,socketModule,buffer,&bpointer,endPointer,&(data->module->OnInterrupt),&(data->user),PAUSE); *p_beginPointer = bpointer; }}/// <summary>/// Internal method dispatched by the OnDisconnect event of the underlying ILibAsyncSocket/// </summary>/// <param name="socketModule"></param>/// <param name="user"></param>static void ILibAsyncServerSocket_OnDisconnect(void* socketModule, void *user){ struct AsyncServerSocket_Data *data = (struct AsyncServerSocket_Data*)user; // // Pass this Disconnect event up // if(data->module->OnDisconnect!=NULL) { data->module->OnDisconnect(data->module,socketModule,data->user); } // // If the chain is shutting down, we need to free some resources // if(ILibIsChainBeingDestroyed(data->module->Chain)==0) { free(data); }}/// <summary>/// Internal method dispatched by the OnSendOK event of the underlying ILibAsyncSocket/// </summary>/// <param name="socketModule"></param>/// <param name="user"></param>static void ILibAsyncServerSocket_OnSendOK(void* socketModule, void *user){ struct AsyncServerSocket_Data *data = (struct AsyncServerSocket_Data*)user; // // Pass the OnSendOK event up // if(data->module->OnSendOK!=NULL) { data->module->OnSendOK(data->module,socketModule,data->user); }}/// <summary>/// Internal method dispatched by ILibAsyncSocket, to signal that the buffers have been reallocated/// </summary>/// <param name="ConnectionToken">The ILibAsyncSocket sender</param>/// <param name="user">The AsyncServerSocket_Data object</param>/// <param name="offSet">The offset to the new buffer location</param>static void ILibAsyncServerSocket_OnBufferReAllocated(void *ConnectionToken, void *user, ptrdiff_t offSet){ struct AsyncServerSocket_Data *data = (struct AsyncServerSocket_Data*)user; if(data->Callback!=NULL) { // // If someone above us, has registered for this callback, we need to fire it, // with the correct user object // data->Callback(data->module,ConnectionToken,data->user,offSet); }}/// <summary>/// Internal method dispatched by the OnSendOK event of the underlying ILibAsyncSocket/// </summary>/// <param name="Chain">The chain to add this module to</param>/// <param name="MaxConnections">The max number of simultaneous connections that will be allowed</param>/// <param name="PortNumber">The port number to bind to. 0 will select a random port</param>/// <param name="initialBufferSize">The initial size of the receive buffer</param>/// <param name="OnConnect">Function Pointer that triggers when a connection is established</param>/// <param name="OnDisconnect">Function Pointer that triggers when a connection is closed</param>/// <param name="OnReceive">Function Pointer that triggers when data is received</param>/// <param name="OnInterrupt">Function Pointer that triggers when connection interrupted</param>/// <param name="OnSendOK">Function Pointer that triggers when pending sends are complete</param>/// <returns>An ILibAsyncServerSocket module</returns>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; // // Instantiate a new AsyncServer module // RetVal = (struct AsyncServerSocketModule*)malloc(sizeof(struct AsyncServerSocketModule)); memset(RetVal,0,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; // // Create our socket pool // for(i=0;i<MaxConnections;++i) { RetVal->AsyncSockets[i] = ILibCreateAsyncSocketModule(Chain,initialBufferSize,&ILibAsyncServerSocket_OnData,NULL,&ILibAsyncServerSocket_OnDisconnect, &ILibAsyncServerSocket_OnSendOK); // // 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 // #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);}/// <summary>/// Returns the port number the server is bound to/// </summary>/// <param name="ServerSocketModule">The ILibAsyncServer to query</param>/// <returns>The listening port number</returns>unsigned short ILibAsyncServerSocket_GetPortNumber(void *ServerSocketModule){ struct AsyncServerSocketModule *ASSM = (struct AsyncServerSocketModule*)ServerSocketModule; return(ASSM->portNumber);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -