From: Mikko Rasa Date: Fri, 9 Dec 2022 16:25:09 +0000 (+0200) Subject: Use size_t to represent sizes X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c2eeb0da9f5e5de1b8de1bc66a42709e54d504e2;p=libs%2Fnet.git Use size_t to represent sizes --- diff --git a/source/net/clientsocket.cpp b/source/net/clientsocket.cpp index 04d0700..e07b1f6 100644 --- a/source/net/clientsocket.cpp +++ b/source/net/clientsocket.cpp @@ -60,7 +60,7 @@ const SockAddr &ClientSocket::get_peer_address() const return *peer_addr; } -unsigned ClientSocket::do_write(const char *buf, unsigned size) +size_t ClientSocket::do_write(const char *buf, size_t size) { check_access(IO::M_WRITE); if(!connected) @@ -72,7 +72,7 @@ unsigned ClientSocket::do_write(const char *buf, unsigned size) return check_sys_error(::send(priv->handle, buf, size, 0), "send"); } -unsigned ClientSocket::do_read(char *buf, unsigned size) +size_t ClientSocket::do_read(char *buf, size_t size) { check_access(IO::M_READ); if(!connected) @@ -81,7 +81,7 @@ unsigned ClientSocket::do_read(char *buf, unsigned size) if(size==0) return 0; - unsigned ret = check_sys_error(::recv(priv->handle, buf, size, 0), "recv"); + size_t ret = check_sys_error(::recv(priv->handle, buf, size, 0), "recv"); if(ret==0 && !eof_flag) { eof_flag = true; diff --git a/source/net/clientsocket.h b/source/net/clientsocket.h index 0b570cd..ab83322 100644 --- a/source/net/clientsocket.h +++ b/source/net/clientsocket.h @@ -42,8 +42,8 @@ public: const SockAddr &get_peer_address() const; protected: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); }; } // namespace Net diff --git a/source/net/communicator.cpp b/source/net/communicator.cpp index 22a7963..07a8494 100644 --- a/source/net/communicator.cpp +++ b/source/net/communicator.cpp @@ -84,7 +84,7 @@ void Communicator::initiate_handshake() handshake_status = 1; } -void Communicator::send_data(unsigned size) +void Communicator::send_data(size_t size) { if(!good) throw sequence_error("connection aborted"); @@ -161,7 +161,7 @@ bool Communicator::receive_packet(const Protocol &proto, ReceiverBase &recv) { if(in_end==in_buf+buf_size) { - unsigned used = in_end-in_begin; + size_t used = in_end-in_begin; memmove(in_buf, in_begin, used); in_begin = in_buf; in_end = in_begin+used; @@ -176,7 +176,7 @@ void Communicator::send_handshake() shake.hash = protocol.get_hash(); HandshakeProtocol hsproto; - unsigned size = hsproto.serialize(shake, out_buf, buf_size); + size_t size = hsproto.serialize(shake, out_buf, buf_size); socket.write(out_buf, size); } diff --git a/source/net/communicator.h b/source/net/communicator.h index 63114fd..e4869b2 100644 --- a/source/net/communicator.h +++ b/source/net/communicator.h @@ -35,7 +35,7 @@ private: const Protocol &protocol; ReceiverBase &receiver; int handshake_status; - unsigned buf_size; + std::size_t buf_size; char *in_buf; char *in_begin; char *in_end; @@ -53,7 +53,7 @@ public: void send(const P &); private: - void send_data(unsigned); + void send_data(std::size_t); void data_available(); bool receive_packet(const Protocol &, ReceiverBase &); diff --git a/source/net/datagramsocket.cpp b/source/net/datagramsocket.cpp index fe01ef5..e92e286 100644 --- a/source/net/datagramsocket.cpp +++ b/source/net/datagramsocket.cpp @@ -33,7 +33,7 @@ bool DatagramSocket::connect(const SockAddr &addr) return true; } -unsigned DatagramSocket::sendto(const char *buf, unsigned size, const SockAddr &addr) +size_t DatagramSocket::sendto(const char *buf, size_t size, const SockAddr &addr) { if(size==0) return 0; @@ -42,13 +42,13 @@ unsigned DatagramSocket::sendto(const char *buf, unsigned size, const SockAddr & return check_sys_error(::sendto(priv->handle, buf, size, 0, reinterpret_cast(&sa.addr), sa.size), "sendto"); } -unsigned DatagramSocket::recvfrom(char *buf, unsigned size, SockAddr *&from_addr) +size_t DatagramSocket::recvfrom(char *buf, size_t size, SockAddr *&from_addr) { if(size==0) return 0; SockAddr::SysAddr sa; - unsigned ret = check_sys_error(::recvfrom(priv->handle, buf, size, 0, reinterpret_cast(&sa.addr), &sa.size), "recvfrom"); + size_t ret = check_sys_error(::recvfrom(priv->handle, buf, size, 0, reinterpret_cast(&sa.addr), &sa.size), "recvfrom"); from_addr = SockAddr::new_from_sys(sa); return ret; diff --git a/source/net/datagramsocket.h b/source/net/datagramsocket.h index 23ca296..678a844 100644 --- a/source/net/datagramsocket.h +++ b/source/net/datagramsocket.h @@ -14,8 +14,8 @@ public: virtual bool connect(const SockAddr &); virtual bool poll_connect(const Time::TimeDelta &) { return false; } - unsigned sendto(const char *, unsigned, const SockAddr &); - unsigned recvfrom(char *, unsigned, SockAddr *&); + std::size_t sendto(const char *, std::size_t, const SockAddr &); + std::size_t recvfrom(char *, std::size_t, SockAddr *&); }; } // namespace Net diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index a152c55..f8bf4c3 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -49,7 +49,7 @@ const Protocol::PacketDefBase &Protocol::get_packet_by_id(unsigned id) const return *get_item(packet_id_defs, id); } -unsigned Protocol::dispatch(ReceiverBase &rcv, const char *buf, unsigned size) const +size_t Protocol::dispatch(ReceiverBase &rcv, const char *buf, size_t size) const { PacketHeader header; buf = header_def.deserialize(header, buf, buf+size); @@ -60,7 +60,7 @@ unsigned Protocol::dispatch(ReceiverBase &rcv, const char *buf, unsigned size) c return ptr-buf; } -unsigned Protocol::get_packet_size(const char *buf, unsigned size) const +size_t Protocol::get_packet_size(const char *buf, size_t size) const { if(size<4) return 0; @@ -87,7 +87,7 @@ char *Protocol::BasicSerializer::serialize(const T &value, char *buf, char *e throw buffer_error("overflow"); const char *ptr = reinterpret_cast(&value)+sizeof(T); - for(unsigned i=0; i::deserialize(T &value, const char *buf, throw buffer_error("underflow"); char *ptr = reinterpret_cast(&value)+sizeof(T); - for(unsigned i=0; i - unsigned serialize(const P &, char *, unsigned) const; + std::size_t serialize(const P &, char *, std::size_t) const; - unsigned get_packet_size(const char *, unsigned) const; - unsigned dispatch(ReceiverBase &, const char *, unsigned) const; + std::size_t get_packet_size(const char *, std::size_t) const; + std::size_t dispatch(ReceiverBase &, const char *, std::size_t) const; UInt64 get_hash() const; @@ -291,7 +291,7 @@ const Protocol::PacketTypeDef

