#include "eventfd.hpp"
+#include <vist/exception.hpp>
+
#include <sys/types.h>
#include <unistd.h>
#include <cstdint>
-#include <stdexcept>
namespace vist {
namespace event {
: fd(::eventfd(initval, flags))
{
if (this->fd == -1)
- throw std::runtime_error("Failed to create eventfd.");
+ THROW(ErrCode::RuntimeError) << "Failed to create eventfd.";
}
EventFD::~EventFD()
{
const std::uint64_t val = 1;
if (::write(this->fd, &val, sizeof(val)) == -1)
- throw std::runtime_error("Failed to write to eventfd.");
+ THROW(ErrCode::RuntimeError) << "Failed to write to eventfd.";
}
void EventFD::receive(void)
{
std::uint64_t val;
if (::read(this->fd, &val, sizeof(val)) == -1)
- throw std::runtime_error("Failed to read from eventfd.");
+ THROW(ErrCode::RuntimeError) << "Failed to read from eventfd.";
}
int EventFD::getFd(void) const noexcept
#include "mainloop.hpp"
#include <vist/logger.hpp>
+#include <vist/exception.hpp>
#include <errno.h>
#include <unistd.h>
stopped(false)
{
if (epollFd == -1)
- throw std::runtime_error("Failed to create epoll instance.");
+ THROW(ErrCode::RuntimeError) << "Failed to create epoll instance.";
}
Mainloop::~Mainloop()
std::lock_guard<Mutex> lock(mutex);
if (this->listener.find(fd) != this->listener.end())
- throw std::runtime_error("Event is already registered.");
+ THROW(ErrCode::RuntimeError) << "Event is already registered.";
::epoll_event event;
std::memset(&event, 0, sizeof(epoll_event));
event.data.fd = fd;
if (::epoll_ctl(this->epollFd, EPOLL_CTL_ADD, fd, &event) == -1)
- throw std::runtime_error("Failed to add event source.");
+ THROW(ErrCode::RuntimeError) << "Failed to add event source.";
auto onEventPtr = std::make_shared<OnEvent>(onEvent);
auto onErrorPtr = (onError != nullptr) ? std::make_shared<OnError>(onError) : nullptr;
#pragma once
#include <vist/archive.hpp>
+#include <vist/exception.hpp>
#include <vist/klass/function.hpp>
#include <functional>
#include <memory>
-#include <stdexcept>
#include <unordered_map>
namespace vist {
Functor<R, K, Ps...> make_functor(std::shared_ptr<K> instance, R (K::* member)(Ps...))
{
if (instance == nullptr)
- throw std::invalid_argument("Instance can't be nullptr.");
+ THROW(ErrCode::LogicError) << "Instance can't be nullptr.";
return Functor<R, K, Ps...>(instance, make_function(member));
}
R (K::* member)(Ps...))
{
if (instance == nullptr)
- throw std::invalid_argument("Instance can't be nullptr.");
+ THROW(ErrCode::LogicError) << "Instance can't be nullptr.";
std::shared_ptr<K> smartPtr(instance);
return std::make_shared<Functor<R, K, Ps...>>(smartPtr, make_function(member));
R (K::* member)(Ps...))
{
if (instance == nullptr)
- throw std::invalid_argument("Instance can't be nullptr.");
+ THROW(ErrCode::LogicError) << "Instance can't be nullptr.";
return std::make_shared<Functor<R, K, Ps...>>(instance, make_function(member));
}
#include "server.hpp"
+#include <vist/exception.hpp>
#include <vist/logger.hpp>
#include <vist/transport/message.hpp>
void Server::onAccept(std::shared_ptr<Connection>&& connection)
{
if (connection == nullptr)
- throw std::invalid_argument("Wrong connection.");
+ THROW(ErrCode::LogicError) << "Wrong connection.";
auto onRead = [this, connection]() {
std::shared_ptr<Connection> conn;
auto iter = this->connectionMap.find(connection->getFd());
if (iter == this->connectionMap.end())
- throw std::runtime_error("Faild to find connection.");
+ THROW(ErrCode::RuntimeError) << "Faild to find connection.";
conn = iter->second;
void Server::onClose(const std::shared_ptr<Connection>& connection)
{
if (connection == nullptr)
- throw std::invalid_argument("Wrong connection.");
+ THROW(ErrCode::LogicError) << "Wrong connection.";
{
std::lock_guard<std::mutex> lock(this->connectionMutex);
auto iter = this->connectionMap.find(connection->getFd());
if (iter == this->connectionMap.end())
- throw std::runtime_error("Faild to find connection.");
+ THROW(ErrCode::RuntimeError) << "Faild to find connection.";
this->mainloop.removeHandler(iter->first);
INFO(VIST) << "Connection is closed. fd: " << iter->first;
auto iter = this->functorMap.find(funcName);
if (iter == this->functorMap.end())
- throw std::runtime_error("Faild to find function.");
+ THROW(ErrCode::RuntimeError) << "Faild to find function.";
DEBUG(VIST) << "Remote method invokation: " << funcName;
#pragma once
+#include <vist/exception.hpp>
#include <vist/sdk/policy-value.hpp>
-#include <string>
#include <stdexcept>
+#include <string>
namespace vist {
namespace policy {
inline const PolicyValue& get() const
{
if (!ready)
- throw std::runtime_error("Policy value should be set once before use.");
+ THROW(ErrCode::RuntimeError) << "Policy value should be set once before use.";
return current;
}
void set_cloexec(int fd)
{
if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
- throw std::runtime_error("Failed to set CLOSEXEC.");
+ THROW(ErrCode::RuntimeError) << "Failed to set CLOSEXEC.";
}
} // anonymous namespace
Socket::Socket(const std::string& path)
{
if (path.size() >= sizeof(::sockaddr_un::sun_path))
- throw std::invalid_argument("Socket path size is wrong.");
+ THROW(ErrCode::LogicError) << "Socket path size is wrong.";
int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1)
- throw std::runtime_error("Failed to create socket.");
+ THROW(ErrCode::RuntimeError) << "Failed to create socket.";
set_cloexec(fd);
struct stat buf;
if (::stat(path.c_str(), &buf) == 0)
if (::unlink(path.c_str()) == -1)
- throw std::runtime_error("Failed to remove exist socket.");
+ THROW(ErrCode::RuntimeError) << "Failed to remove exist socket.";
if (::bind(fd, reinterpret_cast<::sockaddr*>(&addr), sizeof(::sockaddr_un)) == -1) {
::close(fd);
- throw std::runtime_error("Failed to bind.");
+ THROW(ErrCode::RuntimeError) << "Failed to bind.";
}
if (::listen(fd, MAX_BACKLOG_SIZE) == -1) {
::close(fd);
- throw std::runtime_error("Failed to liten.");
+ THROW(ErrCode::RuntimeError) << "Failed to listen.";
}
this->fd = fd;
{
int fd = ::accept(this->fd, nullptr, nullptr);
if (fd == -1)
- throw std::runtime_error("Failed to accept.");
+ THROW(ErrCode::RuntimeError) << "Failed to accept.";
set_cloexec(fd);
Socket Socket::connect(const std::string& path)
{
if (path.size() >= sizeof(::sockaddr_un::sun_path))
- throw std::invalid_argument("Socket path size is wrong.");
+ THROW(ErrCode::LogicError) << "Socket path size is wrong.";
int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1)
- throw std::runtime_error("Failed to create socket.");
+ THROW(ErrCode::RuntimeError) << "Failed to create socket.";
set_cloexec(fd);
if (::connect(fd, reinterpret_cast<::sockaddr*>(&addr), sizeof(sockaddr_un)) == -1) {
::close(fd);
- throw std::runtime_error("Failed to connect.");
+ THROW(ErrCode::RuntimeError) << "Failed to connect.";
}
return Socket(fd);
#pragma once
+#include <vist/exception.hpp>
+
#include <errno.h>
#include <unistd.h>
#include <cstddef>
-#include <stdexcept>
#include <string>
namespace vist {
else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
continue;
else
- std::runtime_error("Failed to write.");
+ THROW(ErrCode::RuntimeError) << "Failed to write to socket.";
}
}