本文整理汇总了C++中CreateIoCompletionPort函数的典型用法代码示例。如果您正苦于以下问题:C++ CreateIoCompletionPort函数的具体用法?C++ CreateIoCompletionPort怎么用?C++ CreateIoCompletionPort使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CreateIoCompletionPort函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CreateIoCompletionPort
int IOCP::Create(void)
{
int iHandle;
HANDLE hnd;
hnd = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if ( hnd == NULL )
{
return -1;
}
iHandle = UniqueCount :: GetUniqueCount();
iocp.insert( MapWordToPtr::value_type( iHandle, hnd ) );
return iHandle;
}
示例2: uv_set_pipe_handle
static int uv_set_pipe_handle(uv_pipe_t* handle, HANDLE pipeHandle) {
DWORD mode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT;
if (!SetNamedPipeHandleState(pipeHandle, &mode, NULL, NULL)) {
return -1;
}
if (CreateIoCompletionPort(pipeHandle,
LOOP->iocp,
(ULONG_PTR)handle,
0) == NULL) {
return -1;
}
return 0;
}
示例3: GetSystemInfo
bool IOCP_Manager::Initialize()
{
SYSTEM_INFO SystemInfo;
GetSystemInfo(&SystemInfo);
mIoThreadCount = SystemInfo.dwNumberOfProcessors * 2;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf_s("WSAStartup Error\n");
return false;
}
mCompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, mIoThreadCount);
if (mCompletionPort == NULL)
{
printf_s("Create CompletionPort error\n");
return false;
}
mListenSocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, NULL, WSA_FLAG_OVERLAPPED);
if (mListenSocket == INVALID_SOCKET)
{
printf_s("Create ListenSocket Error\n");
return false;
}
int opt = 1;
setsockopt(mListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(int));
SOCKADDR_IN ServerAddr;
int SizeServerAddr = sizeof(SOCKADDR_IN);
memset(&ServerAddr, 0, SizeServerAddr);
if (WSAStringToAddress(L"127.0.0.1:9001", AF_INET, NULL, (SOCKADDR*)&ServerAddr, &SizeServerAddr) == SOCKET_ERROR)
{
printf_s("ServerAddress Setting Error\n");
return false;
}
if (bind(mListenSocket, (SOCKADDR*)&ServerAddr, SizeServerAddr) == SOCKET_ERROR)
{
printf_s("Bind Error\n");
return false;
}
return true;
}
示例4: run
int SP_IocpLFServer :: run()
{
int ret = 0;
int listenFD = -1;
ret = SP_IOUtils::tcpListen( mBindIP, mPort, &listenFD, 0 );
if( 0 == ret ) {
mCompletionHandler = mAcceptArg->mHandlerFactory->createCompletionHandler();
SP_IocpMsgQueue * msgQueue = new SP_IocpMsgQueue( mEventArg->getCompletionPort(),
SP_IocpEventCallback::eKeyMsgQueue, SP_IocpEventCallback::onResponse, mEventArg );
mEventArg->setResponseQueue( msgQueue );
mEventArg->loadDisconnectEx( listenFD );
mAcceptArg->mAcceptEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
mAcceptArg->mListenSocket = (HANDLE)listenFD;
if( NULL == CreateIoCompletionPort( mAcceptArg->mListenSocket,
mEventArg->getCompletionPort(), SP_IocpEventCallback::eKeyAccept, 0 ) ) {
sp_syslog( LOG_ERR, "CreateIoCompletionPort fail, errno %d", WSAGetLastError() );
return -1;
}
if( NULL == mAcceptArg->mIOChannelFactory ) {
mAcceptArg->mIOChannelFactory = new SP_DefaultIOChannelFactory();
}
sp_thread_t thread;
ret = sp_thread_create( &thread, NULL, acceptThread, mAcceptArg );
if( 0 == ret ) {
sp_syslog( LOG_NOTICE, "Thread #%ld has been created to accept socket", thread );
} else {
sp_syslog( LOG_WARNING, "Unable to create a thread to accept socket, %s", strerror( errno ) );
return -1;
}
mIsRunning = 1;
mThreadPool = new SP_ThreadPool( mMaxThreads );
for( int i = 0; i < mMaxThreads; i++ ) {
mThreadPool->dispatch( lfHandler, this );
}
}
return ret;
}
示例5: uv_tcp_set_socket
static int uv_tcp_set_socket(uv_loop_t* loop, uv_tcp_t* handle,
SOCKET socket, int imported) {
DWORD yes = 1;
assert(handle->socket == INVALID_SOCKET);
/* Set the socket to nonblocking mode */
if (ioctlsocket(socket, FIONBIO, &yes) == SOCKET_ERROR) {
uv__set_sys_error(loop, WSAGetLastError());
return -1;
}
/* Make the socket non-inheritable */
if (!SetHandleInformation((HANDLE)socket, HANDLE_FLAG_INHERIT, 0)) {
uv__set_sys_error(loop, GetLastError());
return -1;
}
/* Associate it with the I/O completion port. */
/* Use uv_handle_t pointer as completion key. */
if (CreateIoCompletionPort((HANDLE)socket,
loop->iocp,
(ULONG_PTR)socket,
0) == NULL) {
if (imported) {
handle->flags |= UV_HANDLE_EMULATE_IOCP;
} else {
uv__set_sys_error(loop, GetLastError());
return -1;
}
}
if (pSetFileCompletionNotificationModes) {
if (pSetFileCompletionNotificationModes((HANDLE) socket,
FILE_SKIP_SET_EVENT_ON_HANDLE |
FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)) {
handle->flags |= UV_HANDLE_SYNC_BYPASS_IOCP;
} else if (GetLastError() != ERROR_INVALID_FUNCTION) {
uv__set_sys_error(loop, GetLastError());
return -1;
}
}
handle->socket = socket;
return 0;
}
示例6: evq_add
EVQ_API int
evq_add (struct event_queue *evq, struct event *ev)
{
const unsigned int ev_flags = ev->flags;
struct win32thr *wth = &evq->head;
ev->wth = wth;
if (ev_flags & EVENT_SIGNAL)
return signal_add(evq, ev);
if (ev_flags & EVENT_WINMSG) {
evq->win_msg = ev;
evq->nevents++;
return 0;
}
if ((ev_flags & (EVENT_SOCKET | EVENT_SOCKET_ACC_CONN)) == EVENT_SOCKET
&& evq->iocp.h) {
if (!CreateIoCompletionPort((HANDLE) ev->fd, evq->iocp.h, 0, 0)
&& GetLastError() != ERROR_INVALID_PARAMETER) /* already assosiated */
return -1;
ev->flags |= EVENT_AIO;
evq->iocp.n++;
evq->nevents++;
if (pSetFileCompletionNotificationModes
&& pSetFileCompletionNotificationModes((HANDLE) ev->fd, 3))
ev->flags |= EVENT_AIO_SKIP;
win32iocp_set(ev, ev_flags);
return 0;
}
while (wth->n >= NEVENT - 1)
if (!(wth = wth->next)) break;
if (wth)
win32thr_sleep(wth);
else {
wth = win32thr_init(evq);
if (!wth) return -1;
}
return win32thr_add(wth, ev);
}
示例7: OutputDebugStr
CIOCPManager::CIOCPManager(int IOCPThreadCount /*= 0*/)
{
SOCKET TmpSock;
GUID guidAcceptEx = WSAID_ACCEPTEX;
GUID guidGetAcceptExSockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
DWORD dwBytes;
INT i;
OutputDebugStr(_T("IOCPManager::IOCPManager\n"));
// 使用 2.2版的WS2_32.DLL
if (WSAStartup(0x0202, &mwsaData) != 0) {
throw exception("WSAStartup Fails");
}
// 获取AcceptEx和GetAcceptExSockaddrs的函数指针
TmpSock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
if (TmpSock == INVALID_SOCKET) {
throw exception("WSASocket Fails");
}
if (SOCKET_ERROR == WSAIoctl(TmpSock, SIO_GET_EXTENSION_FUNCTION_POINTER,
&guidAcceptEx, sizeof(guidAcceptEx), &g_AcceptEx, sizeof(g_AcceptEx), &dwBytes, NULL, NULL)) {
throw exception("WSAIoctl WSAID_ACCEPTEX Fails");
}
if (SOCKET_ERROR == WSAIoctl(TmpSock, SIO_GET_EXTENSION_FUNCTION_POINTER,
&guidGetAcceptExSockaddrs, sizeof(guidGetAcceptExSockaddrs), &g_GetAcceptExSockaddrs, sizeof(g_GetAcceptExSockaddrs), &dwBytes, NULL, NULL)) {
throw exception("WSAIoctl WSAID_GETACCEPTEXSOCKADDRS Fails");
}
closesocket(TmpSock);
// 初始化临界区
InitializeCriticalSection(&mSockListCS);
InitializeCriticalSection(&mOverLappedListCS);
// 初始化IOCP完成端口
mCompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 0);
if (IOCPThreadCount <= 0) {
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
IOCPThreadCount = SysInfo.dwNumberOfProcessors + 2;
}
mIocpWorkThreadCount = IOCPThreadCount;
mIocpWorkThreads = new HANDLE[mIocpWorkThreadCount];
// 创建IOCP工作线程
for (i = 0; i < mIocpWorkThreadCount; i++) {
mIocpWorkThreads[i] = (HANDLE)_beginthreadex(NULL, 0, IocpWorkThread, (PVOID)mCompletionPort, 0, NULL);
if (mIocpWorkThreads[i] == NULL) {
throw exception("CreateThread FIocpWorkThreads Fails");
}
}
}
示例8: CreateIoCompletionPort
int iocp::iocp_open(int thread_count)
{
m_iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 0);
if(!m_iocp)
{
m_error = GetLastError();
printf("iocp initialize failed!\n");
return 0;
}
m_thread_count = thread_count;
for(int i = 0; i < m_thread_count; ++i)
{
QueueUserWorkItem(iocp_threaad_pro, this, WT_EXECUTELONGFUNCTION);
}
CreateSemaphore(NULL, 0, m_thread_count, NULL);
return 1;
}
示例9: mpk_import_add_named_pipe_instance
void mpk_import_add_named_pipe_instance(common_vars *common_vars) {
HANDLE named_pipe_handle = CreateNamedPipeA("\\\\.\\pipe\\" m_mpk_import_named_pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, nullptr);
if (!named_pipe_handle) {
m_last_error_str(err);
m_die("cannot create mpk import named pipe instance, call to \"CreateNamedPipeA\" failed\nerr: %s", err);
}
HANDLE io_completion_port = CreateIoCompletionPort(named_pipe_handle, common_vars->io_completion_port, mpk_import_named_pipe_event, 0);
if (!io_completion_port) {
m_last_error_str(err_str);
m_die("cannot connect mpk import named pipe to io completion port, call to \"CreateIoCompletionPort\" failed\nerr: %s", err_str);
}
mpk_import_named_pipe_instance *new_named_pipe_instance = (mpk_import_named_pipe_instance *)memory_pool_allocate(&common_vars->mpk_import_named_pipe_instance_memory_pool);
*new_named_pipe_instance = {};
new_named_pipe_instance->handle = named_pipe_handle;
sllist_push(&common_vars->mpk_import_named_pipe_instance_list, new_named_pipe_instance);
ConnectNamedPipe(named_pipe_handle, &new_named_pipe_instance->overlapped);
}
示例10: CRASH_ASSERT
bool ClientSession::PostConnect()
{
CRASH_ASSERT( LThreadType == THREAD_MAIN );
HANDLE handle = CreateIoCompletionPort( (HANDLE)mSocket, GIocpManager->GetComletionPort(), ( ULONG_PTR )this, 0 );
if ( handle != GIocpManager->GetComletionPort() )
{
printf_s( "[DEBUG] CreateIoCompletionPort error: %d\n", GetLastError() );
return false;
}
OverlappedConnectContext* connectContext = new OverlappedConnectContext( this );
DWORD bytes = 0;
connectContext->mWsaBuf.len = 0;
connectContext->mWsaBuf.buf = nullptr;
if ( SERVER_PORT <= 4000 || SERVER_PORT > 10000 )
{
SERVER_PORT = 9001;
}
if ( HOST_NAME.length() == 0 )
{
HOST_NAME = "127.0.0.1";
}
sockaddr_in addr;
ZeroMemory( &addr, sizeof( addr ) );
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr( HOST_NAME.c_str() );
addr.sin_port = htons( SERVER_PORT );
if ( FALSE == IocpManager::ConnectEx( mSocket, (SOCKADDR*)&addr, sizeof( addr ),
GIocpManager->mConnectBuf, 0, &bytes, (LPOVERLAPPED)connectContext ) )
{
if ( WSAGetLastError() != WSA_IO_PENDING )
{
DeleteIoContext( connectContext );
printf_s( "ConnectEx Error : %d\n", GetLastError() );
return false;
}
}
return true;
}
示例11: assert
VOID Acceptor::Init( IoHandler *pIoHandler, DWORD dwNumberOfThreads )
{
assert( dwNumberOfThreads <= MAX_ACCEPT_THREAD );
m_pIoHandler = pIoHandler;
m_numThreads = dwNumberOfThreads;
// Accept 创建IOCP
m_hIOCP = CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 0, 0 );
// 遍历, 创建线程
unsigned threadID;
for( DWORD i = 0; i < m_numThreads; ++i )
{
m_hThread[i] = (HANDLE)_beginthreadex( NULL, 0, accept_thread, (LPVOID)this, 0/*CREATE_SUSPENDED*/, &threadID );
}
}
示例12: tempLock
bool ClientSession::OnConnect(SOCKADDR_IN* addr)
{
//TODO: 이 영역 lock으로 보호 할 것
FastSpinlockGuard tempLock(mLock);
CRASH_ASSERT(LThreadType == THREAD_MAIN_ACCEPT);
/// make socket non-blocking
u_long arg = 1 ;
ioctlsocket(mSocket, FIONBIO, &arg) ;
/// turn off nagle
int opt = 1 ;
setsockopt(mSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&opt, sizeof(int)) ;
opt = 0;
if (SOCKET_ERROR == setsockopt(mSocket, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(int)) )
{
printf_s("[DEBUG] SO_RCVBUF change error: %d\n", GetLastError()) ;
return false;
}
HANDLE handle = CreateIoCompletionPort(
(HANDLE)mSocket,
GIocpManager->GetCompletionPort(),
(ULONG_PTR)this,
0
);
//TODO: 여기에서 CreateIoCompletionPort((HANDLE)mSocket, ...);사용하여 연결할 것
if (handle != GIocpManager->GetCompletionPort())
{
printf_s("[DEBUG] CreateIoCompletionPort error: %d\n", GetLastError());
return false;
}
memcpy(&mClientAddr, addr, sizeof(SOCKADDR_IN));
mConnected = true ;
printf_s("[DEBUG] Client Connected: IP=%s, PORT=%d\n", inet_ntoa(mClientAddr.sin_addr), ntohs(mClientAddr.sin_port));
GSessionManager->IncreaseConnectionCount();
return PostRecv() ;
}
示例13: event_iocp_port_launch
struct event_iocp_port *
event_iocp_port_launch(void)
{
struct event_iocp_port *port;
int i;
if (!extension_fns_initialized)
init_extension_functions(&the_extension_fns);
if (!(port = mm_calloc(1, sizeof(struct event_iocp_port))))
return NULL;
port->n_threads = 2;
port->threads = calloc(port->n_threads, sizeof(HANDLE));
if (!port->threads)
goto err;
port->port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, port->n_threads);
port->ms = -1;
if (!port->port)
goto err;
port->shutdownSemaphore = CreateSemaphore(NULL, 0, 1, NULL);
if (!port->shutdownSemaphore)
goto err;
for (i=0; i<port->n_threads; ++i) {
uintptr_t th = _beginthread(loop, 0, port);
if (th == (uintptr_t)-1)
goto err;
port->threads[i] = (HANDLE)th;
++port->n_live_threads;
}
InitializeCriticalSection(&port->lock);
return port;
err:
if (port->port)
CloseHandle(port->port);
if (port->threads)
mm_free(port->threads);
if (port->shutdownSemaphore)
CloseHandle(port->shutdownSemaphore);
mm_free(port);
return NULL;
}
示例14: AssociateCompletePort
CConnection* CNetManager::AssociateCompletePort( SOCKET hSocket )
{
CConnection *pConnection = CConnectionMgr::GetInstancePtr()->CreateConnection();
pConnection->SetSocket(hSocket);
pConnection->SetDataHandler(m_pBufferHandler);
if(NULL == CreateIoCompletionPort((HANDLE)hSocket, m_hCompletePort, (ULONG_PTR)pConnection, 0))
{
pConnection->Close(FALSE);
return NULL;
}
return pConnection;
}
示例15: CreateIoCompletionPort
NS_IMETHODIMP
NamedPipeService::AddDataObserver(void* aHandle,
nsINamedPipeDataObserver* aObserver)
{
if (!aHandle || aHandle == INVALID_HANDLE_VALUE || !aObserver) {
return NS_ERROR_ILLEGAL_VALUE;
}
nsresult rv;
HANDLE h = CreateIoCompletionPort(aHandle,
mIocp,
reinterpret_cast<ULONG_PTR>(aObserver),
1);
if (NS_WARN_IF(!h)) {
LOG_NPS_ERROR("CreateIoCompletionPort error (%d)", GetLastError());
return NS_ERROR_FAILURE;
}
if (NS_WARN_IF(h != mIocp)) {
LOG_NPS_ERROR("CreateIoCompletionPort got unexpected value %p (should be %p)",
h,
mIocp);
CloseHandle(h);
return NS_ERROR_FAILURE;
}
{
MutexAutoLock lock(mLock);
MOZ_ASSERT(!mObservers.Contains(aObserver));
mObservers.AppendElement(aObserver);
// start event loop
if (mObservers.Length() == 1) {
rv = mThread->Dispatch(this, NS_DISPATCH_NORMAL);
if (NS_WARN_IF(NS_FAILED(rv))) {
LOG_NPS_ERROR("Dispatch to thread failed (%08x)", rv);
mObservers.Clear();
return rv;
}
}
}
return NS_OK;
}