-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinaclient.h
More file actions
105 lines (90 loc) · 2.95 KB
/
linaclient.h
File metadata and controls
105 lines (90 loc) · 2.95 KB
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
#ifndef LINA_CLIENT_H
#define LINA_CLIENT_H
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <cstring>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/uio.h>
#include <netdb.h>
typedef int SOCKET;
#define INVALID_SOCKET (SOCKET)(~0)
#endif
#define LINA_NAME_MAX_LENGTH 255
#define LINA_HEADER_BASE_LENGTH 10 // flags(1) + ilen(1) + dlen(4) + checksum(4)
#include <string>
#include <vector>
#include <memory>
#include <sstream>
#include "crc32.h"
// Forward declaration of HandshakeResult
struct HandshakeResult {
bool status;
std::string token;
uint64_t expires_at;
std::string message;
};
class LiNaClient {
public:
LiNaClient(std::string address, int port, bool auto_refresh = true, uint32_t refresh_buffer = 300);
~LiNaClient();
bool linaUploadFile(std::string name, std::vector<char> data, uint8_t flags);
std::vector<char> linaDownloadFile(std::string name);
bool linaDeleteFile(std::string name);
HandshakeResult linaHandshake(std::string username, std::string password, bool cache_credentials = true);
void check_sendv(const std::vector<std::pair<const void*, size_t>>& buffers, const char* context);
void check_recv(char* buf, size_t len, const char* context);
// Token management functions
bool linaIsTokenExpired() const;
bool linaRefreshTokenIfNeeded();
void linaCacheCredentials(std::string username, std::string password);
void linaClearCachedCredentials();
struct TokenInfo {
bool has_token;
bool is_expired;
uint64_t expires_at;
uint64_t expires_in;
bool has_cached_credentials;
};
TokenInfo linaGetTokenInfo() const;
enum LiNaFlags {
LINA_DELETE = 0xC0,
LINA_WRITE = 0x80,
LINA_AUTH = 0x60,
LINA_READ = 0x40,
LINA_COVER = 0x02,
LINA_COMPRESS = 0x01,
LINA_NONE = 0x00
};
private:
bool connect();
bool disconnect();
SOCKET sock;
struct sockaddr_in server_addr;
std::string server_address; // IP address or hostname
// Token management
std::string session_token;
uint64_t token_expires_at;
// Cached credentials for auto-refresh
std::string cached_username;
std::string cached_password;
// Auto-refresh settings
bool auto_refresh;
uint32_t refresh_buffer;
};
class LiNaClientException : public std::exception {
public:
LiNaClientException(std::string message) : message(message) {}
const char* what() const throw() { return message.c_str(); }
private:
std::string message;
};
// Utility functions
template <typename T>
std::vector<T> to_vector(uint64_t value, uint8_t length, bool little_endian = true);
uint64_t to_long(std::vector<uint8_t> data, uint8_t length, bool little_endian = true);
#endif // LINA_CLIENT_H