@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <stdexcept>
#include <chrono>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <swoc/BufferWriter.h>
namespace shared::rpc
{
using namespace std::chrono_literals;
struct IPCSocketClient {
enum class ReadStatus { NO_ERROR = 0, BUFFER_FULL, READ_ERROR, TIMEOUT, UNKNOWN };
using self_reference = IPCSocketClient &;
IPCSocketClient(std::string path = "/tmp/jsonrpc20.sock") : _path{std::move(path)} { memset(&_server, 0, sizeof(_server)); }
~IPCSocketClient() { this->disconnect(); }
self_reference connect(std::chrono::milliseconds wait_ms = 40ms, int attempts = 5);
self_reference send(std::string_view data);
ReadStatus read_all(std::string &content, std::chrono::milliseconds timeout_ms = 1000ms, int attempts = 10);
void
disconnect()
{
this->close();
}
void
close()
{
if (_sock > 0) {
::close(_sock);
_sock = -1;
}
}
bool
is_closed() const
{
return (_sock < 0);
}
protected:
std::int64_t _safe_write(int fd, const char *buffer, int len);
std::string _path;
struct sockaddr_un _server;
int _sock{-1};
};
}