Skip to content

Commit

Permalink
Tweaks to library
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebenezer-group committed Aug 18, 2024
1 parent d891c26 commit 49e73c9
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/cmwBuffer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,12 @@ void apps (auto& e,auto t,auto...ts){
throw e;
}

inline int getError (){
#ifdef CMW_WINDOWS
return WSAGetLastError();
#else
return errno;
#endif
}

inline int fromChars (::std::string_view s){
int n=0;
::std::from_chars(s.data(),s.data()+s.size(),n);
return n;
}


template<class T>T give (auto& b){return b.template give<T>();}

template<class,class>class ReceiveBuffer;
Expand Down Expand Up @@ -119,7 +110,7 @@ using sockType=SOCKET;
#else
using sockType=int;
inline void setDirectory (char const* d){
if(::chdir(d)==-1)raise("setDirectory",d,errno);
if(::chdir(d)!=0)raise("setDirectory",d,errno);
}

inline int Write (int fd,void const* data,int len){
Expand Down Expand Up @@ -191,22 +182,21 @@ auto setsockWrapper (sockType s,int opt,auto t){
return ::setsockopt(s,SOL_SOCKET,opt,reinterpret_cast<char*>(&t),sizeof t);
}

inline void setRcvTimeout (sockType s,int time){
inline int getError (){
#ifdef CMW_WINDOWS
DWORD t=time*1000;
return WSAGetLastError();
#else
::timeval t{time,0};
return errno;
#endif
if(setsockWrapper(s,SO_RCVTIMEO,t)!=0)raise("setRcvTimeout",getError());
}

inline void closeSocket (sockType s){
inline void setRcvTimeout (sockType s,int time){
#ifdef CMW_WINDOWS
if(::closesocket(s)==SOCKET_ERROR)
DWORD t=time*1000;
#else
if(::close(s)==-1)
::timeval t{time,0};
#endif
raise("closeSocket",getError());
if(setsockWrapper(s,SO_RCVTIMEO,t)!=0)raise("setRcvTimeout",getError());
}

inline int sockWrite (sockType s,void const* data,int len
Expand Down Expand Up @@ -484,6 +474,15 @@ class BufferStack:public SendBuffer<Z>,public ReceiveBuffer<R,Z>{
}
};

inline void closeSocket (sockType s){
#ifdef CMW_WINDOWS
if(::closesocket(s)==SOCKET_ERROR)
#else
if(::close(s)!=0)
#endif
raise("closeSocket",getError());
}

#ifndef CMW_WINDOWS
auto myMin (auto a,auto b){return a<b?a:b;}
constexpr auto qlzFormula (int i){return i+(i>>3)+400;}
Expand Down

0 comments on commit 49e73c9

Please sign in to comment.