Update notes

This commit is contained in:
Douwanna 2020-06-02 19:03:16 +08:00 committed by GitHub
parent 8cb392152e
commit 84dc44a65d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 138 additions and 5 deletions

View File

@ -40,11 +40,13 @@ namespace tars
/**
* @file tc_epoller.h
* @brief epoll操作封装类
* @brief Epoll operation encapsulation class
*/
/////////////////////////////////////////////////
/**
* @brief epoll异常类
* @brief epoll exception class
*/
struct TC_Epoller_Exception : public TC_Exception
{
@ -54,12 +56,14 @@ struct TC_Epoller_Exception : public TC_Exception
/**
* @brief epoller操作类EPOLLET方式做触发
* @brief epoller operation class, EPOLLET has been used by default for triggering
*/
class TC_Epoller
{
public:
/**
* @brief epoll从wait中醒过来
* @brief Notice epoll to wake up from 'wait'
*/
class NotifyInfo
{
@ -69,31 +73,37 @@ public:
/**
*
* Initialization
*/
void init(TC_Epoller *ep);
/**
*
* Add corresponding data
*/
void add(uint64_t data);
/**
* notify醒过来
* Notice notify to wake up
*/
void notify();
/**
*
* Diposit
*/
void release();
/**
* fd
* Get notifyFd
*/
int notifyFd();
/**
* ,
* Clear up notice, otherwise, it will always respond
*/
// void clear();
protected:
@ -103,26 +113,32 @@ public:
// bool _clear = true;
TC_Epoller *_ep;
/*Events associated with the notification handle*/
uint64_t _data; //关联到通知句柄的事件
};
/**
* @brief .
* @brief Constructor Function
*
* @param bEt ET模式
* @param bEt The default is et mode, which is notified when the status changes
*/
TC_Epoller();
/**
* @brief .
* @brief Destructor
*/
~TC_Epoller();
/**
* @brief epoll句柄.
* @brief Generate epoll handle.
*
* @param max_connections epoll服务需要支持的最大连接数
* @param max_connections Maximum number of connections the epoll service needs to support
*/
void create(int max_connections);
@ -135,49 +151,66 @@ public:
/**
* @brief .
* @brief Add listening handle.
*
* @param fd
* @param fd handle
* @param data , epoll_event中获取到
* @param data auxiliary data that can be obtained in epoll_event subsequently
* @param event EPOLLIN|EPOLLOUT
* @param event Events to be listened on EPOLLIN|EPOLLOUT
*
*/
void add(SOCKET_TYPE fd, uint64_t data, int32_t event);
/**
* @brief .
* @brief Modify handle event
*
* @param fd
* @param fd handle
* @param data , epoll_event中获取到
* @param data auxiliary data that can be obtained in epoll_event subsequently
* @param event EPOLLIN|EPOLLOUT
* @param event Events to be listened on EPOLLIN|EPOLLOUT
*/
void mod(SOCKET_TYPE fd, uint64_t data, int32_t event);
/**
* @brief .
* @brief Delete handle event.
*
* @param fd
* @param fd handle
* @param data , epoll_event中获取到
* @param data auxiliary data that can be obtained in epoll_event subsequently
* @param event EPOLLIN|EPOLLOUT
* @param event Events to be listened on EPOLLIN|EPOLLOUT
*/
void del(SOCKET_TYPE fd, uint64_t data, int32_t event);
/**
* @brief .
* @brief wait time
*
* @param millsecond
* @return int
* @return int Number of handles triggered by events
*/
int wait(int millsecond);
/**
* @brief .
* @brief Get the triggered handle
*
* @return struct epoll_event&
* @return Struct epoll_event& triggered event
*/
epoll_event& get(int i);// { assert(_pevs != 0); return _pevs[i]; }
/**
* @brief
* @brief whether it have event to read
*
* @return
*/
@ -185,6 +218,7 @@ public:
/**
* @brief
* @brief whether it have event to write
*
* @return
*/
@ -192,6 +226,7 @@ public:
/**
* @brief
* @brief whether it have eception event
*
* @return
*/
@ -199,13 +234,16 @@ public:
/**
* @brief /
* @brief Get low/high bit data
* @param high: true:, false:
* @param high: true:high level, false:low level
* @return
*/
static uint32_t getU32(const epoll_event &ev, bool high);
/**
* @brief 64bit数据
* @brief Get 64 bit data
* @return
*/
static uint64_t getU64(const epoll_event &ev);
@ -214,12 +252,19 @@ protected:
/**
* @brief epollEPOLL设为边缘触发EPOLLET模式
* @brief Control epoll, set EPOLL to Edge Trigger EPOLLET mode
* @param fd create函数时被赋值
* @param fd Handle, assigned when creating function
* @param data , epoll_event中获取到
* @param data auxiliary data that can be obtained in epoll_event subsequently
* @param event
* @param event the event to be listened
* @param op EPOLL_CTL_ADD fd到epfd中
* EPOLL_CTL_MODfd的监听事件
* EPOLL_CTL_DELepfd中删除一个fd
* @param op EPOLL_CTL_ADDRegister new FD into EPFD
* EPOLL_CTL_MODModify the monitoring events for registered fd
* EPOLL_CTL_DELDelete an FD from epfd
*
*/
void ctrl(SOCKET_TYPE fd, uint64_t data, uint32_t events, int op);
@ -237,11 +282,13 @@ protected:
/**
*
* The max amount of connections
*/
int _max_connections;
/**
*
* Event Set
*/
epoll_event *_pevs;
};

View File

@ -28,40 +28,52 @@ namespace tars
/**
* @file tc_ex.h
* @brief
* @brief Exception Class
*/
/////////////////////////////////////////////////
/**
* @brief .
* @brief Exception Class
*/
class TC_Exception : public exception
{
public:
/**
* @brief errno的构造函数
* @brief Constructor , provide a constructor which can pass errno
* @param err, (linux版本, errno获取错误码, windows版本, GetLastError())
* @param err Whether system error information is included(linux: use errno to get error code, windows: use GetLastError() to get errors)
*
* @param buffer
* @param buffer alert information of exceptions
*/
explicit TC_Exception(const string &buffer);
/**
* @brief ,errno的构造函数
* @brief ,errno的构造函数
* @brief Constructor , provider a constructor to pass errno
*
*
* get error message directly when throw exception
*
* @param buffer
* @param buffer
* @param buffer alert information of exceptions
*
* @param errno :errno, windows版本 :GetLastError()
* @param errno pass:errno, windows version pass:GetLastError()
*/
TC_Exception(const string &buffer, int err);
/**
* @brief
* @brief Destructor
*/
virtual ~TC_Exception() throw();
/**
* @brief .
* @brief Error Message
*
* @return const char*
*/
@ -69,22 +81,28 @@ public:
/**
* @brief
* @brief Get error code
*
* @return 0
* @return if get successfully , return 0
*/
int getErrCode() { return _code; }
/**
* @brief (linux是errno, windows是GetLastError())
* @brief Get the error string (linux: errno, windows: GetLastError())
*
* @return 0
* @return if get successfully , return 0
*/
static string parseError(int err);
/**
* @brief (linux是errno, windows是GetLastError)
* @brief Get system error code(linux: errno, windows: GetLastError())
*
* @return 0
* @return if get successfully , return 0
*/
static int getSystemCode();
@ -95,18 +113,23 @@ private:
/**
*
* Error Code
*/
int _code;
/**
*
* Information about exceptions
*/
string _buffer;
};
//为了避免windows平台GetLastError()获取不对的问题, 因为抛异常, throw TC_Exception("xxxx", TC_Exception::getSystemCode())时
//In order to avoid getting the wrong getlasterror() on Windows platform, throw TC_ Exception("xxxx", TC_ Exception:: getsystemcode())
//回调用系统函数分配内存, 导致错误码被改写, 因此专门定义宏来抛出异常
//Callback uses system function to allocate memory, which causes error code to be rewritten, so it specially defines macro to throw exception
//先获取到错误码, 再throw
//Get the error code first, and then throw
#define THROW_EXCEPTION_SYSCODE(EX_CLASS, buffer) \
{ \
int ret = TC_Exception::getSystemCode(); \

View File

@ -29,11 +29,13 @@ namespace tars
/**
*@file tc_fifo.h
*@brief FIFO封装类.
*@brief FIFO encapsulation class
*/
/////////////////////////////////////////////////
/**
*@brief .
*@brief Pipeline operation.
*/
class TC_Fifo
{
@ -42,6 +44,8 @@ public:
/**
* @brief .
* EM_WRITEEM_READ
* @brief Enumeration types for pipeline operations
* Defines the type of operation on the pipeline, EM_ Write: write pipe, EM_ Read: read pipe
*/
enum ENUM_RW_SET
{
@ -52,75 +56,99 @@ public:
public:
/**
* @brief .
* @brief Constructor
*
* @param bOwener : ture
* @param bOwener : Whether the pipeline is owned, the default is ture
*/
TC_Fifo(bool bOwener = true);
/**
* @brief .
* @brief Constructor
*/
~TC_Fifo();
public:
/**
* @brief FIFO.
* @brief Open FIFO
*
* @param sPath FIFO文件的路径
* @param sPath the file path of the FIFO to open
* @param enRW
* @param enRN Pipe opration type
* @param mode FIFO文件的权限
* @param mode The permissions of this FIFO file are read-write by default
* @return 0-,-1-
* @return 0 - successfull, -1 - failed
*/
int open(const std::string & sPath, ENUM_RW_SET enRW, mode_t mode = 0777);
/**
* @brief fifo
* @brief Close FIFO
*/
void close();
/**
* @brief FIFO的文件描述符.
* @brief Get the file descriptor of FIFO
*
* @return FIFO的文件描述符
* @return the file descriptor of FIFO
*/
int fd() const { return _fd; }
/**
* @brief , 00
* @brief read data, when read successfully, return the actual number of bytes read, if the value returned is 0, the end of the file has been read; less than 0 indicates an error
* @param buffer
* @param buffer the content to be read
* @param max_size
* @param max_size size of the data to be read
* @return ,0
* @return the length of the read data , if it is less then 0, read failed
*/
int read(char * szBuff, const size_t sizeMax);
/**
* @brief .
* @brief Write data to the pipeline
*
* @param szBuff
* @param szBuff data to be wrote
* @param sizeBuffLen
* @param sizeBuffLen size of data
* @return 0
* 0
* @return geater than 0: Some or all of the data are shown and written
* less than 0: Error
*/
int write(const char * szBuff, const size_t sizeBuffLen);
private:
/**
* FIFO文件的路径
* file path of FIFO
*/
std::string _sPathName;
/**
* FIFO
* whether it have FIFO
*/
bool _bOwner;
/**
* FIFO的文件的操作类型
* file opration type of FIFO
*/
ENUM_RW_SET _enRW;
/**
* FIFO的文件描述符
* file descriptor of FIFO
*/
int _fd;
};

View File

@ -46,6 +46,7 @@ struct TC_FileMutex_Exception : public TC_Lock_Exception
/**
* @brief , :.
* @brief file lock , attion : You can only lock between processes
*/
class TC_FileMutex
{
@ -53,45 +54,56 @@ public:
/**
* @brief .
* @brief Constructor
*/
TC_FileMutex();
/**
* @brief .
* @brief Desturctor
*/
virtual ~TC_FileMutex();
/**
* @brief .
* @brief Initialize file lock
*
* @param filename
* @param filename file name of the file to be operated
*/
void init(const std::string& filename);
/**
* @brief .
* @brief Add read lock
*
*@return 0--1-
*@return 0 - lock successfully, -1 - failed to lock
*/
int rlock();
/**
* @brief .
* @brief Unlock
*
* @return 0--1-
* @return 0 - unlock successfully, -1 - failed to unlock
*/
int unrlock();
/**
* @brief .
* @brief Try read lock.
*
* @throws TC_FileMutex_Exception
* @return false, false
* @return true, false
* @return return true: lock successfully, return false: failed to lock
*/
bool tryrlock();
/**
* @brief .
* @brief Add write lock
*
* @return int
*/
@ -99,60 +111,83 @@ public:
/**
* @brief .
* @brief Unlock
*/
int unwlock();
/**
* @brief .
* @brief Try write lock.
*
* @return boolfalse, false
* @return booltrue, false
* @return return true: lock successfully, return false: failed to lock
* @throws TC_FileMutex_Exception
*/
bool trywlock();
/**
* @brief .
* @brief Read Lock.
*
* @return int, 0
* @return int, 0 - correct
*/
int lock(){return wlock();};
/**
* @brief .
* @brief Unlock
*/
int unlock();
/**
* @brief .
* @brief Try unlock
*
* @throws TC_FileMutex_Exception
* @return int, 0
* @return int, 0 - correct
*/
bool trylock() {return trywlock();};
protected:
/**
* @brief .
* @brief Set a lock.
*
* @param fd
* @param fd the file descriptor of the lock to be set
* @param cmd
* @param cmd opeational order
* @param type F_RDLCK ,F_WRLCK ,F_UNLCK
* @param type three states: F_RDLCK ,F_WRLCK ,F_UNLCK
* @param offset
* @param offset the offset
* @param whence
* @param whence the starting position pf the lock,three ways
* @param len
* @param len the size of the locked area
* @return int0-1.
* @return int: successfully,return 0; else return -1
*/
int lock(int fd, int cmd, int type, off_t offset, int whence, off_t len);
/**
* @brief .
* @brief Whether it is locked by other threads
*
* @param fd
* @param fd the file desciptor to be set
* @param type F_RDLCK ,F_WRLCK ,F_UNLCK
* @param type three types: F_RDLCK ,F_WRLCK ,F_UNLCK
* @param offset
* @param offset the offset
* @param whence
* @param whence the starting position of the lock ,three ways
* @param len
* @return bool truefalse.
* @param len the size of the locked area
* @return bool truefalse.
* @return int: successfully,return 0; else return -1
*/
bool hasLock(int fd, int type, off_t offset, int whence, off_t len);