&Protocol::get_packet_by_class() const } template -unsigned Protocol::serialize(const P &pkt, char *buf, unsigned size) const +std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size) const { const PacketTypeDef

&pdef = get_packet_by_class

(); if(!pdef.get_id()) diff --git a/source/net/serversocket.cpp b/source/net/serversocket.cpp index 6028483..051ac50 100644 --- a/source/net/serversocket.cpp +++ b/source/net/serversocket.cpp @@ -9,12 +9,12 @@ ServerSocket::ServerSocket(Family af, int type, int proto): Socket(af, type, proto) { } -unsigned ServerSocket::do_write(const char *, unsigned) +size_t ServerSocket::do_write(const char *, size_t) { throw logic_error("can't write to ServerSocket"); } -unsigned ServerSocket::do_read(char *, unsigned) +size_t ServerSocket::do_read(char *, size_t) { throw logic_error("can't read from ServerSocket"); } diff --git a/source/net/serversocket.h b/source/net/serversocket.h index 10375f0..dd892e0 100644 --- a/source/net/serversocket.h +++ b/source/net/serversocket.h @@ -22,8 +22,8 @@ public: virtual ClientSocket *accept() = 0; protected: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); }; } // namespace Net diff --git a/source/net/socket_private.h b/source/net/socket_private.h index 86ce587..bb9eff6 100644 --- a/source/net/socket_private.h +++ b/source/net/socket_private.h @@ -27,7 +27,7 @@ struct Socket::Private int get_option(int, int, void *, socklen_t *); }; -unsigned check_sys_error(int, const char *); +std::size_t check_sys_error(std::make_signed::type, const char *); bool check_sys_connect_error(int); } // namespace Net diff --git a/source/net/unix/socket.cpp b/source/net/unix/socket.cpp index 970b9ae..b8cc580 100644 --- a/source/net/unix/socket.cpp +++ b/source/net/unix/socket.cpp @@ -9,6 +9,8 @@ #include "socket.h" #include "socket_private.h" +using namespace std; + namespace Msp { namespace Net { @@ -58,7 +60,7 @@ int Socket::Private::get_option(int level, int optname, void *optval, socklen_t } -unsigned check_sys_error(int ret, const char *func) +size_t check_sys_error(make_signed::type ret, const char *func) { if(ret<0) { diff --git a/source/net/windows/socket.cpp b/source/net/windows/socket.cpp index 5094cc4..dfcff62 100644 --- a/source/net/windows/socket.cpp +++ b/source/net/windows/socket.cpp @@ -6,6 +6,8 @@ #include "socket.h" #include "socket_private.h" +using namespace std; + namespace { class WinSockHelper @@ -85,7 +87,7 @@ int Socket::Private::get_option(int level, int optname, void *optval, socklen_t } -unsigned check_sys_error(int ret, const char *func) +size_t check_sys_error(make_signed::type ret, const char *func) { if(ret<0) {