int main (int argc, char *argv[]) { init(); if(open_accesory_dev() < 0) return 0; fprintf(stdout, "Start setting up accessory\n"); if(chAccessoryMode( "bols", "Demo", "Description", "1.0", //"market://search?jp.co.topgate.android.game.gree.sengoku", "http://market.android.com/search?q=jp.co.topgate.android.game.gree.sengoku", //"http://neuxs-computing.ch", "2hhh254711SerialNhho.") < 0){ fprintf(stdout, "Error setting up accessory\n"); closeHandle(); return -1; }; fprintf(stdout, "end setup\n"); closeHandle(); fprintf(stdout, "Done, no errors\n"); return 0; }
Error ChildProcess::writeToStdin(const std::string& input, bool eof) { // write synchronously to the pipe if (!input.empty()) { if (options().pseudoterminal) { Error error = WinPty::writeToPty(pImpl_->hStdInWrite, input); if (error) return error; } else { DWORD dwWritten; BOOL bSuccess = ::WriteFile(pImpl_->hStdInWrite, input.data(), input.length(), &dwWritten, NULL); if (!bSuccess) return systemError(::GetLastError(), ERROR_LOCATION); } } // close pipe if requested if (eof) return closeHandle(&pImpl_->hStdInWrite, ERROR_LOCATION); else return Success(); }
size_t ZnkThread_create( ZnkThreadFunc func, void* arg ) { uintptr_t thread_id = 0; size_t thno = Znk_NPOS; ZnkThreadInfo thinfo; ZnkMutex* mtx = znThreadList_theMutex(); if( *mtx == NULL ){ /* まだ初期化されていない */ ZnkThreadList_initiate( 128 ); } thinfo = znThreadList_issueInfo( &thno ); /*** * 以下、thinfo は used_ が true になっているため、 * 他のスレッドがこの内容を書き換えることはないはず. */ assert( func ); thinfo->func_ = func; thinfo->arg_ = arg; if( thinfo->handle_ ){ /* 過去の残骸が存在する. */ closeHandle( thinfo ); } thinfo->handle_ = createThread( thinfo, &thread_id ); thinfo->id_ = thread_id; thinfo->active_ = true; /* この時点で active_ が true であることが保証される */ return thno; }
void ZProfile::closePath () { ZFUNCTRACE_DEVELOP ("ZProfile::closePath()"); if (iPathHandle) { if ((HKEY) iPathHandle != (HKEY) iRootHandle) closeHandle ((HKEY) iPathHandle); iPathHandle = 0; } // if } // closePath
/* * ======== UARTUtils_deviceclose ======== */ int UARTUtils_deviceclose(int fd) { /* Return if a UART other than UART 0 was specified. */ if (fd != 0) { return (-1); } closeHandle(fd); return (0); }
static HKEY openHandle (HKEY aParentHandle, ZString aPath, ZBoolean aCreate) { ZFUNCTRACE_DEVELOP ("openHandle(HKEY aParentHandle, ZString aPath, ZBoolean aCreate)"); HKEY parentHandle (aParentHandle); HKEY handle; for (;;) { ZString token (splitPath (aPath)); if (aCreate) { if (RegCreateKeyEx (parentHandle, token.constBuffer (), 0, "", REG_OPTION_NON_VOLATILE, (KEY_READ | KEY_WRITE), NULL, &handle, NULL) != ERROR_SUCCESS) { if (parentHandle != aParentHandle) closeHandle (parentHandle); throwSysErr (SRegCreateKeyEx); } // if } else { if (RegOpenKeyEx (parentHandle, token.constBuffer (), 0, KEY_READ, &handle) != ERROR_SUCCESS) { if (parentHandle != aParentHandle) closeHandle (parentHandle); throwSysErr (SRegOpenKeyEx); } // if } // if if (parentHandle != aParentHandle) closeHandle (parentHandle); if (aPath.size () == 0) break; parentHandle = handle; } // for return handle; } // openHandle
int main(int argc, char * argv[]) { printf("Naga Epic Control v%d\n", VERSION); int retry = 0; int count = 0; while ((retry == 1 && count < 3) || count == 0) { count++; if (init() != 0) return 1; // Verbose mode if (scanArgs("-v", argv, argc)) { verbose = 1; } // Brightness control unsigned char brightness = 255; int tmp = scanArgs("-l", argv, argc); if (tmp) { if (tmp < argc - 1) tmp++; // Looking for number after -l brightness = atoi(argv[tmp]); // If number not found, looking for on or off if (strcmp(argv[tmp], "on") == 0) { brightness = 255; } else if (strcmp(argv[tmp], "off") == 0) { brightness = 0; } if (verbose) printf("[Debug] Brightness: %d\n", brightness); // Building command for mouse Lights_Brightness[10] = brightness; Lights_Brightness[11] = brightness ^ 3; sendcmd(Lights_Brightness); // Receiving and checking data if (recvcmd()[0] != 1) { printf("[Error] Command failed!\n"); retry = 1; } else { retry = 0; } } closeHandle(); } return 0; }
void ZProfile::closeRoot () { ZFUNCTRACE_DEVELOP ("ZProfile::closeRoot()"); #ifdef ZC_WIN if (iRootHandle) { closePath (); closeHandle ((HKEY) iRootHandle); iRootHandle = 0; } // if #endif #ifdef ZC_OS2 if (iRootHandle != 0 && iRootHandle != HINI_SYSTEMPROFILE && iRootHandle != HINI_USERPROFILE) { if (!PrfCloseProfile (iRootHandle)) throwSysErr (PrfCloseProfileName); iRootHandle = 0; } // if #endif } // closeRoot
static int finishParentHostCommandStream (HostCommandStream *hcs, void *data) { { HANDLE *handle = getParentHandle(hcs); int mode = hcs->isInput? O_WRONLY: O_RDONLY; int fileDescriptor; if ((fileDescriptor = _open_osfhandle((intptr_t)*handle, mode)) == -1) { logSystemError("_open_osfhandle"); return 0; } *handle = INVALID_HANDLE_VALUE; if (!finishHostCommandStream(hcs, fileDescriptor)) { _close(fileDescriptor); return 0; } } closeHandle(getChildHandle(hcs)); return 1; }
int IOCPNetwork::cancel(iocp_key_t key) { iocp_context_t* context = reinterpret_cast<iocp_context_t*>(key); int ret = IOCP_SUCESS; /* * 如果有速度限制定时器,则先删除定时器. */ if(context->lockPtr) context->lockPtr->wlock(); if(context->readOlp.oppType == IOCP_DELAY_READ) { context->readOlp.oppType = IOCP_NONE; _timerQueue.deleteTimer(context->readOlp.timer, false); context->readOlp.timer = NULL; } if(context->writeOlp.oppType == IOCP_DELAY_WRITE) { context->writeOlp.oppType = IOCP_NONE; _timerQueue.deleteTimer(context->writeOlp.timer, false); context->writeOlp.timer = NULL; } /* * 如果此时有异步操作正在进行,则关闭套接字使异步操作失败并返回. */ if(context->status == IOCP_NORMAL && (context->readOlp.oppType != IOCP_NONE || context->writeOlp.oppType != IOCP_NONE)) { closeHandle(context); context->status |= (IOCP_CANCELED | IOCP_HANDLE_CLOSED); ret = IOCP_PENDING; } if(context->lockPtr) context->lockPtr->unlock(); return ret; }
void* threadTypeB(void* in) { char ipName[20]; #ifdef DEBUG int portName; portName=(htons(((conInfwRec*)in)->conAddr->sin_port)); #endif strcpy(ipName,inet_ntoa(((conInfwRec*)in)->conAddr->sin_addr)); #ifdef DEBUG printHeader(ipName,portName); printf("thread type B is created\n"); #endif int conSocket=*(((conInfwRec*)in)->conSocket); msgReceiver msgRecSer(serverSocket); msgReceiver* msgRecCli=((conInfwRec*)in)->rec; MYMSG msg[LMSGL]; char screenName[MSGL]; bool validClient=false; screenName[0]=0; while(1) { uint32_t length; #ifdef DEBUG printHeader(ipName,portName); printf("thread type b: tries to get type b message\n"); #endif if(msgRecCli->receiveTypeBMsg(msg,&length)==DISCONNECT) { #ifdef DEBUG printHeader(ipName,portName); printf("connection close\n"); #endif if(screenName[0]==0) closeHandle(conSocket); else closeHandle(conSocket,scrName); } switch (msg[0]) { case HELLO: { #ifdef DEBUG printHeader(ipName,portName); printf("receive a HELLO MSG, content is:\n"); printMsg(ipName,portName,msg,length); #endif uint32_t length; int num; userInf uInf[MAXUSER]; fetchFmsg(msg,&length,1); memcpy(screenName,&msg[5],length); screenName[length]=0; MYMSG sMsg[MSGL]; length=createGetListMsg(sMsg); #ifdef DEBUG printHeader(ipName,portName); printf("get list message:\n"); printMsg(ipName,portName,sMsg,length); printHeader(ipName,portName); printf("send to server\n"); #endif write(serverSocket,sMsg,length); msgRecSer.receiveMsg(msg,&length); #ifdef DEBUG printHeader(ipName,portName); printf("receive a message from server, content is:\n"); printMsg(ipName,portName,msg,length); #endif clientListParse(msg,uInf,&num); bool find=false; for(int i=0; i<num; i++) { if (strcmp(screenName,uInf[i].name)==0) { find=true; break; } } if (find) { #ifdef DEBUG printHeader(ipName,portName); printf("tries to send hello ok message\n"); #endif pthread_rwlock_wrlock(&cConListLock); if (cConList.connectWith(screenName,conSocket,&msgRecCli)==TOOMANYCONNECTION) { printf("Too many connections between clients\n"); delete msgRecCli; close(conSocket); err("Type B: makeConnection()\n",-9); } pthread_rwlock_unlock(&cConListLock); validClient=true; length=createHelloOkMsg(sMsg); #ifdef DEBUG printHeader(ipName,portName); printf("sending hello ok message\n"); printMsg(ipName,portName,sMsg,length); #endif write(conSocket,sMsg,length); } else { #ifdef DEBUG printHeader(ipName,portName); printf("unknown client\n"); #endif length=createErrMsg(sMsg,0x04); #ifdef DEBUG printHeader(ipName,portName); printf("error msg is sent\n"); printMsg(ipName,portName,sMsg,length); #endif write(conSocket,sMsg,length); } break; } case MSG: { if (!validClient) { close(conSocket); err("Type B: validClient==false",NOSUCHUSER); } buf.addMsg(msg,screenName); } } } return NULL; }
FileInputStream::~FileInputStream() { closeHandle(); }
QNativeWifiEngine::~QNativeWifiEngine() { closeHandle(); }
QNativeWifiEngine::QNativeWifiEngine(QObject *parent) : QBearerEngineImpl(parent), handle(INVALID_HANDLE_VALUE) { connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(closeHandle())); }
FileOutputStream::~FileOutputStream() { flushBuffer(); flushInternal(); closeHandle(); }
Znk_INLINE int waitEnd( ZnkThreadInfo thinfo ) { return closeHandle( thinfo ); }
Znk_INLINE int waitEnd( ZnkThreadInfo thinfo ) { int result = (int)WaitForSingleObject( (HANDLE)thinfo->handle_, INFINITE ); closeHandle( thinfo ); return result; }
int IOCPNetwork::destroy() { /* * destroy 需要等待工作线程结束,如果工作线程中调用destroy,会死锁. */ if(_tids) { uintptr_t curThread = reinterpret_cast<uintptr_t>(GetCurrentThread()); for(int i = 0; i < _threads; ++i) { if(curThread == _tids[i]) return IOCP_DESTROYFAILED; } } /* * 马上设置停止标志 */ _inited = false; /* * 发送退出通知,确保工作线程都从GetQueuedCompletionStatus阻塞中返回并终止运行. */ if(_tids) { assert(_threads > 0); if(_iocpHandle) { for(int i = 0; i < _threads; ++i) { PostQueuedCompletionStatus(_iocpHandle, 0, NULL, NULL); } } for(int i = 0; i < _threads; ++i) { if( _tids[i] ) { WaitForSingleObject(reinterpret_cast<HANDLE>(_tids[i]), INFINITE); CloseHandle(reinterpret_cast<HANDLE>(_tids[i])); } } delete []_tids; } _tids = NULL; _threads = 0; /* * 关闭完成端口句柄 */ if(_iocpHandle) { if(!CloseHandle(_iocpHandle)) { assert(0); } _iocpHandle = NULL; } /* * 关闭定时器 */ _timerQueue.destroy(); /* * 回收资源 */ if(_contextMap.size() > 0) { for(context_map_t::iterator iter = _contextMap.begin(); iter != _contextMap.end(); ++iter) { iocp_context_t *context = iter->second; /* * 关闭套接字 */ if(!(context->status & IOCP_HANDLE_CLOSED)) { closeHandle(context); context->status |= IOCP_HANDLE_CLOSED; } freeContext(context); } _contextMap.clear(); } /* * 同步锁池 */ _lockPool.destroy(); return IOCP_SUCESS; }
static void closeOnSignal( int sig ) { printf( "signal %d\n", sig ); closeHandle(); }
WatcherThread::~WatcherThread() { closeHandle(); }
void destructHostCommandPackageData (HostCommandPackageData *pkg) { closeHandle(&pkg->inputHandle); closeHandle(&pkg->outputHandle); }
ProcessHandleImpl::~ProcessHandleImpl() { closeHandle(); }
int IOCPNetwork::remove(iocp_key_t key) { if( !_inited ) return IOCP_UNINITIALIZED; iocp_context_t* context = reinterpret_cast<iocp_context_t*>(key); bool isBusy = false; /* * 如果套接字处于忙碌状态,则设置删除标志并关闭套接字. */ if(context->lockPtr) context->lockPtr->wlock(); if( context->readOlp.oppType != IOCP_NONE || context->writeOlp.oppType != IOCP_NONE ) { isBusy = true; } else { /* 空闲状态,设置一个删除标志,禁止继续操作 */ context->status |= IOCP_REMOVE; } if(context->lockPtr) context->lockPtr->unlock(); /* * 如果套接字处于忙碌状态,则关闭 */ if(isBusy) { return IOCP_BUSY; } /* * 删除各个定时器.延时操作. */ if(context->readOlp.timer != NULL) { assert(0); _timerQueue.deleteTimer(context->readOlp.timer, true); context->readOlp.timer = NULL; } if(context->writeOlp.timer != NULL) { assert(0); _timerQueue.deleteTimer(context->writeOlp.timer, true); context->writeOlp.timer = NULL; } /* * 关闭套接字 */ if(!(context->status & IOCP_HANDLE_CLOSED)) { closeHandle(context); context->status |= IOCP_HANDLE_CLOSED; } /* * 移除记录 */ _lock.lock(); _contextMap.erase(context->h); if(context->lockPtr) { _lockPool.recycle(context->lockPtr); context->lockPtr = NULL; } _lock.unlock(); /* * 回收资源 */ freeContext(context); return IOCP_SUCESS; }