1 #include "resolvproto.h"
8 ResolvProtoRequest::ResolvProtoRequest(int fd)
14 ResolvProtoRequest::ResolvProtoRequest(std::string name, uint8_t type):
15 type(type), name(name)
19 void ResolvProtoRequest::send(int sockfd) const
20 throw (std::runtime_error)
22 if (libtcp_send(sockfd, &type, sizeof(uint8_t)) != sizeof(uint8_t))
23 throw std::runtime_error("Error al enviar type por socket");
24 uint16_t size = name.size();
25 if (libtcp_send(sockfd, &size, sizeof(uint16_t)) != sizeof(uint16_t))
26 throw std::runtime_error("Error al enviar size por socket");
27 if (libtcp_send(sockfd, name.c_str(), size) != size)
28 throw std::runtime_error("Error al enviar name por socket");
32 void ResolvProtoRequest::recv(int sockfd)
33 throw (std::runtime_error)
35 if (libtcp_receive_bin(sockfd, &type, sizeof(uint8_t)) != sizeof(uint8_t))
36 throw std::runtime_error("Error al recibir type por socket");
38 if (libtcp_receive_bin(sockfd, &size, sizeof(uint16_t)) != sizeof(uint16_t))
39 throw std::runtime_error("Error al recibir size por socket");
40 char* buf = (char*) malloc(size);
41 if (libtcp_receive_bin(sockfd, buf, size) != size)
44 throw std::runtime_error("Error al recibir name por socket");
46 name.assign(buf, size);
50 /// Impresión de request
51 std::ostream& operator<< (std::ostream& os, const ResolvProtoRequest& rpr)
53 return os << "ResolvProtoRequest(type=" << unsigned(rpr.type) << ", name="
58 ResolvProtoResponse::ResolvProtoResponse(int fd)
64 ResolvProtoResponse::ResolvProtoResponse(ret_t ret):
69 void ResolvProtoResponse::send(int sockfd) const
70 throw (std::runtime_error)
72 if (libtcp_send(sockfd, &ret, sizeof(uint8_t)) != sizeof(uint8_t))
73 throw std::runtime_error("Error al enviar ret por socket");
74 uint8_t count = ips.size();
75 if (libtcp_send(sockfd, &count, sizeof(uint8_t)) != sizeof(uint8_t))
76 throw std::runtime_error("Error al enviar count por socket");
77 for (ipvec_t::const_iterator i = ips.begin(); i != ips.end(); ++i)
80 if (libtcp_send(sockfd, &ip, sizeof(uint32_t)) != sizeof(uint32_t))
81 throw std::runtime_error("Error al enviar IPAddr por socket");
86 void ResolvProtoResponse::recv(int sockfd)
87 throw (std::runtime_error)
89 if (libtcp_receive_bin(sockfd, &ret, sizeof(uint8_t)) != sizeof(uint8_t))
90 throw std::runtime_error("Error al recibir ret por socket");
92 if (libtcp_receive_bin(sockfd, &count, sizeof(uint8_t)) != sizeof(uint8_t))
93 throw std::runtime_error("Error al recibir count por socket");
96 for (uint8_t i = 0; i < count; ++i)
99 if (libtcp_receive_bin(sockfd, &ip, sizeof(uint32_t)) != sizeof(uint32_t))
100 throw std::runtime_error("Error al recibir IPAddr por socket");
105 /// Impresión de response
106 std::ostream& operator<< (std::ostream& os, const ResolvProtoResponse& rpr)
110 os << "ResolvProtoResponse(ret=" << unsigned(rpr.ret) << ", ";
111 std::copy(rpr.ips.begin(), rpr.ips.end() - 1,
112 std::ostream_iterator< IPAddr >(os, ", "));
113 return os << rpr.ips.back() << ")";
116 // vim: set et sw=4 sts=4 :