Add comments

This commit is contained in:
Kirigaya Kazuto 2018-09-09 23:49:08 +08:00
parent 6c46214f6b
commit f733f6c74e
2 changed files with 34 additions and 20 deletions

View File

@ -120,17 +120,7 @@ int GetNativeErrCode()
#endif #endif
} }
// Internal Socket Call Errcode
enum gerrno
{
UnknownError = -1,
OK,
WouldBlock,
InProgress,
Already,
IsConnected,
Interrupted,
};
gerrno TranslateNativeErrToGErr(int native_errcode) gerrno TranslateNativeErrToGErr(int native_errcode)
{ {
@ -460,7 +450,7 @@ struct NBSendResult::_impl
// 3: Error occurs. // 3: Error occurs.
int status; int status;
int errcode; gerrno errcode;
void update(); void update();
}; };
@ -483,7 +473,7 @@ void NBSendResult::_impl::update()
else if (ret == 0) else if (ret == 0)
{ {
status = 3; status = 3;
errcode = 0; errcode = gerrno::OK;
} }
else // ret == -1 else // ret == -1
{ {
@ -529,7 +519,7 @@ int NBSendResult::getBytesDone()
return _p->done; return _p->done;
} }
int NBSendResult::getErrCode() gerrno NBSendResult::getErrCode()
{ {
return _p->errcode; return _p->errcode;
} }
@ -550,7 +540,7 @@ struct NBRecvResult::_impl
// 3: Error occurs. // 3: Error occurs.
int status; int status;
int errcode; gerrno errcode;
void update(); void update();
}; };
@ -573,7 +563,7 @@ void NBRecvResult::_impl::update()
else if (ret == 0) else if (ret == 0)
{ {
status = 3; status = 3;
errcode = 0; errcode = gerrno::OK;
} }
else // ret == -1 else // ret == -1
{ {
@ -631,12 +621,11 @@ int NBRecvResult::getBytesDone()
return _p->done; return _p->done;
} }
int NBRecvResult::getErrCode() gerrno NBRecvResult::getErrCode()
{ {
return _p->errcode; return _p->errcode;
} }
NBConnectResult sock::connect_nb(const std::string& IPStr, int Port) NBConnectResult sock::connect_nb(const std::string& IPStr, int Port)
{ {
NBConnectResult res; NBConnectResult res;

29
gsock.h
View File

@ -28,6 +28,23 @@ enum
GSOCK_MISMATCH_MODE = -10, // Example: calling blocking method on a non-blocking socket. 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 class vsock
{ {
public: public:
@ -78,13 +95,17 @@ public:
// Is the operation finished. // Is the operation finished.
bool isFinished(); bool isFinished();
// Wait until all data is sent. // Wait until all data is sent.
void wait(); void wait();
// Is all data sent successfully. // Is all data sent successfully.
bool isSuccess(); bool isSuccess();
int getBytesDone(); 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: private:
struct _impl; struct _impl;
std::shared_ptr<_impl> _p; std::shared_ptr<_impl> _p;
@ -104,7 +125,11 @@ public:
bool isSuccess(); bool isSuccess();
int getBytesDone(); 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: private:
struct _impl; struct _impl;
std::shared_ptr<_impl> _p; std::shared_ptr<_impl> _p;