1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 Eran Ifrah
// file name : clindexerprotocol.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "clindexerprotocol.h"
#include <memory>
#include <stdio.h>
#include <sstream>
#define ACK_MAGIC 1975
class CharDeleter
{
char* m_ptr;
public:
CharDeleter(char* p)
: m_ptr(p)
{
}
~CharDeleter()
{
if(m_ptr) {
delete[] m_ptr;
m_ptr = NULL;
}
}
};
clIndexerProtocol::clIndexerProtocol() {}
clIndexerProtocol::~clIndexerProtocol() {}
bool clIndexerProtocol::ReadReply(clNamedPipe* conn, clIndexerReply& reply, std::string& errmsg)
{
// first we read sizeof(size_t) to get the actual data size
size_t buff_len(0);
size_t actual_read(0);
if(!conn->read((void*)&buff_len, sizeof(buff_len), &actual_read, 10000)) {
std::stringstream ss;
ss << "ERROR: ReadReply: Failed to read from the pipe, reason: " << conn->getLastError();
errmsg = ss.str();
return false;
}
if(actual_read != sizeof(buff_len)) {
std::stringstream ss;
ss << "ERROR: ReadReply: Protocol error: expected" << (sizeof(buff_len)) << "bytes, got: " << (int)actual_read
<< " reason: " << conn->getLastError();
errmsg = ss.str();
return false;
}
if((buff_len / (1024 * 1024)) > 15) {
// Dont read buffers larger than 15MB...
errmsg = "Buffer size is larger than 15MB";
return false;
}
char* data = new char[buff_len];
CharDeleter deleter(data);
int bytes_left(buff_len);
size_t bytes_read(0);
while(bytes_left > 0) {
if(!conn->read(data + bytes_read, bytes_left, &actual_read, 10000)) {
std::stringstream ss;
ss << "ERROR: Protocol error: expected " << buff_len << " bytes, got " << actual_read;
return false;
}
bytes_left -= actual_read;
bytes_read += actual_read;
}
reply.fromBinary(data);
return true;
}
bool clIndexerProtocol::ReadRequest(clNamedPipe* conn, clIndexerRequest& req)
{
// first we read sizeof(size_t) to get the actual data size
size_t buff_len(0);
size_t actual_read(0);
if(!conn->read((void*)&buff_len, sizeof(buff_len), &actual_read, -1)) {
fprintf(stderr, "ERROR: Failed to read from the pipe, reason: %d\n", conn->getLastError());
return false;
}
if(actual_read != sizeof(buff_len)) {
fprintf(stderr, "ERROR: Protocol error: expected %u bytes, got %u\n", (unsigned int)sizeof(buff_len),
(unsigned int)actual_read);
return false;
}
if(buff_len == 0) return false;
char* data = new char[buff_len];
CharDeleter deleter(data);
int bytes_left(buff_len);
size_t bytes_read(0);
while(bytes_left > 0) {
if(!conn->read(data + bytes_read, bytes_left, &actual_read, -1)) {
fprintf(stderr, "ERROR: [%s] Protocol error: expected %u bytes, got %u\n", __PRETTY_FUNCTION__,
(unsigned int)buff_len, (unsigned int)actual_read);
return false;
}
bytes_left -= actual_read;
bytes_read += actual_read;
}
req.fromBinary(data);
return true;
}
bool clIndexerProtocol::SendReply(clNamedPipe* conn, clIndexerReply& reply)
{
size_t buff_size(0);
char* data = reply.toBinary(buff_size);
CharDeleter deleter(data);
// send the reply size
size_t written(0);
conn->write((void*)&buff_size, sizeof(buff_size), &written, -1);
int bytes_left(buff_size);
int bytes_to_write(0);
int bytes_written(0);
while(bytes_left > 0) {
// we write in chunks of 3000 bytes
if(bytes_left < 3000) {
bytes_to_write = bytes_left;
} else {
bytes_to_write = 3000;
}
size_t actual_written(0);
if(!conn->write(data + bytes_written, bytes_to_write, &actual_written, -1)) {
return false;
}
bytes_left -= actual_written;
bytes_written += actual_written;
}
// the above problem does not exist under Windows' NamedPipes
return true;
}
bool clIndexerProtocol::SendRequest(clNamedPipe* conn, clIndexerRequest& req)
{
size_t size(0);
size_t written(0);
char* data = req.toBinary(size);
CharDeleter deleter(data);
// write request
if(!conn->write((void*)&size, sizeof(size), &written, -1)) {
printf("ERROR: [%s] protocol error: rc %d\n", __PRETTY_FUNCTION__, conn->getLastError());
return false;
}
int bytes_left(size);
int bytes_to_write(0);
int bytes_written(0);
while(bytes_left > 0) {
// we write in chunks of 3000 bytes
if(bytes_left < 3000) {
bytes_to_write = bytes_left;
} else {
bytes_to_write = 3000;
}
size_t actual_written(0);
if(!conn->write(data + bytes_written, bytes_to_write, &actual_written, -1)) {
return false;
}
bytes_left -= actual_written;
bytes_written += actual_written;
}
return true;
}
|