Skip to content

Commit

Permalink
添加跨平台错误码处理
Browse files Browse the repository at this point in the history
  • Loading branch information
aurshine committed Nov 25, 2024
1 parent 13b1f94 commit 48e9a35
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
18 changes: 16 additions & 2 deletions aurshine/ayr/fs/linux/linuxlib.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
#ifndef AYR_FS_LINUX_LINUXLIB_HPP_
#define AYR_FS_LINUX_LINUXLIB_HPP_

#if defined(__linux__) || defined(__unix__)
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif

#include "../../base/CString.hpp"

namespace ayr
{
CString errorno2str(int errorno)
{
CString error_msg{ 256 };
strerror_r(errorno, error_msg.data(), 256);
return error_msg;
}

CString get_error_msg() { return errorno2str(errno); }
}
#endif
29 changes: 26 additions & 3 deletions aurshine/ayr/fs/win/winlib.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
#ifndef AYR_FS_WIN_WINLIB_HPP_
#define AYR_FS_WIN_WINLIB_HPP_
#ifndef AYR_FS_WIN_WINLIB_HPP
#define AYR_FS_WIN_WINLIB_HPP

#include <WinSock2.h>
#include <Windows.h>
#include <fileapi.h>
#include <WS2tcpip.h>

#include "../../base/CString.hpp"

#pragma comment(lib, "Ws2_32.lib")
#endif // AYR_FS_WIN_WINLIB_HPP_

namespace ayr
{
// 错误码转化为字符串
CString errorno2str(int errorno)
{
CString error_msg{ 256 };
FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM,
nullptr,
errorno,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
error_msg.data(),
256,
nullptr
);
return error_msg;
}

CString get_error_msg() { return errorno2str(GetLastError()); }
}
#endif // AYR_FS_WIN_WINLIB_HPP
36 changes: 13 additions & 23 deletions aurshine/ayr/net/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ namespace ayr
def closesocket(int socket) { ::close(socket); }
#endif

CString error_msg()
{
CString error_msg{ 128 };
#ifdef _WIN32 || _WIN64
int errorno = WSAGetLastError();
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, errorno,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_msg.data(), 128, nullptr);
#else
strerror(error_msg.data(), errno);
#endif
return error_msg;
}


struct SockAddrIn : public Object<SockAddrIn>
{
Expand All @@ -66,7 +53,7 @@ namespace ayr
if (ip == nullptr)
addr_.sin_addr.s_addr = INADDR_ANY;
else if (inet_pton(AF_INET, ip, &addr_.sin_addr) != 1)
RuntimeError(error_msg());
RuntimeError(get_error_msg());
}

sockaddr* get_sockaddr() { return reinterpret_cast<sockaddr*>(&addr_); }
Expand All @@ -79,7 +66,7 @@ namespace ayr
{
CString ip{ 16 };
if (inet_ntop(family, &addr_.sin_addr, ip.data(), 16) == nullptr)
RuntimeError(error_msg());
RuntimeError(get_error_msg());
return ip;
}

Expand Down Expand Up @@ -113,7 +100,7 @@ namespace ayr

socket_ = socket(family, type, protocol);
if (socket_ == INVALID_SOCKET)
RuntimeError(error_msg());
RuntimeError(get_error_msg());
}

Socket(Socket&& other) noexcept : socket_(other.socket_) { other.socket_ = INVALID_SOCKET; }
Expand All @@ -124,25 +111,28 @@ namespace ayr

Socket& operator=(Socket&& other) noexcept
{
close();
socket_ = other.socket_;
other.socket_ = INVALID_SOCKET;
return *this;
}

int get_socket() const { return socket_; }

// 绑定ip:port
void bind(const char* ip, int port) const
{
SockAddrIn addr(ip, port);

if (::bind(socket_, addr.get_sockaddr(), addr.get_socklen()) != 0)
RuntimeError(error_msg());
RuntimeError(get_error_msg());
}

// 监听端口
void listen(int backlog = 8) const
{
if (::listen(socket_, backlog) != 0)
RuntimeError(error_msg());
RuntimeError(get_error_msg());
}

// 接受一个连接
Expand All @@ -157,7 +147,7 @@ namespace ayr
SockAddrIn addr(ip, port);

if (::connect(socket_, addr.get_sockaddr(), addr.get_socklen()) != 0)
RuntimeError(error_msg());
RuntimeError(get_error_msg());
}

// 发送size个字节的数据, 数据头部包含了数据大小,需要用recv接收
Expand All @@ -177,7 +167,7 @@ namespace ayr
int head_size = 0;
int recvd = ::recv(socket_, (char*)&head_size, 4, flags);
if (recvd == SOCKET_ERROR)
RuntimeError(error_msg());
RuntimeError(get_error_msg());
else if (recvd == 0)
return "";

Expand All @@ -189,7 +179,7 @@ namespace ayr
{
int num_send = ::sendto(socket_, data, size, flags, to.get_sockaddr(), to.get_socklen());
if (num_send == SOCKET_ERROR)
RuntimeError(error_msg());
RuntimeError(get_error_msg());
else if (num_send != size)
RuntimeError(std::format("Failed to send all data.{}/{}", num_send, size));
}
Expand Down Expand Up @@ -231,7 +221,7 @@ namespace ayr
{
int num_send = ::send(socket_, ptr, size, flags);
if (num_send == SOCKET_ERROR)
RuntimeError(error_msg());
RuntimeError(get_error_msg());
else if (num_send == 0)
continue;
ptr += num_send;
Expand All @@ -248,7 +238,7 @@ namespace ayr
{
int recvd = ::recv(socket_, ptr, size, flags);
if (recvd == SOCKET_ERROR)
RuntimeError(error_msg());
RuntimeError(get_error_msg());
ptr += recvd;
size -= recvd;
}
Expand Down

0 comments on commit 48e9a35

Please sign in to comment.