AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.5.1
amps_impl.h
1 /*//////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2010-2025 60East Technologies Inc., All Rights Reserved.
4 //
5 // This computer software is owned by 60East Technologies Inc. and is
6 // protected by U.S. copyright laws and other laws and by international
7 // treaties. This computer software is furnished by 60East Technologies
8 // Inc. pursuant to a written license agreement and may be used, copied,
9 // transmitted, and stored only in accordance with the terms of such
10 // license agreement and with the inclusion of the above copyright notice.
11 // This computer software or any other copies thereof may not be provided
12 // or otherwise made available to any other person.
13 //
14 // U.S. Government Restricted Rights. This computer software: (a) was
15 // developed at private expense and is in all respects the proprietary
16 // information of 60East Technologies Inc.; (b) was not developed with
17 // government funds; (c) is a trade secret of 60East Technologies Inc.
18 // for all purposes of the Freedom of Information Act; and (d) is a
19 // commercial item and thus, pursuant to Section 12.212 of the Federal
20 // Acquisition Regulations (FAR) and DFAR Supplement Section 227.7202,
21 // Government's use, duplication or disclosure of the computer software
22 // is subject to the restrictions set forth by 60East Technologies Inc..
23 //
25 #ifndef _AMPS_IMPL_H_
26 #define _AMPS_IMPL_H_
27 #ifndef _WIN32
28  #define _GNU_SOURCE 1
29 #endif
30 #define _AMPS_BUILD_C_CLIENT 1
31 #include <amps/amps.h>
32 #include <assert.h>
33 #include <stdlib.h>
34 
35 #define AMPSASSERT assert
36 #define AMPS_THREAD_START_TIMEOUT 120
37 
38 #ifdef _WIN32
39 
40  #include <Windows.h>
41  #define _AMPS_SNPRINTF(buf_, count_, ...) _snprintf_s(buf_, count_, _TRUNCATE, __VA_ARGS__)
42  #define _AMPS_KEYCREATE TlsAlloc
43  #define _AMPS_TLS_KEY DWORD
44  char* _amps_strndup(const char* ptr_, size_t len_);
45  #define _AMPS_STRNDUP _amps_strndup
46  #define AMPS_CLOSESOCKET closesocket
47 
48 #else
49  #include <pthread.h>
50  #include <unistd.h>
51  #include <sys/socket.h>
52  #include <netinet/in.h>
53  #include <netinet/tcp.h>
54  #include <arpa/inet.h>
55  #define _AMPS_SNPRINTF snprintf
56  #define _AMPS_KEYCREATE pthread_key_create
57  #define _AMPS_TLS_KEY pthread_key_t
58  #define AMPS_CLOSESOCKET close
59  #define _AMPS_STRNDUP strndup
60 
61  #ifndef SO_DOMAIN
62  #define AMPS_SO_DOMAIN 39
63  #else
64  #define AMPS_SO_DOMAIN (SO_DOMAIN)
65  #endif
66 
67 #endif
68 
69 #define AMPS_MIN(x,y) ((x)<(y)?(x):(y))
70 #define AMPS_MAX(x,y) ((x)>(y)?(x):(y))
71 
72 typedef enum
73 {
74  AMPS_PREFER_IPV4,
75  AMPS_PREFER_IPV6
76 }
77 amps_ip_protocol_preference;
78 #define AMPS_DEFAULT_IP_PROTOCOL_PREFERENCE AMPS_PREFER_IPV4
79 
80 typedef struct
81 {
82  char clientName[128];
83  size_t transportType;
84  amps_handle transport;
85  char lastError[1024];
86 
87  amps_predisconnect_handler predisconnectHandler;
88  void* predisconnectHandlerUserData;
89  amps_handler disconnectHandler;
90  void* disconnectHandlerUserData;
91 
92  amps_handler messageHandler;
93  void* messageHandlerUserData;
94 
95  amps_transport_filter_function transportFilterFunction;
96  void* transportFilterFunctionUserData;
97 
98  amps_thread_created_callback threadCreatedCallback;
99  void* threadCreatedCallbackUserData;
100 
101  amps_thread_exit_callback threadExitCallback;
102  void* threadExitCallbackUserData;
103 
104  amps_http_preflight_callback httpPreflightCallback;
105  void* httpPreflightCallbackUserData;
106 
107  amps_uint64_t batchSendSize;
108  amps_uint64_t batchSendTimeout;
109 }
110 amps_client_t;
111 
112 typedef struct
113 {
114  char name[8];
115  amps_handle(*createFunc)(void);
116  amps_result(*connectFunc)(amps_handle, const amps_char*);
117  amps_result(*predisconnectFunc)(amps_handle, amps_predisconnect_handler, void*);
118  amps_result(*disconnectFunc)(amps_handle, amps_handler, void*);
119  amps_result(*receiveFunc)(amps_handle, amps_handler, void*);
120  amps_result(*sendFunc)(amps_handle, amps_handle);
121  amps_result(*sendWithVersionFunc)(amps_handle, amps_handle, unsigned*);
122  amps_result(*sendBatchFunc)(amps_handle, amps_handle, unsigned*, int);
123  const amps_char* (*getError)(amps_handle);
124  void(*closeFunc)(amps_handle);
125  void(*destroyFunc)(amps_handle);
126  amps_result(*reconnectFunc)(amps_handle, unsigned);
127  amps_result(*setReadTimeoutFunc)(amps_handle, int);
128  amps_result(*setIdleTimeFunc)(amps_handle, int);
129  void(*setTransportFilterFunc)(amps_handle, amps_transport_filter_function, void*);
130  void(*setThreadCreatedCallback)(amps_handle, amps_thread_created_callback, void*);
131  void(*setThreadExitCallback)(amps_handle, amps_thread_exit_callback, void*);
132  void(*setHttpPreflightCallback)(amps_handle, amps_http_preflight_callback, void*);
133  void(*setBatchSend)(amps_handle, amps_uint64_t, amps_uint64_t);
134  AMPS_SOCKET(*getSocket)(amps_handle);
135 } transport_entry_t;
136 
137 extern transport_entry_t g_transports[];
138 extern size_t g_transport_count;
139 typedef struct
140 {
141  char* begin;
142  size_t length;
143  short owner;
144  size_t capacity;
145 } amps_field_t;
146 
147 
148 typedef struct
149 {
150  const char* rawBuffer;
151  size_t length;
152  size_t capacity;
153  unsigned long long bitmask;
154  amps_field_t fields[MESSAGEFIELDS];
155  amps_field_t data;
156 } amps_message_t;
157 
158 typedef struct
159 {
160  char name[8];
161  int(*serializeFunc)(amps_handle message, amps_char* buffer, size_t length);
162  amps_result(*preDeserializeFunc)(amps_handle message, const amps_char* buffer, size_t length);
163  amps_result(*deserializeFunc)(amps_handle message, size_t startingPosition, unsigned long* bytesRead);
164 } protocol_entry_t;
165 extern protocol_entry_t g_message_protocols[];
166 extern size_t g_message_protocol_count;
167 
168 
169 
175 amps_int64_t amps_message_get_protocol(
176  const amps_char* protocolname);
177 
178 int amps_message_serialize(
179  amps_handle message,
180  amps_int64_t serializer,
181  amps_char* buffer,
182  size_t length);
183 
184 amps_result amps_message_pre_deserialize(
185  amps_handle message,
186  amps_int64_t serializer,
187  const amps_char* buffer,
188  size_t length);
189 
190 amps_result amps_message_deserialize(
191  amps_handle message,
192  amps_int64_t serializer,
193  size_t startingPosition,
194  unsigned long* bytesRead);
195 
196 
200 amps_result amps_protocol_pre_deserialize(amps_handle message, const amps_char* buffer, size_t length);
201 amps_result amps_protocol_deserialize(amps_handle message, size_t startingPosition, unsigned long* bytesRead);
202 int amps_protocol_serialize(amps_handle message, amps_char* buffer, size_t length);
203 
204 #ifndef _WIN32
205  int amps_spin_lock_counted(pthread_mutex_t* lock_);
206  void amps_spin_lock_unlimited(pthread_mutex_t* lock_);
207  void amps_cleanup_unlock_mutex(void* m);
208  void amps_cleanup_free_buffer(void* data);
209  void amps_mutex_pair_atfork(void* vpMutex_, int code_);
210 #endif
211 
212 typedef void(*_amps_atfork_callback_function)(void*, int);
213 AMPSDLL void amps_atfork_init(void);
214 AMPSDLL void amps_atfork_add(void*, _amps_atfork_callback_function);
215 AMPSDLL void amps_atfork_remove(void*, _amps_atfork_callback_function);
216 
217 #ifdef AMPS_CPP_COUNT_THREADS
218 #if __STDC_VERSION__ >= 201100
219  extern _Atomic size_t amps_thread_create_count;
220  extern _Atomic size_t amps_thread_join_count;
221  extern _Atomic size_t amps_thread_detach_count;
222 #else
223  extern volatile size_t amps_thread_create_count;
224  extern volatile size_t amps_thread_join_count;
225  extern volatile size_t amps_thread_detach_count;
226 #endif
227 #endif
228 
229 
230 #endif