void HandshakeReceiver::receive(const Handshake &shake)
{
- hash=shake.hash;
+ hash = shake.hash;
}
}
throw InvalidState("Handshaking is already underway or done");
send_handshake();
- handshake_status=1;
+ handshake_status = 1;
}
void Communicator::data_available()
{
- in_end+=socket.read(in_end, in_buf+buf_size-in_end);
+ in_end += socket.read(in_end, in_buf+buf_size-in_end);
try
{
- bool more=true;
+ bool more = true;
while(more)
{
if(handshake_status==2)
{
- more=receive_packet(protocol, receiver);
+ more = receive_packet(protocol, receiver);
}
else
{
HandshakeProtocol hsproto;
HandshakeReceiver hsrecv;
- if((more=receive_packet(hsproto, hsrecv)))
+ if((more = receive_packet(hsproto, hsrecv)))
{
if(hsrecv.get_hash()==protocol.get_hash())
{
if(handshake_status==0)
send_handshake();
- handshake_status=2;
+ handshake_status = 2;
signal_handshake_done.emit();
}
else
bool Communicator::receive_packet(const Protocol &proto, ReceiverBase &recv)
{
- int psz=proto.get_packet_size(in_begin, in_end-in_begin);
+ int psz = proto.get_packet_size(in_begin, in_end-in_begin);
if(psz && psz<=in_end-in_begin)
{
- char *pkt=in_begin;
- in_begin+=psz;
+ char *pkt = in_begin;
+ in_begin += psz;
proto.disassemble(recv, pkt, psz);
return true;
}
{
if(in_end==in_buf+buf_size)
{
- unsigned used=in_end-in_begin;
+ unsigned used = in_end-in_begin;
memmove(in_buf, in_begin, used);
- in_begin=in_buf;
- in_end=in_begin+used;
+ in_begin = in_buf;
+ in_end = in_begin+used;
}
return false;
}
void Communicator::send_handshake()
{
Handshake shake;
- shake.hash=protocol.get_hash();
+ shake.hash = protocol.get_hash();
HandshakeProtocol hsproto;
- unsigned size=hsproto.assemble(shake, out_buf, buf_size);
+ unsigned size = hsproto.assemble(shake, out_buf, buf_size);
socket.write(out_buf, size);
}
{
if(handshake_status!=2)
throw InvalidState("Handshaking is not done");
- unsigned size=protocol.assemble(pkt, out_buf, buf_size);
+ unsigned size = protocol.assemble(pkt, out_buf, buf_size);
socket.write(out_buf, size);
}
enum Family
{
- UNSPEC=AF_UNSPEC,
- INET=AF_INET,
- INET6=AF_INET6,
- UNIF=AF_UNIX
+ UNSPEC = AF_UNSPEC,
+ INET = AF_INET,
+ INET6 = AF_INET6,
+ UNIF = AF_UNIX
};
} // namespace Net
check_state(false);
sockaddr_storage sa;
- socklen_t size=addr.fill_sockaddr(sa);
+ socklen_t size = addr.fill_sockaddr(sa);
- int err=::connect(handle, reinterpret_cast<sockaddr *>(&sa), size);
+ int err = ::connect(handle, reinterpret_cast<sockaddr *>(&sa), size);
if(err==-1)
throw SystemError("Unable to connect", errno);
delete peer_addr;
- peer_addr=addr.copy();
+ peer_addr = addr.copy();
delete local_addr;
- size=sizeof(sockaddr_storage);
+ size = sizeof(sockaddr_storage);
getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
- local_addr=SockAddr::create(sa);
+ local_addr = SockAddr::create(sa);
- connected=true;
+ connected = true;
return (err==0)?0:1;
}
return 0;
sockaddr_storage addr;
- socklen_t addr_len=addr_.fill_sockaddr(addr);
+ socklen_t addr_len = addr_.fill_sockaddr(addr);
- int ret=::sendto(handle, buf, size, 0, reinterpret_cast<sockaddr *>(&addr), addr_len);
+ int ret = ::sendto(handle, buf, size, 0, reinterpret_cast<sockaddr *>(&addr), addr_len);
if(ret<0)
{
if(errno==EAGAIN)
return 0;
sockaddr_storage addr;
- socklen_t addr_len=sizeof(sockaddr_storage);
+ socklen_t addr_len = sizeof(sockaddr_storage);
- int ret=::recvfrom(handle, buf, size, 0, reinterpret_cast<sockaddr *>(&addr), &addr_len);
+ int ret = ::recvfrom(handle, buf, size, 0, reinterpret_cast<sockaddr *>(&addr), &addr_len);
if(ret<0)
{
if(errno==EAGAIN)
throw SystemError("Recvfrom failed", errno);
}
- addr_=SockAddr::create(addr);
+ addr_ = SockAddr::create(addr);
return ret;
}
class DatagramSocket: public Socket
{
public:
- DatagramSocket(Family, int =0);
+ DatagramSocket(Family, int = 0);
+
int connect(const SockAddr &);
unsigned sendto(const char *, unsigned, const SockAddr &);
unsigned recvfrom(char *, unsigned, SockAddr *&);
string InetAddr::str() const
{
- const unsigned char *ptr=reinterpret_cast<const unsigned char *>(&addr);
+ const unsigned char *ptr = reinterpret_cast<const unsigned char *>(&addr);
string result = format("%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]);
if(port)
result += format(":%d", ntohs(port));
unsigned InetAddr::fill_sockaddr(sockaddr &sa) const
{
- sockaddr_in &sai=reinterpret_cast<sockaddr_in &>(sa);
- sai.sin_family=AF_INET;
- sai.sin_addr.s_addr=addr;
- sai.sin_port=port;
+ sockaddr_in &sai = reinterpret_cast<sockaddr_in &>(sa);
+ sai.sin_family = AF_INET;
+ sai.sin_addr.s_addr = addr;
+ sai.sin_port = port;
return sizeof(sockaddr_in);
}
char *Assembler<T>::assemble(const T &v, char *data, char *end)
{
// XXX Assumes little-endian
- const char *ptr=reinterpret_cast<const char *>(&v)+sizeof(T);
+ const char *ptr = reinterpret_cast<const char *>(&v)+sizeof(T);
for(unsigned i=0; i<sizeof(T); ++i)
{
if(data==end)
throw Msp::Exception("Out of buffer space");
- *data++=*--ptr;
+ *data++ = *--ptr;
}
return data;
}
template<>
char *Assembler<string>::assemble(const string &v, char *data, char *end)
{
- data=Assembler<unsigned short>::assemble(v.size(), data, end);
+ data = Assembler<unsigned short>::assemble(v.size(), data, end);
if(end-data<static_cast<int>(v.size()))
throw Msp::Exception("Out of buffer space");
memcpy(data, v.data(), v.size());
template<typename T>
char *Assembler<vector<T> >::assemble(const vector<T> &v, char *data, char *end)
{
- data=Assembler<unsigned short>::assemble(v.size(), data, end);
+ data = Assembler<unsigned short>::assemble(v.size(), data, end);
for(typename vector<T>::const_iterator i=v.begin(); i!=v.end(); ++i)
- data=Assembler<T>::assemble(*i, data, end);
+ data = Assembler<T>::assemble(*i, data, end);
return data;
}
template<typename T>
const char *Assembler<T>::disassemble(T &v, const char *data, const char *end)
{
- char *ptr=reinterpret_cast<char *>(&v)+sizeof(T);
+ char *ptr = reinterpret_cast<char *>(&v)+sizeof(T);
for(unsigned i=0; i<sizeof(T); ++i)
{
if(data==end)
throw Msp::Exception("Premature end of data");
- *--ptr=*data++;
+ *--ptr = *data++;
}
return data;
}
const char *Assembler<string>::disassemble(string &v, const char *data, const char *end)
{
unsigned short size;
- data=Assembler<unsigned short>::disassemble(size, data, end);
+ data = Assembler<unsigned short>::disassemble(size, data, end);
if(end-data<size)
throw Msp::Exception("Premature end of data");
v.assign(data, data+size);
only code path leading here is from PacketDef<P>::disassemble, which creates
a new packet. */
unsigned short size;
- data=Assembler<unsigned short>::disassemble(size, data, end);
+ data = Assembler<unsigned short>::disassemble(size, data, end);
for(unsigned i=0; i<size; ++i)
{
T u;
- data=Assembler<T>::disassemble(u, data, end);
+ data = Assembler<T>::disassemble(u, data, end);
v.push_back(u);
}
return data;
void Protocol::add_packet(PacketDefBase &pdef)
{
- PacketDefBase *&ptr=packet_class_defs[pdef.get_class_id()];
+ PacketDefBase *&ptr = packet_class_defs[pdef.get_class_id()];
if(ptr)
delete ptr;
- ptr=&pdef;
- packet_id_defs[pdef.get_id()]=&pdef;
+ ptr = &pdef;
+ packet_id_defs[pdef.get_id()] = &pdef;
}
const Protocol::PacketDefBase &Protocol::get_packet_by_class(unsigned id) const
{
- PacketMap::const_iterator i=packet_class_defs.find(id);
+ PacketMap::const_iterator i = packet_class_defs.find(id);
if(i==packet_class_defs.end())
throw KeyError("Unknown packet class", lexical_cast(id));
return *i->second;
const Protocol::PacketDefBase &Protocol::get_packet_by_id(unsigned id) const
{
- PacketMap::const_iterator i=packet_id_defs.find(id);
+ PacketMap::const_iterator i = packet_id_defs.find(id);
if(i==packet_id_defs.end())
throw KeyError("Unknown packet ID", lexical_cast(id));
return *i->second;
unsigned Protocol::disassemble(ReceiverBase &rcv, const char *data, unsigned size) const
{
- const unsigned char *udata=reinterpret_cast<const unsigned char *>(data);
- unsigned id=(udata[0]<<8)+udata[1];
- unsigned psz=(udata[2]<<8)+udata[3];
+ const unsigned char *udata = reinterpret_cast<const unsigned char *>(data);
+ unsigned id = (udata[0]<<8)+udata[1];
+ unsigned psz = (udata[2]<<8)+udata[3];
if(psz>size)
throw InvalidParameterValue("Not enough data for packet");
- const PacketDefBase &pdef=get_packet_by_id(id);
- const char *ptr=pdef.disassemble(rcv, data+4, data+psz);
+ const PacketDefBase &pdef = get_packet_by_id(id);
+ const char *ptr = pdef.disassemble(rcv, data+4, data+psz);
return ptr-data;
}
{
if(size<4)
return 0;
- const unsigned char *udata=reinterpret_cast<const unsigned char *>(data);
+ const unsigned char *udata = reinterpret_cast<const unsigned char *>(data);
return (udata[2]<<8)+udata[3];
}
void Protocol::assemble_header(char *buf, unsigned id, unsigned size)
{
- buf[0]=(id>>8)&0xFF;
- buf[1]=id&0xFF;
- buf[2]=(size>>8)&0xFF;
- buf[3]=size&0xFF;
+ buf[0] = (id>>8)&0xFF;
+ buf[1] = id&0xFF;
+ buf[2] = (size>>8)&0xFF;
+ buf[3] = size&0xFF;
}
template<typename T>
template const char *Protocol::disassemble_field<>(vector<double> &v, const char *d, const char *e);
template const char *Protocol::disassemble_field<>(vector<string> &v, const char *d, const char *e);
-unsigned Protocol::PacketDefBase::next_class_id=1;
+unsigned Protocol::PacketDefBase::next_class_id = 1;
} // namespace Net
} // namespace Msp
PacketDefBase(unsigned i): id(i) { }
public:
virtual ~PacketDefBase() { }
- virtual unsigned get_class_id() const =0;
+ virtual unsigned get_class_id() const = 0;
unsigned get_id() const { return id; }
- virtual const char *disassemble(ReceiverBase &, const char *, const char *) const =0;
+ virtual const char *disassemble(ReceiverBase &, const char *, const char *) const = 0;
static unsigned next_class_id;
};
FieldBase() { }
public:
virtual ~FieldBase() { }
- virtual char *assemble(const P &, char *, char *) const =0;
- virtual const char *disassemble(P &, const char *, const char *) const =0;
+ virtual char *assemble(const P &, char *, char *) const = 0;
+ virtual const char *disassemble(P &, const char *, const char *) const = 0;
};
template<typename P, typename T>
public:
PacketDef(unsigned i): PacketDefBase(i)
- { if(!class_id) class_id=next_class_id++; }
+ { if(!class_id) class_id = next_class_id++; }
~PacketDef()
{
char *assemble(const P &p, char *d, char *e) const
{
for(typename std::vector<FieldBase<P> *>::const_iterator i=fields.begin(); i!=fields.end(); ++i)
- d=(*i)->assemble(p, d, e);
+ d = (*i)->assemble(p, d, e);
return d;
}
const char *disassemble(ReceiverBase &r, const char *d, const char *e) const
{
- PacketReceiver<P> *prcv=dynamic_cast<PacketReceiver<P> *>(&r);
+ PacketReceiver<P> *prcv = dynamic_cast<PacketReceiver<P> *>(&r);
if(!prcv)
throw Exception("Packet type not supported by receiver");
P pkt;
for(typename std::vector<FieldBase<P> *>::const_iterator i=fields.begin(); i!=fields.end(); ++i)
- d=(*i)->disassemble(pkt, d, e);
+ d = (*i)->disassemble(pkt, d, e);
prcv->receive(pkt);
return d;
}
PacketMap packet_class_defs;
PacketMap packet_id_defs;
- Protocol(unsigned =1);
+ Protocol(unsigned = 1);
public:
~Protocol();
template<typename P>
PacketDef<P> &add()
{
- PacketDef<P> *pdef=new PacketDef<P>(next_packet_id++);
+ PacketDef<P> *pdef = new PacketDef<P>(next_packet_id++);
add_packet(*pdef);
return *pdef;
}
template<typename P>
unsigned assemble(const P &pkt, char *buf, unsigned size) const
{
- unsigned id=PacketDef<P>::class_id;
- const PacketDef<P> &pdef=static_cast<const PacketDef<P> &>(get_packet_by_class(id));
- char *ptr=pdef.assemble(pkt, buf+4, buf+size);
- assemble_header(buf, pdef.get_id(), (size=ptr-buf));
+ unsigned id = PacketDef<P>::class_id;
+ const PacketDef<P> &pdef = static_cast<const PacketDef<P> &>(get_packet_by_class(id));
+ char *ptr = pdef.assemble(pkt, buf+4, buf+size);
+ assemble_header(buf, pdef.get_id(), (size = ptr-buf));
return size;
}
};
template<typename P>
-unsigned Protocol::PacketDef<P>::class_id=0;
+unsigned Protocol::PacketDef<P>::class_id = 0;
} // namespace Net
} // namespace Msp
protected:
PacketReceiver() { }
public:
- virtual void receive(const P &) =0;
+ virtual void receive(const P &) = 0;
};
} // namespace Net
host = s;
}
- addrinfo hints={0, family, 0, 0, 0, 0, 0, 0};
+ addrinfo hints = {0, family, 0, 0, 0, 0, 0, 0};
addrinfo *res;
- const char *chost=(host.empty() ? 0 : host.c_str());
- const char *cserv=(serv.empty() ? 0 : serv.c_str());
- int err=getaddrinfo(chost, cserv, &hints, &res);
+ const char *chost = (host.empty() ? 0 : host.c_str());
+ const char *cserv = (serv.empty() ? 0 : serv.c_str());
+ int err = getaddrinfo(chost, cserv, &hints, &res);
if(err==0)
{
- SockAddr *addr=SockAddr::create(*res->ai_addr);
+ SockAddr *addr = SockAddr::create(*res->ai_addr);
freeaddrinfo(res);
return addr;
}
}
/*sockaddr sa;
- unsigned size=fill_sockaddr(sa);
+ unsigned size = fill_sockaddr(sa);
char hst[128];
char srv[128];
- int err=getnameinfo(&sa, size, hst, 128, srv, 128, 0);
+ int err = getnameinfo(&sa, size, hst, 128, srv, 128, 0);
if(err==0)
{
- host=hst;
- serv=srv;
+ host = hst;
+ serv = srv;
}*/
} // namespace Net
class SockAddr;
-SockAddr *resolve(const std::string &, Family =UNSPEC);
+SockAddr *resolve(const std::string &, Family = UNSPEC);
} // namespace Net
} // namespace Msp
class SockAddr
{
public:
- virtual Family get_family() const =0;
- virtual std::string str() const =0;
+ virtual Family get_family() const = 0;
+ virtual std::string str() const = 0;
/**
Fills the given struct sockaddr with information from this SockAddr.
@return Number of bytes used
*/
- virtual unsigned fill_sockaddr(sockaddr &) const =0;
+ virtual unsigned fill_sockaddr(sockaddr &) const = 0;
virtual unsigned fill_sockaddr(sockaddr_storage &) const;
virtual SockAddr *copy() const =0;
WinSockHelper()
{
WSADATA wsa_data;
- int err=WSAStartup(0x0002, &wsa_data);
+ int err = WSAStartup(0x0002, &wsa_data);
if(err)
std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
}
peer_addr(paddr.copy())
{
sockaddr_storage sa;
- socklen_t size=sizeof(sockaddr_storage);
+ socklen_t size = sizeof(sockaddr_storage);
getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
- local_addr=SockAddr::create(sa);
+ local_addr = SockAddr::create(sa);
#ifdef WIN32
- event=CreateEvent(0, false, false, 0);
+ event = CreateEvent(0, false, false, 0);
#endif
}
local_addr(0),
peer_addr(0)
{
- handle=socket(af, type, proto);
+ handle = socket(af, type, proto);
#ifdef WIN32
- event=CreateEvent(0, false, false, 0);
+ event = CreateEvent(0, false, false, 0);
#endif
}
void Socket::set_block(bool b)
{
- mode=(mode&~IO::M_NONBLOCK);
+ mode = (mode&~IO::M_NONBLOCK);
if(b)
- mode=(mode|IO::M_NONBLOCK);
+ mode = (mode|IO::M_NONBLOCK);
#ifdef WIN32
- u_long flag=!b;
+ u_long flag = !b;
ioctlsocket(handle, FIONBIO, &flag);
#else
- int flags=fcntl(handle, F_GETFL);
+ int flags = fcntl(handle, F_GETFL);
fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
#endif
}
check_state(false);
sockaddr_storage sa;
- unsigned size=addr.fill_sockaddr(sa);
+ unsigned size = addr.fill_sockaddr(sa);
- int err=::bind(handle, reinterpret_cast<sockaddr *>(&sa), size);
+ int err = ::bind(handle, reinterpret_cast<sockaddr *>(&sa), size);
if(err==-1)
throw SystemError("Unable to bind", errno);
delete local_addr;
- local_addr=addr.copy();
+ local_addr = addr.copy();
}
/**
#else
::close(handle);
#endif
- handle=MSP_NET_INVALID_SOCKET_HANDLE;
- connected=false;
+ handle = MSP_NET_INVALID_SOCKET_HANDLE;
+ connected = false;
signal_closed.emit();
delete local_addr;
- local_addr=0;
+ local_addr = 0;
delete peer_addr;
- peer_addr=0;
+ peer_addr = 0;
}
void Socket::set_timeout(const Time::TimeDelta &timeout)
if(size==0)
return 0;
- int ret=::send(handle, buf, size, 0);
+ int ret = ::send(handle, buf, size, 0);
if(ret<0)
{
if(errno==EAGAIN)
if(size==0)
return 0;
- int ret=::recv(handle, buf, size, 0);
+ int ret = ::recv(handle, buf, size, 0);
if(ret<0)
{
if(errno==EAGAIN)
}
else if(ret==0 && !eof_flag)
{
- eof_flag=true;
+ eof_flag = true;
signal_end_of_file.emit();
set_events(IO::P_NONE);
}
bool is_connected() const { return connected; }
void bind(const SockAddr &);
- virtual int connect(const SockAddr &) =0;
+ virtual int connect(const SockAddr &) = 0;
void close();
void set_timeout(const Time::TimeDelta &);
const SockAddr &get_local_address() const;
{
bind(addr);
- int err=::listen(handle, backlog);
+ int err = ::listen(handle, backlog);
if(err==-1)
throw SystemError("Unable to listen", errno);
#endif
set_events(IO::P_INPUT);
- listening=true;
+ listening = true;
}
StreamSocket *StreamListenSocket::accept()
throw InvalidState("Socket is not listening");
sockaddr_storage sa;
- socklen_t size=sizeof(sockaddr_storage);
- SocketHandle new_h=::accept(handle, reinterpret_cast<sockaddr *>(&sa), &size);
+ socklen_t size = sizeof(sockaddr_storage);
+ SocketHandle new_h = ::accept(handle, reinterpret_cast<sockaddr *>(&sa), &size);
- RefPtr<SockAddr> paddr=SockAddr::create(sa);
+ RefPtr<SockAddr> paddr = SockAddr::create(sa);
return new StreamSocket(new_h, *paddr);
}
class StreamListenSocket: public Socket
{
public:
- StreamListenSocket(Family, int =0);
+ StreamListenSocket(Family, int = 0);
int connect(const SockAddr &);
- void listen(const SockAddr &, unsigned =4);
+ void listen(const SockAddr &, unsigned = 4);
StreamSocket *accept();
private:
bool listening;
if(!connecting)
throw InvalidState("No connection attempt going on");
- int res=poll(*this, IO::P_OUTPUT, timeout);
+ int res = poll(*this, IO::P_OUTPUT, timeout);
if(res==-1)
#ifdef WIN32
throw Exception(format("Connection polling failed: %d", WSAGetLastError()));
#endif
else if(res>0)
{
- connecting=false;
+ connecting = false;
int err;
- socklen_t len=sizeof(int);
+ socklen_t len = sizeof(int);
get_option(SOL_SOCKET, SO_ERROR, &err, &len);
if(err!=0)
#endif
set_events(IO::P_INPUT);
- connected=true;
+ connected = true;
return 0;
}
throw InvalidState("Socket is already connected");
sockaddr_storage sa;
- socklen_t size=addr.fill_sockaddr(sa);
+ socklen_t size = addr.fill_sockaddr(sa);
#ifdef WIN32
- int err=WSAConnect(handle, reinterpret_cast<sockaddr *>(&sa), size, 0, 0, 0, 0);
+ int err = WSAConnect(handle, reinterpret_cast<sockaddr *>(&sa), size, 0, 0, 0, 0);
if(err==SOCKET_ERROR)
{
- int err_code=WSAGetLastError();
+ int err_code = WSAGetLastError();
if(err_code==WSAEWOULDBLOCK)
{
- connecting=true;
+ connecting = true;
WSAEventSelect(handle, event, FD_CONNECT);
set_events(IO::P_OUTPUT);
}
throw Exception(format("Unable to connect: %d", err_code));
}
#else
- int err=::connect(handle, reinterpret_cast<sockaddr *>(&sa), size);
+ int err = ::connect(handle, reinterpret_cast<sockaddr *>(&sa), size);
if(err==-1)
{
if(errno==EINPROGRESS)
{
- connecting=true;
+ connecting = true;
set_events(IO::P_OUTPUT);
}
else
#endif
delete peer_addr;
- peer_addr=addr.copy();
+ peer_addr = addr.copy();
delete local_addr;
- size=sizeof(sockaddr_storage);
+ size = sizeof(sockaddr_storage);
getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
- local_addr=SockAddr::create(sa);
+ local_addr = SockAddr::create(sa);
if(err==0)
{
- connected=true;
+ connected = true;
set_events(IO::P_INPUT);
signal_connect_finished.emit(0);
}
if((ev&(IO::P_OUTPUT|IO::P_ERROR)) && connecting)
{
int err;
- socklen_t len=sizeof(err);
+ socklen_t len = sizeof(err);
get_option(SOL_SOCKET, SO_ERROR, &err, &len);
- connecting=false;
- connected=(err==0);
+ connecting = false;
+ connected = (err==0);
signal_connect_finished.emit(err);
if(err!=0)
{
delete peer_addr;
- peer_addr=0;
+ peer_addr = 0;
}
#ifdef WIN32
private:
StreamSocket(SocketHandle, const SockAddr &);
public:
- StreamSocket(Family, int =0);
+ StreamSocket(Family, int = 0);
bool is_connecting() const { return connecting; }
int poll_connect(const Time::TimeDelta &);