node.native
node.native copied to clipboard
to integrate new lib uv (new error checking mechannizm)
I have tried to update the libuv and found out that just now there was a major change in libuv.
https://github.com/joyent/libuv/commit/3ee4d3f183331a123ce35edd0d32268a2bb22aa5
https://github.com/joyent/libuv/issues/696
TODO: look into updating libuv dependency version
I've been looking at this. I think the error class gets simpler because there are fewer types to handle.
I've drafted out error.h as:
#ifndef __ERROR_H__
#define __ERROR_H__
#include "base.h"
namespace native
{
class exception
{
public:
exception(const std::string& message)
: message_(message)
{}
virtual ~exception() {}
const std::string& message() const { return message_; }
private:
std::string message_;
};
class error
{
public:
error() : uv_err_(0) {}
error(int e) : uv_err_(e) {}
~error() = default;
public:
operator bool() { return uv_err_ != 0; } // Is 0 the right number for no error in UV?
int code() const { return uv_err_; }
const char* name() const { return uv_err_name(uv_err_); }
const char* str() const { return uv_strerror(uv_err_); }
private:
int uv_err_;
};
// uv no longer has uv_last_error. Instead use return from the uv call.
//error get_last_error() { return uv_last_error(uv_default_loop()); }
}
#endif
But I don't know how to modify the calling code, such as tcp.h, fs.h and so on. Any hints from the original coder?
For example, here's a call to uv_tcp_connect from tcp.h:
bool connect(const std::string& ip, int port, std::function<void(error)> callback)
{
callbacks::store(get()->data, native::internal::uv_cid_connect, callback);
return uv_tcp_connect(new uv_connect_t, get<uv_tcp_t>(), to_ip4_addr(ip, port), [](uv_connect_t* req, int status) {
callbacks::invoke<decltype(callback)>(req->handle->data, native::internal::uv_cid_connect, status?uv_last_error(req->handle->loop):error());
delete req;
}) == 0;
}
Previously you had to call uv_last_error to get the last error code, if any. Now, uv_tcp_connect returns the error directly, as an int. So what about the handling of "status" in the callback? I can't see how to do the right thing here.