From b2535d79fffb1cacd6d9b013ad5fea3b0284ae47 Mon Sep 17 00:00:00 2001 From: weizili Date: Sun, 28 May 2017 13:53:35 +0800 Subject: [PATCH] Fix windows-x64 platform compile issues --- benchmark/ioevent/evpp/evpp_ioevent_bench.cc | 4 ++-- .../fd_channel_vs_pipe_event_watcher.cc | 6 ++--- evpp/buffer.cc | 2 +- evpp/buffer.h | 2 +- evpp/connector.h | 4 ++-- evpp/event_watcher.cc | 10 ++++---- evpp/event_watcher.h | 14 +++++------ evpp/fd_channel.cc | 6 ++--- evpp/fd_channel.h | 10 ++++---- evpp/libevent.cc | 2 +- evpp/libevent.h | 2 +- evpp/listener.h | 4 ++-- evpp/sockets.cc | 24 +++++++++---------- evpp/sockets.h | 21 ++++++++-------- evpp/sys_sockets.h | 9 +++++++ evpp/tcp_client.cc | 2 +- evpp/tcp_client.h | 2 +- evpp/tcp_conn.cc | 2 +- evpp/tcp_conn.h | 4 ++-- evpp/tcp_server.cc | 2 +- evpp/tcp_server.h | 2 +- evpp/udp/sync_udp_client.h | 2 +- evpp/udp/udp_message.h | 10 ++++---- evpp/udp/udp_server.cc | 2 +- .../basic_01/event_watcher.cc | 6 +---- .../basic_01/event_watcher.h | 6 ++++- .../basic_02/event_watcher.cc | 6 +---- .../basic_02/event_watcher.h | 6 ++++- .../cancel_03/event_watcher.cc | 8 ++----- .../cancel_03/event_watcher.h | 10 +++++--- .../periodic_04/event_watcher.cc | 8 ++----- .../periodic_04/event_watcher.h | 10 +++++--- test/event_watcher_test.cc | 2 +- 33 files changed, 110 insertions(+), 100 deletions(-) diff --git a/benchmark/ioevent/evpp/evpp_ioevent_bench.cc b/benchmark/ioevent/evpp/evpp_ioevent_bench.cc index 0c31a073b..d36c67414 100644 --- a/benchmark/ioevent/evpp/evpp_ioevent_bench.cc +++ b/benchmark/ioevent/evpp/evpp_ioevent_bench.cc @@ -20,7 +20,7 @@ using namespace evpp; -std::vector g_pipes; +std::vector g_pipes; int numPipes; int numActive; int numWrites; @@ -30,7 +30,7 @@ std::vector g_channels; int g_reads, g_writes, g_fired; int g_total_reads = 0; -void readCallback(int fd, int idx) { +void readCallback(evpp_socket_t fd, int idx) { g_total_reads++; char ch = 0; g_reads += static_cast(::recv(fd, &ch, sizeof(ch), 0)); diff --git a/benchmark/ioevent/fd_channel_vs_pipe_event_watcher/fd_channel_vs_pipe_event_watcher.cc b/benchmark/ioevent/fd_channel_vs_pipe_event_watcher/fd_channel_vs_pipe_event_watcher.cc index 05e4daf16..a3ca55f8a 100644 --- a/benchmark/ioevent/fd_channel_vs_pipe_event_watcher/fd_channel_vs_pipe_event_watcher.cc +++ b/benchmark/ioevent/fd_channel_vs_pipe_event_watcher/fd_channel_vs_pipe_event_watcher.cc @@ -18,7 +18,7 @@ using namespace evpp; -std::vector g_pipes; +std::vector g_pipes; int numPipes; int numActive; int numWrites; @@ -28,7 +28,7 @@ std::vector> g_pipe_event_watchers; int g_reads, g_writes, g_fired; -void ReadCallbackOfFdChannel(int fd, int idx) { +void ReadCallbackOfFdChannel(evpp_socket_t fd, int idx) { char ch = 'm'; g_reads += static_cast(::recv(fd, &ch, sizeof(ch), 0)); @@ -88,7 +88,7 @@ std::pair FdChannelRunOnce() { std::pair PipeEventWatcherRunOnce() { Timestamp beforeInit(Timestamp::Now()); for (int i = 0; i < numActive; ++i) { - int fd = g_pipe_event_watchers[i]->wfd(); + evpp_socket_t fd = g_pipe_event_watchers[i]->wfd(); ::send(fd, "m", 1, 0); } diff --git a/evpp/buffer.cc b/evpp/buffer.cc index fdfced362..077c2ebfa 100644 --- a/evpp/buffer.cc +++ b/evpp/buffer.cc @@ -19,7 +19,7 @@ const char Buffer::kCRLF[] = "\r\n"; const size_t Buffer::kCheapPrependSize = 8; const size_t Buffer::kInitialSize = 1024; -ssize_t Buffer::ReadFromFD(int fd, int* savedErrno) { +ssize_t Buffer::ReadFromFD(evpp_socket_t fd, int* savedErrno) { // saved an ioctl()/FIONREAD call to tell how much to read char extrabuf[65536]; struct iovec vec[2]; diff --git a/evpp/buffer.h b/evpp/buffer.h index f4c33d019..3c364bf29 100644 --- a/evpp/buffer.h +++ b/evpp/buffer.h @@ -235,7 +235,7 @@ class EVPP_EXPORT Buffer { // ReadFromFD reads data from a fd directly into buffer, // and return result of readv, errno is saved into saved_errno - ssize_t ReadFromFD(int fd, int* saved_errno); + ssize_t ReadFromFD(evpp_socket_t fd, int* saved_errno); // Next returns a slice containing the next n bytes from the buffer, // advancing the buffer as if the bytes had been returned by Read. diff --git a/evpp/connector.h b/evpp/connector.h index 091022823..d3c058f92 100644 --- a/evpp/connector.h +++ b/evpp/connector.h @@ -13,7 +13,7 @@ class DNSResolver; class TCPClient; class EVPP_EXPORT Connector : public std::enable_shared_from_this { public: - typedef std::function NewConnectionCallback; + typedef std::function NewConnectionCallback; Connector(EventLoop* loop, TCPClient* client); ~Connector(); void Start(); @@ -54,7 +54,7 @@ class EVPP_EXPORT Connector : public std::enable_shared_from_this { Duration timeout_; - int fd_ = -1; + evpp_socket_t fd_ = -1; // A flag indicate whether the Connector owns this fd. // If the Connector owns this fd, the Connector has responsibility to close this fd. diff --git a/evpp/event_watcher.cc b/evpp/event_watcher.cc index 4193384d8..6f4cb69aa 100644 --- a/evpp/event_watcher.cc +++ b/evpp/event_watcher.cc @@ -149,7 +149,7 @@ void PipeEventWatcher::DoClose() { } } -void PipeEventWatcher::HandlerFn(int /*fd*/, short /*which*/, void* v) { +void PipeEventWatcher::HandlerFn(evpp_socket_t /*fd*/, short /*which*/, void* v) { PipeEventWatcher* e = (PipeEventWatcher*)v; #ifdef H_BENCHMARK_TESTING // Every time we only read 1 byte for testing the IO event performance. @@ -213,7 +213,7 @@ bool TimerEventWatcher::DoInit() { return true; } -void TimerEventWatcher::HandlerFn(int /*fd*/, short /*which*/, void* v) { +void TimerEventWatcher::HandlerFn(evpp_socket_t /*fd*/, short /*which*/, void* v) { TimerEventWatcher* h = (TimerEventWatcher*)v; h->handler_(); } @@ -226,14 +226,14 @@ bool TimerEventWatcher::AsyncWait() { ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// -SignalEventWatcher::SignalEventWatcher(int signo, EventLoop* loop, +SignalEventWatcher::SignalEventWatcher(signal_number_t signo, EventLoop* loop, const Handler& handler) : EventWatcher(loop->event_base(), handler) , signo_(signo) { assert(signo_); } -SignalEventWatcher::SignalEventWatcher(int signo, EventLoop* loop, +SignalEventWatcher::SignalEventWatcher(signal_number_t signo, EventLoop* loop, Handler&& h) : EventWatcher(loop->event_base(), std::move(h)) , signo_(signo) { @@ -246,7 +246,7 @@ bool SignalEventWatcher::DoInit() { return true; } -void SignalEventWatcher::HandlerFn(int /*sn*/, short /*which*/, void* v) { +void SignalEventWatcher::HandlerFn(signal_number_t /*sn*/, short /*which*/, void* v) { SignalEventWatcher* h = (SignalEventWatcher*)v; h->handler_(); } diff --git a/evpp/event_watcher.h b/evpp/event_watcher.h index 0c1e16abe..dcc2fdd1b 100644 --- a/evpp/event_watcher.h +++ b/evpp/event_watcher.h @@ -57,13 +57,13 @@ class EVPP_EXPORT PipeEventWatcher : public EventWatcher { bool AsyncWait(); void Notify(); - int wfd() const { return pipe_[0]; } + evpp_socket_t wfd() const { return pipe_[0]; } private: virtual bool DoInit(); virtual void DoClose(); - static void HandlerFn(int fd, short which, void* v); + static void HandlerFn(evpp_socket_t fd, short which, void* v); - int pipe_[2]; // Write to pipe_[0] , Read from pipe_[1] + evpp_socket_t pipe_[2]; // Write to pipe_[0] , Read from pipe_[1] }; class EVPP_EXPORT TimerEventWatcher : public EventWatcher { @@ -77,20 +77,20 @@ class EVPP_EXPORT TimerEventWatcher : public EventWatcher { private: virtual bool DoInit(); - static void HandlerFn(int fd, short which, void* v); + static void HandlerFn(evpp_socket_t fd, short which, void* v); private: Duration timeout_; }; class EVPP_EXPORT SignalEventWatcher : public EventWatcher { public: - SignalEventWatcher(int signo, EventLoop* loop, const Handler& handler); - SignalEventWatcher(int signo, EventLoop* loop, Handler&& handler); + SignalEventWatcher(signal_number_t signo, EventLoop* loop, const Handler& handler); + SignalEventWatcher(signal_number_t signo, EventLoop* loop, Handler&& handler); bool AsyncWait(); private: virtual bool DoInit(); - static void HandlerFn(int sn, short which, void* v); + static void HandlerFn(signal_number_t sn, short which, void* v); int signo_; }; diff --git a/evpp/fd_channel.cc b/evpp/fd_channel.cc index 9ee65a18e..a2748a2d2 100644 --- a/evpp/fd_channel.cc +++ b/evpp/fd_channel.cc @@ -10,7 +10,7 @@ namespace evpp { static_assert(FdChannel::kReadable == EV_READ, ""); static_assert(FdChannel::kWritable == EV_WRITE, ""); -FdChannel::FdChannel(EventLoop* l, int f, bool r, bool w) +FdChannel::FdChannel(EventLoop* l, evpp_socket_t f, bool r, bool w) : loop_(l), attached_(false), event_(nullptr), fd_(f) { DLOG_TRACE << "fd=" << fd_; assert(fd_ > 0); @@ -147,12 +147,12 @@ std::string FdChannel::EventsToString() const { return s; } -void FdChannel::HandleEvent(int sockfd, short which, void* v) { +void FdChannel::HandleEvent(evpp_socket_t sockfd, short which, void* v) { FdChannel* c = (FdChannel*)v; c->HandleEvent(sockfd, which); } -void FdChannel::HandleEvent(int sockfd, short which) { +void FdChannel::HandleEvent(evpp_socket_t sockfd, short which) { assert(sockfd == fd_); DLOG_TRACE << "fd=" << sockfd << " " << EventsToString(); diff --git a/evpp/fd_channel.h b/evpp/fd_channel.h index 57cf0f07c..e6c121351 100644 --- a/evpp/fd_channel.h +++ b/evpp/fd_channel.h @@ -25,7 +25,7 @@ class EVPP_EXPORT FdChannel { typedef std::function ReadEventCallback; public: - FdChannel(EventLoop* loop, int fd, + FdChannel(EventLoop* loop, evpp_socket_t fd, bool watch_read_event, bool watch_write_event); ~FdChannel(); @@ -56,7 +56,7 @@ class EVPP_EXPORT FdChannel { void DisableAllEvent(); public: - int fd() const { + evpp_socket_t fd() const { return fd_; } std::string EventsToString() const; @@ -71,8 +71,8 @@ class EVPP_EXPORT FdChannel { } private: - void HandleEvent(int fd, short which); - static void HandleEvent(int fd, short which, void* v); + void HandleEvent(evpp_socket_t fd, short which); + static void HandleEvent(evpp_socket_t fd, short which, void* v); void Update(); void DetachFromLoop(); @@ -86,7 +86,7 @@ class EVPP_EXPORT FdChannel { struct event* event_; int events_; // the bitwise OR of zero or more of the EventType flags - int fd_; + evpp_socket_t fd_; }; } diff --git a/evpp/libevent.cc b/evpp/libevent.cc index 807a253e9..6ad9a6630 100644 --- a/evpp/libevent.cc +++ b/evpp/libevent.cc @@ -2,7 +2,7 @@ #include "evpp/libevent.h" #ifdef H_LIBEVENT_VERSION_14 -struct event* event_new(struct event_base* base, int fd, short events, +struct event* event_new(struct event_base* base, evpp_socket_t fd, short events, void(*cb)(int, short, void*), void* arg) { struct event* ev; ev = (struct event*)malloc(sizeof(struct event)); diff --git a/evpp/libevent.h b/evpp/libevent.h index 9a7c2a4a9..cdd1d59d6 100644 --- a/evpp/libevent.h +++ b/evpp/libevent.h @@ -49,7 +49,7 @@ #ifdef H_LIBEVENT_VERSION_14 extern "C" { struct evdns_base; - EVPP_EXPORT struct event* event_new(struct event_base* base, int fd, short events, void(*cb)(int, short, void*), void* arg); + EVPP_EXPORT struct event* event_new(struct event_base* base, evpp_socket_t fd, short events, void(*cb)(int, short, void*), void* arg); EVPP_EXPORT void event_free(struct event* ev); EVPP_EXPORT evhttp_connection* evhttp_connection_base_new( struct event_base* base, struct evdns_base* dnsbase, diff --git a/evpp/listener.h b/evpp/listener.h index a489dc415..0557d3054 100644 --- a/evpp/listener.h +++ b/evpp/listener.h @@ -10,7 +10,7 @@ class FdChannel; class EVPP_EXPORT Listener { public: typedef std::function < - void(int sockfd, + void(evpp_socket_t sockfd, const std::string& /*remote address with format "ip:port"*/, const struct sockaddr_in* /*remote address*/) > NewConnectionCallback; @@ -33,7 +33,7 @@ class EVPP_EXPORT Listener { void HandleAccept(); private: - int fd_ = -1;// The listening socket fd + evpp_socket_t fd_ = -1;// The listening socket fd EventLoop* loop_; std::string addr_; std::unique_ptr chan_; diff --git a/evpp/sockets.cc b/evpp/sockets.cc index 1cdf5d250..333c05c0f 100644 --- a/evpp/sockets.cc +++ b/evpp/sockets.cc @@ -33,11 +33,11 @@ std::string strerror(int e) { } namespace sock { -int CreateNonblockingSocket() { +evpp_socket_t CreateNonblockingSocket() { int serrno = 0; /* Create listen socket */ - int fd = ::socket(AF_INET, SOCK_STREAM, 0); + evpp_socket_t fd = ::socket(AF_INET, SOCK_STREAM, 0); if (fd == -1) { serrno = errno; LOG_ERROR << "socket error " << strerror(serrno); @@ -65,8 +65,8 @@ int CreateNonblockingSocket() { return INVALID_SOCKET; } -int CreateUDPServer(int port) { - int fd = ::socket(AF_INET, SOCK_DGRAM, 0); +evpp_socket_t CreateUDPServer(int port) { + evpp_socket_t fd = ::socket(AF_INET, SOCK_DGRAM, 0); if (fd == -1) { int serrno = errno; LOG_ERROR << "socket error " << strerror(serrno); @@ -158,7 +158,7 @@ bool SplitHostPort(const char* address, std::string& host, int& port) { return true; } -struct sockaddr_storage GetLocalAddr(int sockfd) { +struct sockaddr_storage GetLocalAddr(evpp_socket_t sockfd) { struct sockaddr_storage laddr; memset(&laddr, 0, sizeof laddr); socklen_t addrlen = static_cast(sizeof laddr); @@ -237,7 +237,7 @@ std::string ToIP(const struct sockaddr* s) { return empty_string; } -void SetTimeout(int fd, uint32_t timeout_ms) { +void SetTimeout(evpp_socket_t fd, uint32_t timeout_ms) { #ifdef H_OS_WINDOWS DWORD tv = timeout_ms; #else @@ -253,11 +253,11 @@ void SetTimeout(int fd, uint32_t timeout_ms) { } } -void SetTimeout(int fd, const Duration& timeout) { +void SetTimeout(evpp_socket_t fd, const Duration& timeout) { SetTimeout(fd, (uint32_t)(timeout.Milliseconds())); } -void SetKeepAlive(int fd, bool on) { +void SetKeepAlive(evpp_socket_t fd, bool on) { int optval = on ? 1 : 0; int rc = ::setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast(&optval), static_cast(sizeof optval)); @@ -267,7 +267,7 @@ void SetKeepAlive(int fd, bool on) { } } -void SetReuseAddr(int fd) { +void SetReuseAddr(evpp_socket_t fd) { int optval = 1; int rc = ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&optval), static_cast(sizeof optval)); @@ -277,7 +277,7 @@ void SetReuseAddr(int fd) { } } -void SetReusePort(int fd) { +void SetReusePort(evpp_socket_t fd) { #ifdef SO_REUSEPORT int optval = 1; int rc = ::setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, @@ -290,7 +290,7 @@ void SetReusePort(int fd) { } -void SetTCPNoDelay(int fd, bool on) { +void SetTCPNoDelay(evpp_socket_t fd, bool on) { int optval = on ? 1 : 0; int rc = ::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&optval), static_cast(sizeof optval)); @@ -304,7 +304,7 @@ void SetTCPNoDelay(int fd, bool on) { } #ifdef H_OS_WINDOWS -int readv(int sockfd, struct iovec* iov, int iovcnt) { +int readv(evpp_socket_t sockfd, struct iovec* iov, int iovcnt) { DWORD readn = 0; DWORD flags = 0; diff --git a/evpp/sockets.h b/evpp/sockets.h index bd6aa5ab1..ef003e4e3 100644 --- a/evpp/sockets.h +++ b/evpp/sockets.h @@ -1,6 +1,7 @@ #pragma once #include "evpp/sys_addrinfo.h" +#include "evpp/sys_sockets.h" #include @@ -12,14 +13,14 @@ EVPP_EXPORT std::string strerror(int e); namespace sock { -EVPP_EXPORT int CreateNonblockingSocket(); -EVPP_EXPORT int CreateUDPServer(int port); -EVPP_EXPORT void SetKeepAlive(int fd, bool on); -EVPP_EXPORT void SetReuseAddr(int fd); -EVPP_EXPORT void SetReusePort(int fd); -EVPP_EXPORT void SetTCPNoDelay(int fd, bool on); -EVPP_EXPORT void SetTimeout(int fd, uint32_t timeout_ms); -EVPP_EXPORT void SetTimeout(int fd, const Duration& timeout); +EVPP_EXPORT evpp_socket_t CreateNonblockingSocket(); +EVPP_EXPORT evpp_socket_t CreateUDPServer(int port); +EVPP_EXPORT void SetKeepAlive(evpp_socket_t fd, bool on); +EVPP_EXPORT void SetReuseAddr(evpp_socket_t fd); +EVPP_EXPORT void SetReusePort(evpp_socket_t fd); +EVPP_EXPORT void SetTCPNoDelay(evpp_socket_t fd, bool on); +EVPP_EXPORT void SetTimeout(evpp_socket_t fd, uint32_t timeout_ms); +EVPP_EXPORT void SetTimeout(evpp_socket_t fd, const Duration& timeout); EVPP_EXPORT std::string ToIPPort(const struct sockaddr_storage* ss); EVPP_EXPORT std::string ToIPPort(const struct sockaddr* ss); EVPP_EXPORT std::string ToIPPort(const struct sockaddr_in* ss); @@ -47,7 +48,7 @@ inline struct sockaddr_storage ParseFromIPPort(const char* address) { // @return bool - false if the network address is invalid format EVPP_EXPORT bool SplitHostPort(const char* address, std::string& host, int& port); -EVPP_EXPORT struct sockaddr_storage GetLocalAddr(int sockfd); +EVPP_EXPORT struct sockaddr_storage GetLocalAddr(evpp_socket_t sockfd); inline bool IsZeroAddress(const struct sockaddr_storage* ss) { const char* p = reinterpret_cast(ss); @@ -117,5 +118,5 @@ inline const struct sockaddr_storage* sockaddr_storage_cast(const struct sockadd } #ifdef H_OS_WINDOWS -EVPP_EXPORT int readv(int sockfd, struct iovec* iov, int iovcnt); +EVPP_EXPORT int readv(evpp_socket_t sockfd, struct iovec* iov, int iovcnt); #endif diff --git a/evpp/sys_sockets.h b/evpp/sys_sockets.h index 753c468db..3a7bdd160 100644 --- a/evpp/sys_sockets.h +++ b/evpp/sys_sockets.h @@ -137,3 +137,12 @@ typedef int ssize_t; ((e) == ECONNREFUSED) #endif + + +#ifdef H_OS_WINDOWS +#define evpp_socket_t intptr_t +#else +#define evpp_socket_t int +#endif + +#define signal_number_t evpp_socket_t \ No newline at end of file diff --git a/evpp/tcp_client.cc b/evpp/tcp_client.cc index 1013d6bfd..0ac3b137a 100644 --- a/evpp/tcp_client.cc +++ b/evpp/tcp_client.cc @@ -94,7 +94,7 @@ void TCPClient::SetConnectionCallback(const ConnectionCallback& cb) { } } -void TCPClient::OnConnection(int sockfd, const std::string& laddr) { +void TCPClient::OnConnection(evpp_socket_t sockfd, const std::string& laddr) { if (sockfd < 0) { DLOG_TRACE << "Failed to connect to " << remote_addr_ << ". errno=" << errno << " " << strerror(errno); // We need to notify this failure event to the user layer diff --git a/evpp/tcp_client.h b/evpp/tcp_client.h index c794ae442..f265ac1cd 100644 --- a/evpp/tcp_client.h +++ b/evpp/tcp_client.h @@ -95,7 +95,7 @@ class EVPP_EXPORT TCPClient { } private: void DisconnectInLoop(); - void OnConnection(int sockfd, const std::string& laddr); + void OnConnection(evpp_socket_t sockfd, const std::string& laddr); void OnRemoveConnection(const TCPConnPtr& conn); void Reconnect(); private: diff --git a/evpp/tcp_conn.cc b/evpp/tcp_conn.cc index 28482696d..b7678d0c5 100644 --- a/evpp/tcp_conn.cc +++ b/evpp/tcp_conn.cc @@ -11,7 +11,7 @@ namespace evpp { TCPConn::TCPConn(EventLoop* l, const std::string& n, - int sockfd, + evpp_socket_t sockfd, const std::string& laddr, const std::string& raddr, uint64_t conn_id) diff --git a/evpp/tcp_conn.h b/evpp/tcp_conn.h index 49e8350ed..a069cafe8 100644 --- a/evpp/tcp_conn.h +++ b/evpp/tcp_conn.h @@ -31,7 +31,7 @@ class EVPP_EXPORT TCPConn : public std::enable_shared_from_this { public: TCPConn(EventLoop* loop, const std::string& name, - int sockfd, + evpp_socket_t sockfd, const std::string& laddr, const std::string& raddr, uint64_t id); @@ -50,7 +50,7 @@ class EVPP_EXPORT TCPConn : public std::enable_shared_from_this { EventLoop* loop() const { return loop_; } - int fd() const { + evpp_socket_t fd() const { return fd_; } uint64_t id() const { diff --git a/evpp/tcp_server.cc b/evpp/tcp_server.cc index 444520c29..ec87f2389 100644 --- a/evpp/tcp_server.cc +++ b/evpp/tcp_server.cc @@ -124,7 +124,7 @@ void TCPServer::StopThreadPool() { substatus_.store(kSubStatusNull); } -void TCPServer::HandleNewConn(int sockfd, +void TCPServer::HandleNewConn(evpp_socket_t sockfd, const std::string& remote_addr/*ip:port*/, const struct sockaddr_in* raddr) { DLOG_TRACE << "fd=" << sockfd; diff --git a/evpp/tcp_server.h b/evpp/tcp_server.h index be69580e7..794f02692 100644 --- a/evpp/tcp_server.h +++ b/evpp/tcp_server.h @@ -99,7 +99,7 @@ class EVPP_EXPORT TCPServer : public ThreadDispatchPolicy, public ServerStatus { void StopThreadPool(); void StopInLoop(DoneCallback on_stopped_cb); void RemoveConnection(const TCPConnPtr& conn); - void HandleNewConn(int sockfd, const std::string& remote_addr/*ip:port*/, const struct sockaddr_in* raddr); + void HandleNewConn(evpp_socket_t sockfd, const std::string& remote_addr/*ip:port*/, const struct sockaddr_in* raddr); EventLoop* GetNextLoop(const struct sockaddr_in* raddr); private: EventLoop* loop_; // the listening loop diff --git a/evpp/udp/sync_udp_client.h b/evpp/udp/sync_udp_client.h index bba399040..c9ac4e0d2 100644 --- a/evpp/udp/sync_udp_client.h +++ b/evpp/udp/sync_udp_client.h @@ -36,7 +36,7 @@ class EVPP_EXPORT Client { static bool Send(const MessagePtr& msg); static bool Send(const Message* msg); public: - int sockfd() const { + evpp_socket_t sockfd() const { return sockfd_; } private: diff --git a/evpp/udp/udp_message.h b/evpp/udp/udp_message.h index f4d86fb4b..0ff9df954 100644 --- a/evpp/udp/udp_message.h +++ b/evpp/udp/udp_message.h @@ -8,7 +8,7 @@ namespace evpp { namespace udp { class EVPP_EXPORT Message : public Buffer { public: - Message(int fd, size_t buffer_size = 1472) + Message(evpp_socket_t fd, size_t buffer_size = 1472) : Buffer(buffer_size), sockfd_(fd) { memset(&remote_addr_, 0, sizeof(remote_addr_)); } @@ -20,7 +20,7 @@ class EVPP_EXPORT Message : public Buffer { } std::string remote_ip() const; - int sockfd() const { + evpp_socket_t sockfd() const { return sockfd_; } private: @@ -41,7 +41,7 @@ inline std::string Message::remote_ip() const { return sock::ToIP(remote_addr()); } -inline bool SendMessage(int fd, const struct sockaddr* addr, const char* d, size_t dlen) { +inline bool SendMessage(evpp_socket_t fd, const struct sockaddr* addr, const char* d, size_t dlen) { if (dlen == 0) { return true; } @@ -54,11 +54,11 @@ inline bool SendMessage(int fd, const struct sockaddr* addr, const char* d, size return true; } -inline bool SendMessage(int fd, const struct sockaddr* addr, const std::string& d) { +inline bool SendMessage(evpp_socket_t fd, const struct sockaddr* addr, const std::string& d) { return SendMessage(fd, addr, d.data(), d.size()); } -inline bool SendMessage(int fd, const struct sockaddr* addr, const Slice& d) { +inline bool SendMessage(evpp_socket_t fd, const struct sockaddr* addr, const Slice& d) { return SendMessage(fd, addr, d.data(), d.size()); } diff --git a/evpp/udp/udp_server.cc b/evpp/udp/udp_server.cc index a173453e5..d864f49e8 100644 --- a/evpp/udp/udp_server.cc +++ b/evpp/udp/udp_server.cc @@ -81,7 +81,7 @@ class Server::RecvThread { status_ = s; } - int fd() const { + evpp_socket_t fd() const { return fd_; } diff --git a/examples/recipes/self_control_timer/basic_01/event_watcher.cc b/examples/recipes/self_control_timer/basic_01/event_watcher.cc index cbc2422ab..2e6f7c74c 100644 --- a/examples/recipes/self_control_timer/basic_01/event_watcher.cc +++ b/examples/recipes/self_control_timer/basic_01/event_watcher.cc @@ -1,10 +1,6 @@ #include #include -#include -#include -#include - #include #include "event_watcher.h" @@ -106,7 +102,7 @@ bool TimerEventWatcher::DoInit() { return true; } -void TimerEventWatcher::HandlerFn(int /*fd*/, short /*which*/, void* v) { +void TimerEventWatcher::HandlerFn(evutil_socket_t /*fd*/, short /*which*/, void* v) { TimerEventWatcher* h = (TimerEventWatcher*)v; h->handler_(); } diff --git a/examples/recipes/self_control_timer/basic_01/event_watcher.h b/examples/recipes/self_control_timer/basic_01/event_watcher.h index b5be1c25d..7f9e38536 100644 --- a/examples/recipes/self_control_timer/basic_01/event_watcher.h +++ b/examples/recipes/self_control_timer/basic_01/event_watcher.h @@ -1,5 +1,9 @@ #pragma once +#include +#include +#include + #include struct event; @@ -41,7 +45,7 @@ class TimerEventWatcher : public EventWatcher { private: virtual bool DoInit(); - static void HandlerFn(int fd, short which, void* v); + static void HandlerFn(evutil_socket_t fd, short which, void* v); private: double timeout_ms_; }; diff --git a/examples/recipes/self_control_timer/basic_02/event_watcher.cc b/examples/recipes/self_control_timer/basic_02/event_watcher.cc index 4aea094a8..6eb763045 100644 --- a/examples/recipes/self_control_timer/basic_02/event_watcher.cc +++ b/examples/recipes/self_control_timer/basic_02/event_watcher.cc @@ -1,10 +1,6 @@ #include #include -#include -#include -#include - #include #include "event_watcher.h" @@ -106,7 +102,7 @@ namespace recipes { return true; } - void TimerEventWatcher::HandlerFn(int /*fd*/, short /*which*/, void* v) { + void TimerEventWatcher::HandlerFn(evutil_socket_t /*fd*/, short /*which*/, void* v) { TimerEventWatcher* h = (TimerEventWatcher*)v; h->handler_(); } diff --git a/examples/recipes/self_control_timer/basic_02/event_watcher.h b/examples/recipes/self_control_timer/basic_02/event_watcher.h index 66988c4dd..9ca2ba308 100644 --- a/examples/recipes/self_control_timer/basic_02/event_watcher.h +++ b/examples/recipes/self_control_timer/basic_02/event_watcher.h @@ -1,5 +1,9 @@ #pragma once +#include +#include +#include + #include struct event; @@ -41,7 +45,7 @@ namespace recipes { private: virtual bool DoInit(); - static void HandlerFn(int fd, short which, void* v); + static void HandlerFn(evutil_socket_t fd, short which, void* v); private: double timeout_ms_; }; diff --git a/examples/recipes/self_control_timer/cancel_03/event_watcher.cc b/examples/recipes/self_control_timer/cancel_03/event_watcher.cc index f29a3db96..aa0bd815d 100644 --- a/examples/recipes/self_control_timer/cancel_03/event_watcher.cc +++ b/examples/recipes/self_control_timer/cancel_03/event_watcher.cc @@ -1,10 +1,6 @@ #include #include -#include -#include -#include - #include @@ -140,7 +136,7 @@ void PipeEventWatcher::DoClose() { } } -void PipeEventWatcher::HandlerFn(int /*fd*/, short /*which*/, void* v) { +void PipeEventWatcher::HandlerFn(evutil_socket_t /*fd*/, short /*which*/, void* v) { PipeEventWatcher* e = (PipeEventWatcher*)v; char buf[128]; int n = 0; @@ -177,7 +173,7 @@ bool TimerEventWatcher::DoInit() { return true; } -void TimerEventWatcher::HandlerFn(int /*fd*/, short /*which*/, void* v) { +void TimerEventWatcher::HandlerFn(evutil_socket_t /*fd*/, short /*which*/, void* v) { TimerEventWatcher* h = (TimerEventWatcher*)v; h->handler_(); } diff --git a/examples/recipes/self_control_timer/cancel_03/event_watcher.h b/examples/recipes/self_control_timer/cancel_03/event_watcher.h index ac1c8428f..62715b49c 100644 --- a/examples/recipes/self_control_timer/cancel_03/event_watcher.h +++ b/examples/recipes/self_control_timer/cancel_03/event_watcher.h @@ -1,5 +1,9 @@ #pragma once +#include +#include +#include + #include struct event; @@ -56,9 +60,9 @@ class PipeEventWatcher : public EventWatcher { private: virtual bool DoInit(); virtual void DoClose(); - static void HandlerFn(int fd, short which, void* v); + static void HandlerFn(evutil_socket_t fd, short which, void* v); - int pipe_[2]; // Write to pipe_[0] , Read from pipe_[1] + evutil_socket_t pipe_[2]; // Write to pipe_[0] , Read from pipe_[1] }; class TimerEventWatcher : public EventWatcher { @@ -69,7 +73,7 @@ class TimerEventWatcher : public EventWatcher { private: virtual bool DoInit(); - static void HandlerFn(int fd, short which, void* v); + static void HandlerFn(evutil_socket_t fd, short which, void* v); private: double timeout_ms_; }; diff --git a/examples/recipes/self_control_timer/periodic_04/event_watcher.cc b/examples/recipes/self_control_timer/periodic_04/event_watcher.cc index b810a8711..e68c4e41e 100644 --- a/examples/recipes/self_control_timer/periodic_04/event_watcher.cc +++ b/examples/recipes/self_control_timer/periodic_04/event_watcher.cc @@ -1,10 +1,6 @@ #include #include -#include -#include -#include - #include @@ -141,7 +137,7 @@ void PipeEventWatcher::DoClose() { } } -void PipeEventWatcher::HandlerFn(int /*fd*/, short /*which*/, void* v) { +void PipeEventWatcher::HandlerFn(evutil_socket_t /*fd*/, short /*which*/, void* v) { PipeEventWatcher* e = (PipeEventWatcher*)v; char buf[128]; int n = 0; @@ -178,7 +174,7 @@ bool TimerEventWatcher::DoInit() { return true; } -void TimerEventWatcher::HandlerFn(int /*fd*/, short /*which*/, void* v) { +void TimerEventWatcher::HandlerFn(evutil_socket_t /*fd*/, short /*which*/, void* v) { TimerEventWatcher* h = (TimerEventWatcher*)v; h->handler_(); } diff --git a/examples/recipes/self_control_timer/periodic_04/event_watcher.h b/examples/recipes/self_control_timer/periodic_04/event_watcher.h index ac1c8428f..62715b49c 100644 --- a/examples/recipes/self_control_timer/periodic_04/event_watcher.h +++ b/examples/recipes/self_control_timer/periodic_04/event_watcher.h @@ -1,5 +1,9 @@ #pragma once +#include +#include +#include + #include struct event; @@ -56,9 +60,9 @@ class PipeEventWatcher : public EventWatcher { private: virtual bool DoInit(); virtual void DoClose(); - static void HandlerFn(int fd, short which, void* v); + static void HandlerFn(evutil_socket_t fd, short which, void* v); - int pipe_[2]; // Write to pipe_[0] , Read from pipe_[1] + evutil_socket_t pipe_[2]; // Write to pipe_[0] , Read from pipe_[1] }; class TimerEventWatcher : public EventWatcher { @@ -69,7 +73,7 @@ class TimerEventWatcher : public EventWatcher { private: virtual bool DoInit(); - static void HandlerFn(int fd, short which, void* v); + static void HandlerFn(evutil_socket_t fd, short which, void* v); private: double timeout_ms_; }; diff --git a/test/event_watcher_test.cc b/test/event_watcher_test.cc index cb89a147a..2e40cdfa7 100644 --- a/test/event_watcher_test.cc +++ b/test/event_watcher_test.cc @@ -73,7 +73,7 @@ TEST_UNIT(testTimerEventWatcher) { } TEST_UNIT(testsocketpair) { - int sockpair[2]; + evpp_socket_t sockpair[2]; memset(sockpair, 0, sizeof(sockpair[0] * 2)); int r = evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair); H_TEST_ASSERT(r >= 0);