fileapi.cpp
来自「天之炼狱1服务器端源文件游戏服务端不完整」· C++ 代码 · 共 698 行 · 第 1/2 页
CPP
698 行
#if __LINUX__ int result = fcntl ( fd , cmd ); if ( result < 0 ) { switch ( errno ) { case EINTR : throw Error("The F_SETLKW command was interrupted by a signal."); case EBADF : throw Error("fd is not an open file descriptor."); case EACCES : throw Error("Operation is prohibited by locks held by other processes."); case EAGAIN : throw Error("Operation is prohibited because the file has been memory-mapped by another process."); case EDEADLK : throw Error("It was detected that the specified F_SETLKW command would cause a deadlock."); case EMFILE : throw Error("For F_DUPFD, the process already has the maximum number of file descriptors open."); case ENOLCK : throw Error("Too many segment locks open, lock table is full."); default : throw UnknownError(strerror(errno),errno); } } return result;#elif __WINDOWS__ throw UnsupportedError();#endif __END_CATCH}////////////////////////////////////////////////////////////////////////// int FileAPI::fcntl_ex ( int fd , int cmd , long arg ) // throw ( Error );//// Parameters// fd - file descriptor// cmd - file control command// arg - command argument//// Return// various according to cmd//// Exceptions// Error////////////////////////////////////////////////////////////////////////int FileAPI::fcntl_ex ( int fd , int cmd , long arg ) throw ( Error ){ __BEGIN_TRY#if __LINUX__ int result = fcntl ( fd , cmd , arg ); if ( result < 0 ) { switch ( errno ) { case EINTR : throw Error("The F_SETLKW command was interrupted by a signal."); case EINVAL : throw Error("For F_DUPFD, arg is negative or is greater than the maximum allowable value."); case EBADF : throw Error("fd is not an open file descriptor."); case EACCES : throw Error("Operation is prohibited by locks held by other processes."); case EAGAIN : throw Error("Operation is prohibited because the file has been memory-mapped by another process."); case EDEADLK : throw Error("It was detected that the specified F_SETLKW command would cause a deadlock."); case EMFILE : throw Error("For F_DUPFD, the process already has the maximum number of file descriptors open."); case ENOLCK : throw Error("Too many segment locks open, lock table is full."); default : throw UnknownError(strerror(errno),errno); } } return result;#elif __WINDOWS__ throw UnsupportedError();#endif __END_CATCH}////////////////////////////////////////////////////////////////////////// bool getfilenonblocking_ex ( int fd ) // throw ( Error );//// check if this file is nonblocking mode//// Parameters// fd - file descriptor//// Return// true if nonblocking, false if blocking//// Exceptions// Error////////////////////////////////////////////////////////////////////////bool FileAPI::getfilenonblocking_ex ( int fd ) throw ( Error ){ __BEGIN_TRY#if __LINUX__ int flags = fcntl_ex( fd , F_GETFL , 0 ); return flags | O_NONBLOCK;#elif __WINDOWS__ throw UnsupportedError();#endif __END_CATCH}////////////////////////////////////////////////////////////////////////// void setfilenonblocking_ex ( int fd , bool on ) // throw ( Error );//// make this file blocking/nonblocking//// Parameters// fd - file descriptor// on - true if nonblocking, false if blocking//// Return// none//// Exceptions// Error////////////////////////////////////////////////////////////////////////void FileAPI::setfilenonblocking_ex ( int fd , bool on ) throw ( Error ){ __BEGIN_TRY#if __LINUX__ int flags = fcntl_ex( fd , F_GETFL , 0 ); if ( on ) // make nonblocking fd flags |= O_NONBLOCK; else // make blocking fd flags &= ~O_NONBLOCK; fcntl_ex( fd , F_SETFL , flags );#elif __WINDOWS__ throw UnsupportedError();#endif __END_CATCH}////////////////////////////////////////////////////////////////////////// void FileAPI::ioctl_ex ( int fd , int request , void * argp )// throw ( Error )//// exception version of ioctl()//// Parameters// fd - file descriptor// request - i/o control request// argp - argument//// Return// none//// Exceptions// Error////////////////////////////////////////////////////////////////////////void FileAPI::ioctl_ex ( int fd , int request , void * argp ) throw ( Error ){ __BEGIN_TRY#if __LINUX__ if ( ioctl(fd,request,argp) < 0 ) { switch ( errno ) { case EBADF : throw Error("fd is not a valid descriptor."); case ENOTTY : throw Error("fd is not associated with a character special device. or The specified request does not apply to the kind of object that the descriptor fd references."); case EINVAL : throw Error("Request or argp is not valid."); default : throw UnknownError(strerror(errno),errno); } }#elif __WINDOWS__ throw UnsupportedError();#endif __END_CATCH}////////////////////////////////////////////////////////////////////////// void FileAPI::setfilenonblocking_ex2 ( int fd , bool on )// throw ( Error )//// make this stream blocking/nonblocking using ioctl_ex()//// Parameters// fd - file descriptor// on - true if nonblocking, false else//// Return// none//// Exception// Error////////////////////////////////////////////////////////////////////////void FileAPI::setfilenonblocking_ex2 ( int fd , bool on ) throw ( Error ){ __BEGIN_TRY#if __LINUX__ ulong arg = ( on == true ? 1 : 0 ); ioctl_ex(fd,FIONBIO,&arg);#elif __WINDOWS__ throw UnsupportedError();#endif __END_CATCH}////////////////////////////////////////////////////////////////////////// uint FileAPI::available_ex ( int fd )// throw ( Error )//// how much bytes available in this stream? using ioctl_ex()//// Parameters// fd - file descriptor//// Return// #bytes available//// Exceptions// Error////////////////////////////////////////////////////////////////////////uint FileAPI::availablefile_ex ( int fd ) throw ( Error ){ __BEGIN_TRY#if __LINUX__ // 角荐肺 FIONBIO 颇扼固磐甫 林绰 官恩俊 橇肺弊伐捞 俺啊 登菌促. // 蔼阑 罐酒坷骨肺 0 栏肺 檬扁拳矫难林搁 任纠 救傈且 巴 鞍促. uint arg = 0; ioctl_ex(fd,FIONREAD,&arg); return arg;#elif __WINDOWS__ throw UnsupportedError();#endif __END_CATCH}////////////////////////////////////////////////////////////////////////// int FileAPI::dup_ex ( int fd )// throw ( Error )////////////////////////////////////////////////////////////////////////int FileAPI::dup_ex ( int fd ) throw ( Error ){ __BEGIN_TRY#if __LINUX__ int newfd = dup(fd);#elif __WINDOWS__ int newfd = _dup(fd);#endif if ( newfd < 0 ) {#if __LINUX__ switch ( errno ) { case EBADF : throw Error("oldfd isn't an open file descriptor, or newfd is out of the allowed range for file descriptors."); case EMFILE : throw Error("The process already has the maximum number of file descriptors open and tried to open a new one."); default : throw UnknownError(strerror(errno),errno); }//end of switch#elif __WINDOWS__#endif } return newfd; __END_CATCH}////////////////////////////////////////////////////////////////////////// long FileAPI::lseek_ex ( int fd , long offset , int whence )// throw ( Error );////////////////////////////////////////////////////////////////////////long FileAPI::lseek_ex ( int fd , long offset , int whence ) throw ( Error ){ __BEGIN_TRY#if __LINUX__ long result = lseek(fd,offset,whence); if ( result < 0 ) { switch ( errno ) { case EBADF : throw Error("Fildes is not an open file descriptor."); case ESPIPE : throw Error("Fildes is associated with a pipe, socket, or FIFO."); case EINVAL : throw Error("Whence is not a proper value."); default : throw UnknownError(strerror(errno),errno); } }#elif __WINDOWS__ long result = _lseek(fd,offset,whence); if ( result < 0 ) { }#endif return result; __END_CATCH}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?