📄 ilibasyncserversocket.c
字号:
/* * INTEL CONFIDENTIAL * Copyright (c) 2002, 2003 Intel Corporation. All rights reserved. * * The source code contained or described herein and all documents * related to the source code ("Material") are owned by Intel * Corporation or its suppliers or licensors. Title to the * Material remains with Intel Corporation or its suppliers and * licensors. The Material contains trade secrets and proprietary * and confidential information of Intel or its suppliers and * licensors. The Material is protected by worldwide copyright and * trade secret laws and treaty provisions. No part of the Material * may be used, copied, reproduced, modified, published, uploaded, * posted, transmitted, distributed, or disclosed in any way without * Intel's prior express written permission. * No license under any patent, copyright, trade secret or other * intellectual property right is granted to or conferred upon you * by disclosure or delivery of the Materials, either expressly, by * implication, inducement, estoppel or otherwise. Any license * under such intellectual property rights must be express and * approved by Intel in writing. * * $Workfile: ILibAsyncServerSocket.c * $Revision: #1.0.1799.42459 * $Author: Intel Corporation, Intel Device Builder * $Date: Wednesday, January 19, 2005 * * * */#if defined(_WIN32_WCE) #define _CRTDBG_MAP_ALLOC #include <math.h> #include <winerror.h> #include <stdlib.h> #include <stdio.h> #include <stddef.h> #include <string.h> #include <winsock.h> #include <wininet.h> #include <windows.h> #include <winioctl.h> #include <winbase.h>#elif defined(WIN32) #define _CRTDBG_MAP_ALLOC #include <math.h> #include <winerror.h> #include <stdlib.h> #include <stdio.h> #include <stddef.h> #include <string.h> #ifdef WINSOCK2 #include <winsock2.h> #include <ws2tcpip.h> #else #include <winsock.h> #include <wininet.h> #endif #include <windows.h> #include <winioctl.h> #include <winbase.h> #include <crtdbg.h>#elif defined(_POSIX) #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/time.h> #include <netdb.h> #include <string.h> #include <sys/ioctl.h> #include <net/if.h> #include <sys/utsname.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> #include <fcntl.h> #include <errno.h>#endif#include "ILibAsyncServerSocket.h"#include "ILibAsyncSocket.h"#include "ILibParsers.h"#if defined(WIN32) && !defined(_WIN32_WCE) #include <errno.h>#elif defined(_POSIX) #include <errno.h> #include <semaphore.h>#endif#define DEBUGSTATEMENT(x)struct AsyncServerSocketModule{ void (*PreSelect)(void* object,fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime); void (*PostSelect)(void* object,int slct, fd_set *readset, fd_set *writeset, fd_set *errorset); void (*Destroy)(void* object); void *Chain; int MaxConnection; void **AsyncSockets; ILibServerScope scope; #ifdef _WIN32_WCE SOCKET ListenSocket; #elif defined(WIN32) SOCKET ListenSocket; #elif defined(_POSIX) int ListenSocket; #endif unsigned short portNumber; int listening; 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 (*OnConnect)(void *AsyncServerSocketModule, void *ConnectionToken,void **user); void (*OnDisconnect)(void *AsyncServerSocketModule, void *ConnectionToken, void *user); void (*OnInterrupt)(void *AsyncServerSocketModule, void *ConnectionToken, void *user); void (*OnSendOK)(void *AsyncServerSocketModule, void *ConnectionToken, void *user); void *Tag;};struct AsyncServerSocket_Data{ struct AsyncServerSocketModule *module; ILibAsyncServerSocket_BufferReAllocated Callback; void *user;};/// <summary>/// Returns the user Tag associated with the AsyncServer/// </summary>/// <param name="AsyncSocketModule">The ILibAsyncServerSocket to query</param>/// <returns>The user Tag</returns>void *ILibAsyncServerSocket_GetTag(void *AsyncSocketModule){ struct AsyncServerSocketModule *module = (struct AsyncServerSocketModule*)AsyncSocketModule; return(module->Tag);}/// <summary>/// Sets the user Tag associated with the AsyncServer/// </summary>/// <param name="AsyncSocketModule">The ILibAsyncServerSocket to save the tag to</param>void ILibAsyncServerSocket_SetTag(void *AsyncSocketModule, void *tag){ struct AsyncServerSocketModule *module = (struct AsyncServerSocketModule*)AsyncSocketModule; module->Tag = tag;}/// <summary>/// Internal method called by ILibAsyncSocket, to signal an interrupt condition/// </summary>/// <param name="socketModule">The ILibAsyncServerSocket that was interrupted</param>/// <param name="user">The associated user tag</param>static void ILibAsyncServerSocket_OnInterrupt(void *socketModule, void *user){ struct AsyncServerSocket_Data *data = (struct AsyncServerSocket_Data*)user; if(data->module->OnInterrupt!=NULL) { data->module->OnInterrupt(data->module,socketModule,data->user); } free(user);}/// <summary>/// Chain PreSelect handler/// </summary>/// <param name="socketModule"></param>/// <param name="readset"></param>/// <param name="writeset"></param>/// <param name="errorset"></param>/// <param name="blocktime"></param>static void ILibAsyncServerSocket_PreSelect(void* socketModule,fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime){ struct AsyncServerSocketModule *module = (struct AsyncServerSocketModule*)socketModule; int flags,i; // // The socket isn't put in listening mode, until the chain is started. // If this variable==0, that means we need to do that. // if(module->listening==0) { // // Set the socket to non-block mode, so we can play nice and share the thread // #ifdef _WIN32_WCE flags = 1; ioctlsocket(module->ListenSocket,FIONBIO,&flags); #elif defined(WIN32) flags = 1; ioctlsocket(module->ListenSocket,FIONBIO,&flags); #elif defined(_POSIX) flags = fcntl(module->ListenSocket,F_GETFL,0); fcntl(module->ListenSocket,F_SETFL,O_NONBLOCK|flags); #endif // // Put the socket in Listen, and add it to the fdset for the Select loop // module->listening=1; listen(module->ListenSocket,4); FD_SET(module->ListenSocket,readset); } else { // Only put the ListenSocket in the readset, if we are able to handle a new socket for(i=0;i<module->MaxConnection;++i) { if(ILibAsyncSocket_IsFree(module->AsyncSockets[i])!=0) { FD_SET(module->ListenSocket,readset); break; } } }}void ILibAsyncServerSocket_SetReAllocateNotificationCallback(void *AsyncServerSocketToken, void *ConnectionToken, ILibAsyncServerSocket_BufferReAllocated Callback){ struct AsyncServerSocket_Data *data = (struct AsyncServerSocket_Data*)ILibAsyncSocket_GetUser(ConnectionToken); data->Callback = Callback;}/// <summary>/// Chain PostSelect handler/// </summary>/// <param name="socketModule"></param>/// <param name="slct"></param>/// <param name="readset"></param>/// <param name="writeset"></param>/// <param name="errorset"></param>static void ILibAsyncServerSocket_PostSelect(void* socketModule,int slct, fd_set *readset, fd_set *writeset, fd_set *errorset){ struct AsyncServerSocket_Data *data; struct sockaddr_in addr; unsigned int addrlen; struct AsyncServerSocketModule *module = (struct AsyncServerSocketModule*)socketModule; int i,flags; struct sockaddr_in receivingAddress; unsigned int receivingAddressLength = sizeof(struct sockaddr_in); #ifdef _WIN32_WCE SOCKET NewSocket; #elif defined(WIN32) SOCKET NewSocket; #elif defined(_POSIX) int NewSocket; #endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -