From f733f6c74e5ab12dcc791685cee8f22ea3b15d43 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sun, 9 Sep 2018 23:49:08 +0800 Subject: [PATCH] Add comments --- gsock.cpp | 25 +++++++------------------ gsock.h | 29 +++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/gsock.cpp b/gsock.cpp index 42468b6..fb854cd 100644 --- a/gsock.cpp +++ b/gsock.cpp @@ -120,17 +120,7 @@ int GetNativeErrCode() #endif } -// Internal Socket Call Errcode -enum gerrno -{ - UnknownError = -1, - OK, - WouldBlock, - InProgress, - Already, - IsConnected, - Interrupted, -}; + gerrno TranslateNativeErrToGErr(int native_errcode) { @@ -460,7 +450,7 @@ struct NBSendResult::_impl // 3: Error occurs. int status; - int errcode; + gerrno errcode; void update(); }; @@ -483,7 +473,7 @@ void NBSendResult::_impl::update() else if (ret == 0) { status = 3; - errcode = 0; + errcode = gerrno::OK; } else // ret == -1 { @@ -529,7 +519,7 @@ int NBSendResult::getBytesDone() return _p->done; } -int NBSendResult::getErrCode() +gerrno NBSendResult::getErrCode() { return _p->errcode; } @@ -550,7 +540,7 @@ struct NBRecvResult::_impl // 3: Error occurs. int status; - int errcode; + gerrno errcode; void update(); }; @@ -573,7 +563,7 @@ void NBRecvResult::_impl::update() else if (ret == 0) { status = 3; - errcode = 0; + errcode = gerrno::OK; } else // ret == -1 { @@ -631,12 +621,11 @@ int NBRecvResult::getBytesDone() return _p->done; } -int NBRecvResult::getErrCode() +gerrno NBRecvResult::getErrCode() { return _p->errcode; } - NBConnectResult sock::connect_nb(const std::string& IPStr, int Port) { NBConnectResult res; diff --git a/gsock.h b/gsock.h index e701b14..15e171e 100644 --- a/gsock.h +++ b/gsock.h @@ -28,6 +28,23 @@ enum GSOCK_MISMATCH_MODE = -10, // Example: calling blocking method on a non-blocking socket. }; +// Internal Socket Call Errcode +enum gerrno +{ + UnknownError = -1, + OK = 0, + // Values of all known errors are positive number. + WouldBlock, + InProgress, + Already, + IsConnected, + Interrupted, +}; + +// For Debug purpose. +int GetNativeErrCode(); +gerrno TranslateNativeErrToGErr(int native_errcode); + class vsock { public: @@ -78,13 +95,17 @@ public: // Is the operation finished. bool isFinished(); + // Wait until all data is sent. void wait(); + // Is all data sent successfully. bool isSuccess(); int getBytesDone(); - int getErrCode(); + // If connection is closed while sending data, + // the state changes to [Finished,Failed]. And errcode will be 0. + gerrno getErrCode(); private: struct _impl; std::shared_ptr<_impl> _p; @@ -104,7 +125,11 @@ public: bool isSuccess(); int getBytesDone(); - int getErrCode(); + // If connection is closed while receiving data, + // the state changes to [Finished,Failed]. And errcode will be 0. + // If setStopAtEdge(true) is called and there's no more data while isFinished() is called, + // the state changes to [Finished,Failed]. And errcode will be gerrno::WouldBlock + gerrno getErrCode(); private: struct _impl; std::shared_ptr<_impl> _p;