Skip to content

Commit

Permalink
change port to unsigned short
Browse files Browse the repository at this point in the history
  • Loading branch information
yedongfu committed Feb 26, 2019
1 parent 1b5001c commit adfd8ab
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion 10m/10m-cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int main(int argc, const char *argv[]) {
base.runAfter(100 * k, [&] {
int c = conn_count / create_seconds / 10;
for (int i = 0; i < c; i++) {
short port = begin_port + (i % (end_port - begin_port));
unsigned short port = begin_port + (i % (end_port - begin_port));
auto con = TcpConn::createConnection(&base, host, port, 20 * 1000);
allConns.push_back(con);
con->setReconnectInterval(20 * 1000);
Expand Down
8 changes: 4 additions & 4 deletions handy/conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void TcpConn::attach(EventBase *base, int fd, Ip4Addr local, Ip4Addr peer) {
con->channel_->onWrite([=] { con->handleWrite(con); });
}

void TcpConn::connect(EventBase *base, const string &host, short port, int timeout, const string &localip) {
void TcpConn::connect(EventBase *base, const string &host, unsigned short port, int timeout, const string &localip) {
fatalif(state_ != State::Invalid && state_ != State::Closed && state_ != State::Failed, "current state is bad state to connect. state: %d", state_);
destHost_ = host;
destPort_ = port;
Expand Down Expand Up @@ -265,7 +265,7 @@ void TcpConn::sendMsg(Slice msg) {

TcpServer::TcpServer(EventBases *bases) : base_(bases->allocBase()), bases_(bases), listen_channel_(NULL), createcb_([] { return TcpConnPtr(new TcpConn); }) {}

int TcpServer::bind(const std::string &host, short port, bool reusePort) {
int TcpServer::bind(const std::string &host, unsigned short port, bool reusePort) {
addr_ = Ip4Addr(host, port);
int fd = socket(AF_INET, SOCK_STREAM, 0);
int r = net::setReuseAddr(fd);
Expand All @@ -288,7 +288,7 @@ int TcpServer::bind(const std::string &host, short port, bool reusePort) {
return 0;
}

TcpServerPtr TcpServer::startServer(EventBases *bases, const std::string &host, short port, bool reusePort) {
TcpServerPtr TcpServer::startServer(EventBases *bases, const std::string &host, unsigned short port, bool reusePort) {
TcpServerPtr p(new TcpServer(bases));
int r = p->bind(host, port, reusePort);
if (r) {
Expand Down Expand Up @@ -342,7 +342,7 @@ void TcpServer::handleAccept() {
}
}

HSHAPtr HSHA::startServer(EventBase *base, const std::string &host, short port, int threads) {
HSHAPtr HSHA::startServer(EventBase *base, const std::string &host, unsigned short port, int threads) {
HSHAPtr p = HSHAPtr(new HSHA(threads));
p->server_ = TcpServer::startServer(base, host, port);
return p->server_ ? p : NULL;
Expand Down
10 changes: 5 additions & 5 deletions handy/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct TcpConn : public std::enable_shared_from_this<TcpConn>, private noncopyab
virtual ~TcpConn();
//可传入连接类型,返回智能指针
template <class C = TcpConn>
static TcpConnPtr createConnection(EventBase *base, const std::string &host, short port, int timeout = 0, const std::string &localip = "") {
static TcpConnPtr createConnection(EventBase *base, const std::string &host, unsigned short port, int timeout = 0, const std::string &localip = "") {
TcpConnPtr con(new C);
con->connect(base, host, port, timeout, localip);
return con;
Expand Down Expand Up @@ -103,7 +103,7 @@ struct TcpConn : public std::enable_shared_from_this<TcpConn>, private noncopyab
void handleWrite(const TcpConnPtr &con);
ssize_t isend(const char *buf, size_t len);
void cleanup(const TcpConnPtr &con);
void connect(EventBase *base, const std::string &host, short port, int timeout, const std::string &localip);
void connect(EventBase *base, const std::string &host, unsigned short port, int timeout, const std::string &localip);
void reconnect();
void attach(EventBase *base, int fd, Ip4Addr local, Ip4Addr peer);
virtual int readImp(int fd, void *buf, size_t bytes) { return ::read(fd, buf, bytes); }
Expand All @@ -115,8 +115,8 @@ struct TcpConn : public std::enable_shared_from_this<TcpConn>, private noncopyab
struct TcpServer : private noncopyable {
TcpServer(EventBases *bases);
// return 0 on sucess, errno on error
int bind(const std::string &host, short port, bool reusePort = false);
static TcpServerPtr startServer(EventBases *bases, const std::string &host, short port, bool reusePort = false);
int bind(const std::string &host, unsigned short port, bool reusePort = false);
static TcpServerPtr startServer(EventBases *bases, const std::string &host, unsigned short port, bool reusePort = false);
~TcpServer() { delete listen_channel_; }
Ip4Addr getAddr() { return addr_; }
EventBase *getBase() { return base_; }
Expand Down Expand Up @@ -150,7 +150,7 @@ typedef std::function<std::string(const TcpConnPtr &, const std::string &msg)> R
struct HSHA;
typedef std::shared_ptr<HSHA> HSHAPtr;
struct HSHA {
static HSHAPtr startServer(EventBase *base, const std::string &host, short port, int threads);
static HSHAPtr startServer(EventBase *base, const std::string &host, unsigned short port, int threads);
HSHA(int threads) : threadPool_(threads) {}
void exit() {
threadPool_.exit();
Expand Down
2 changes: 1 addition & 1 deletion handy/event_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ void TcpConn::reconnect() {
info("reconnect interval: %d will reconnect after %lld ms", reconnectInterval_, interval);
getBase()->runAfter(interval, [this, con]() {
getBase()->imp_->reconnectConns_.erase(con);
connect(getBase(), destHost_, (short) destPort_, connectTimeout_, localIp_);
connect(getBase(), destHost_, (unsigned short) destPort_, connectTimeout_, localIp_);
});
delete channel_;
channel_ = NULL;
Expand Down
6 changes: 3 additions & 3 deletions handy/net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int net::setNoDelay(int fd, bool value) {
return setsockopt(fd, SOL_SOCKET, TCP_NODELAY, &flag, len);
}

Ip4Addr::Ip4Addr(const string &host, short port) {
Ip4Addr::Ip4Addr(const string &host, unsigned short port) {
memset(&addr_, 0, sizeof addr_);
addr_.sin_family = AF_INET;
addr_.sin_port = htons(port);
Expand All @@ -68,8 +68,8 @@ string Ip4Addr::ip() const {
return util::format("%d.%d.%d.%d", (uip >> 0) & 0xff, (uip >> 8) & 0xff, (uip >> 16) & 0xff, (uip >> 24) & 0xff);
}

short Ip4Addr::port() const {
return ntohs(addr_.sin_port);
unsigned short Ip4Addr::port() const {
return (unsigned short)ntohs(addr_.sin_port);
}

unsigned int Ip4Addr::ipInt() const {
Expand Down
6 changes: 3 additions & 3 deletions handy/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ struct net {
};

struct Ip4Addr {
Ip4Addr(const std::string &host, short port);
Ip4Addr(short port = 0) : Ip4Addr("", port) {}
Ip4Addr(const std::string &host, unsigned short port);
Ip4Addr(unsigned short port = 0) : Ip4Addr("", port) {}
Ip4Addr(const struct sockaddr_in &addr) : addr_(addr){};
std::string toString() const;
std::string ip() const;
short port() const;
unsigned short port() const;
unsigned int ipInt() const;
// if you pass a hostname to constructor, then use this to check error
bool isIpValid() const;
Expand Down
2 changes: 1 addition & 1 deletion handy/stat-svr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct StatServer : private noncopyable {
CMD,
};
StatServer(EventBase *base);
int bind(const std::string &host, short port) { return server_.bind(host, port); }
int bind(const std::string &host, unsigned short port) { return server_.bind(host, port); }
typedef std::string string;
void onRequest(StatType type, const string &key, const string &desc, const StatCallBack &cb);
void onRequest(StatType type, const string &key, const string &desc, const InfoCallBack &cb);
Expand Down
8 changes: 4 additions & 4 deletions handy/udp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace handy {

UdpServer::UdpServer(EventBases *bases) : base_(bases->allocBase()), bases_(bases), channel_(NULL) {}

int UdpServer::bind(const std::string &host, short port, bool reusePort) {
int UdpServer::bind(const std::string &host, unsigned short port, bool reusePort) {
addr_ = Ip4Addr(host, port);
int fd = socket(AF_INET, SOCK_DGRAM, 0);
int r = net::setReuseAddr(fd);
Expand Down Expand Up @@ -45,7 +45,7 @@ int UdpServer::bind(const std::string &host, short port, bool reusePort) {
return 0;
}

UdpServerPtr UdpServer::startServer(EventBases *bases, const std::string &host, short port, bool reusePort) {
UdpServerPtr UdpServer::startServer(EventBases *bases, const std::string &host, unsigned short port, bool reusePort) {
UdpServerPtr p(new UdpServer(bases));
int r = p->bind(host, port, reusePort);
if (r) {
Expand All @@ -68,7 +68,7 @@ void UdpServer::sendTo(const char *buf, size_t len, Ip4Addr addr) {
trace("udp %d sendto %s %d bytes", fd, addr.toString().c_str(), wn);
}

UdpConnPtr UdpConn::createConnection(EventBase *base, const string &host, short port) {
UdpConnPtr UdpConn::createConnection(EventBase *base, const string &host, unsigned short port) {
Ip4Addr addr(host, port);
int fd = socket(AF_INET, SOCK_DGRAM, 0);
fatalif(fd < 0, "socket failed %d %s", errno, strerror(errno));
Expand Down Expand Up @@ -128,7 +128,7 @@ void UdpConn::send(const char *buf, size_t len) {
trace("udp %d write %d bytes", fd, wn);
}

HSHAUPtr HSHAU::startServer(EventBase *base, const std::string &host, short port, int threads) {
HSHAUPtr HSHAU::startServer(EventBase *base, const std::string &host, unsigned short port, int threads) {
HSHAUPtr p = HSHAUPtr(new HSHAU(threads));
p->server_ = UdpServer::startServer(base, host, port);
return p->server_ ? p : NULL;
Expand Down
8 changes: 4 additions & 4 deletions handy/udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const int kUdpPacketSize = 4096;
struct UdpServer : public std::enable_shared_from_this<UdpServer>, private noncopyable {
UdpServer(EventBases *bases);
// return 0 on sucess, errno on error
int bind(const std::string &host, short port, bool reusePort = false);
static UdpServerPtr startServer(EventBases *bases, const std::string &host, short port, bool reusePort = false);
int bind(const std::string &host, unsigned short port, bool reusePort = false);
static UdpServerPtr startServer(EventBases *bases, const std::string &host, unsigned short port, bool reusePort = false);
~UdpServer() { delete channel_; }
Ip4Addr getAddr() { return addr_; }
EventBase *getBase() { return base_; }
Expand Down Expand Up @@ -44,7 +44,7 @@ struct UdpConn : public std::enable_shared_from_this<UdpConn>, private noncopyab
// Udp构造函数,实际可用的连接应当通过createConnection创建
UdpConn(){};
virtual ~UdpConn() { close(); };
static UdpConnPtr createConnection(EventBase *base, const std::string &host, short port);
static UdpConnPtr createConnection(EventBase *base, const std::string &host, unsigned short port);
// automatically managed context. allocated when first used, deleted when destruct
template <class T>
T &context() {
Expand Down Expand Up @@ -83,7 +83,7 @@ typedef std::function<std::string(const UdpServerPtr &, const std::string &, Ip4
struct HSHAU;
typedef std::shared_ptr<HSHAU> HSHAUPtr;
struct HSHAU {
static HSHAUPtr startServer(EventBase *base, const std::string &host, short port, int threads);
static HSHAUPtr startServer(EventBase *base, const std::string &host, unsigned short port, int threads);
HSHAU(int threads) : threadPool_(threads) {}
void exit() {
threadPool_.exit();
Expand Down
2 changes: 1 addition & 1 deletion raw-examples/epoll-et.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ int main(int argc, const char *argv[]) {
for (int i = 0; i < 1048570; i++) {
httpRes += '\0';
}
short port = 80;
unsigned short port = 80;
int epollfd = epoll_create(1);
exit_if(epollfd < 0, "epoll_create failed");
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
Expand Down
2 changes: 1 addition & 1 deletion raw-examples/epoll.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ int main(int argc, const char *argv[]) {
for (int i = 0; i < 1048570; i++) {
httpRes += '\0';
}
short port = 80;
short unsigned short = 80;
int epollfd = epoll_create(1);
exit_if(epollfd < 0, "epoll_create failed");
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
Expand Down
2 changes: 1 addition & 1 deletion raw-examples/kqueue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void loop_once(int efd, int lfd, int waitms) {
}

int main() {
short port = 2099;
unsigned short port = 2099;
int epollfd = kqueue();
exit_if(epollfd < 0, "kqueue failed");
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
Expand Down
2 changes: 1 addition & 1 deletion test/tcpcli.ut.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using namespace std;
using namespace handy;

TcpConnPtr connectto(EventBase *base, const char *host, short port) {
TcpConnPtr connectto(EventBase *base, const char *host, unsigned short port) {
TcpConnPtr con1 = TcpConn::createConnection(base, host, port);
con1->onState([=](const TcpConnPtr con) {
if (con->getState() == TcpConn::Connected) {
Expand Down

0 comments on commit adfd8ab

Please sign in to comment.