bool TcpServer::Recv(int fd) { Json::Value response; ssize_t nb = -1; char buf[1500]; nb = recv(fd, buf, sizeof(buf), 0); /* give the message to JsonHandler */ if(nb > 0) { std::string msg = std::string(buf, nb); if(GetEncapsulatedFormat() == Json::Rpc::NETSTRING) { try { msg = netstring::decode(msg); } catch(const netstring::NetstringException& e) { /* error parsing Netstring */ std::cerr << e.what() << std::endl; return false; } } m_jsonHandler.Process(msg, response); /* in case of notification message received, the response could be Json::Value::null */ if(response != Json::Value::null) { std::string rep = m_jsonHandler.GetString(response); /* encoding */ if(GetEncapsulatedFormat() == Json::Rpc::NETSTRING) { rep = netstring::encode(rep); } if(send(fd, rep.c_str(), rep.length(), 0) == -1) { /* error */ std::cerr << "Error while sending data" << std::endl; return false; } } return true; } else { m_purge.push_back(fd); return false; } }
bool UdpServer::Recv(int fd) { Json::Value response; ssize_t nb = -1; char buf[1500]; struct sockaddr_storage addr; socklen_t addrlen = sizeof(struct sockaddr_storage); nb = ::recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &addrlen); if(nb > 0) { std::string msg = std::string(buf, nb); if(GetEncapsulatedFormat() == Json::Rpc::NETSTRING) { try { msg = netstring::decode(msg); } catch(const netstring::NetstringException& e) { /* error parsing NetString */ std::cerr << e.what() << std::endl; return false; } } /* give the message to JsonHandler */ m_jsonHandler.Process(msg, response); /* in case of notification message received, the response could be Json::Value::null */ if(response != Json::Value::null) { std::string rep = m_jsonHandler.GetString(response); /* encoding */ if(GetEncapsulatedFormat() == Json::Rpc::NETSTRING) { rep = netstring::encode(rep); } if(::sendto(fd, rep.c_str(), rep.length(), 0, (struct sockaddr*)&addr, addrlen) == -1) { /* error */ std::cerr << "Error while sending" << std::endl; return false; } } return true; } return false; }
ssize_t UdpClient::Recv(std::string& data) { char buf[1500]; ssize_t nb = -1; if((nb = ::recvfrom(m_sock, buf, sizeof(buf), 0, NULL, NULL)) == -1) { std::cerr << "Error while receiving" << std::endl; return -1; } data = std::string(buf, nb); /* decoding if any */ if(GetEncapsulatedFormat() == Json::Rpc::NETSTRING) { try { data = netstring::decode(data); } catch(const netstring::NetstringException& e) { std::cerr << e.what() << std::endl; } } return nb; }
ssize_t UdpClient::Send(const std::string& data) { std::string rep = data; /* encoding if any */ if(GetEncapsulatedFormat() == Json::Rpc::NETSTRING) { rep = netstring::encode(rep); } return ::sendto(m_sock, rep.c_str(), rep.length(), 0, (struct sockaddr*)&m_sockaddr, m_sockaddrlen); }
ssize_t TcpServer::Send(int fd, const std::string& data) { std::string rep = data; /* encoding if any */ if(GetEncapsulatedFormat() == Json::Rpc::NETSTRING) { rep = netstring::encode(rep); } if (GetEncapsulatedFormat() == Json::Rpc::HTTP_POST) { std::string tmp = "HTTP/1.1 200 OK\r\nServer: eidcppd server\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: "; char v[16]; snprintf(v, sizeof(v), "%u", rep.size()); tmp += v; tmp += "\r\n\r\n"; rep = tmp + rep; } return ::send(fd, rep.c_str(), rep.length(), 0); }
ssize_t TcpServer::Send(int fd, const std::string& data) { std::string rep = data; /* encoding if any */ if(GetEncapsulatedFormat() == Json::Rpc::NETSTRING) { rep = netstring::encode(rep); } return ::send(fd, rep.c_str(), rep.length(), 0); }
bool TcpServer::Recv(int fd) { Json::Value response; ssize_t nb = -1; char buf[1500]; nb = ::recv(fd, buf, sizeof(buf), 0); /* give the message to JsonHandler */ if(nb > 0) { std::string msg = std::string(buf, nb); if(GetEncapsulatedFormat() == Json::Rpc::NETSTRING) { try { msg = netstring::decode(msg); } catch(const netstring::NetstringException& e) { /* error parsing Netstring */ std::cerr << e.what() << std::endl; return false; } } if (GetEncapsulatedFormat() == Json::Rpc::HTTP_POST) { if (0 == msg.compare(0,15,"POST / HTTP/1.1")) { //printf("orig_msg: %s\n\n", msg.c_str()); fflush(stdout); size_t istart = msg.find("Content-Length: "); size_t iend = msg.find("\r\n\r\n{"); size_t content_msg_size = ::atoi(msg.substr(istart+16,iend-istart-16).c_str()); //printf("content_msg_size: %d\n", content_msg_size); fflush(stdout); if (iend != std::string::npos) { msg = msg.substr(iend+4); //printf("msg: %s msg_size: %d\n", msg.c_str(), msg.size()); fflush(stdout); if (content_msg_size != msg.size()) { return false; } } } else { return false; } } m_jsonHandler.Process(msg, response); /* in case of notification message received, the response could be Json::Value::null */ if(response != Json::Value::null) { Send(fd, m_jsonHandler.GetString(response)); } return true; } else { m_purge.push_back(fd); return false; } }