AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.5.17
amps_impl.h
1 /*//////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2010-2026 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 // Max 2s sleeps before givng up on the thread_created_callback
37 #ifdef _WIN32 // Sleep is about 15.6ms
38 #define AMPS_THREAD_START_TIMEOUT 128
39 #else
40 #define AMPS_THREAD_START_TIMEOUT 200
41 #endif
42 
43 #ifdef _WIN32
44 
45  #include <Windows.h>
46  #define _AMPS_SNPRINTF(buf_, count_, ...) _snprintf_s(buf_, count_, _TRUNCATE, __VA_ARGS__)
47  #define _AMPS_KEYCREATE TlsAlloc
48  #define _AMPS_TLS_KEY DWORD
49  char* _amps_strndup(const char* ptr_, size_t len_);
50  #define _AMPS_STRNDUP _amps_strndup
51  #define AMPS_CLOSESOCKET closesocket
52 
53 #else
54  #include <pthread.h>
55  #include <unistd.h>
56  #include <sys/socket.h>
57  #include <netinet/in.h>
58  #include <netinet/tcp.h>
59  #include <arpa/inet.h>
60  #define _AMPS_SNPRINTF snprintf
61  #define _AMPS_KEYCREATE pthread_key_create
62  #define _AMPS_TLS_KEY pthread_key_t
63  #define AMPS_CLOSESOCKET close
64  #define _AMPS_STRNDUP strndup
65 
66  #ifndef SO_DOMAIN
67  #define AMPS_SO_DOMAIN 39
68  #else
69  #define AMPS_SO_DOMAIN (SO_DOMAIN)
70  #endif
71 
72 #endif
73 
74 #define AMPS_MIN(x,y) ((x)<(y)?(x):(y))
75 #define AMPS_MAX(x,y) ((x)>(y)?(x):(y))
76 
77 typedef enum
78 {
79  AMPS_PREFER_IPV4,
80  AMPS_PREFER_IPV6
81 }
82 amps_ip_protocol_preference;
83 #define AMPS_DEFAULT_IP_PROTOCOL_PREFERENCE AMPS_PREFER_IPV4
84 
85 typedef struct
86 {
87  char clientName[128];
88  size_t transportType;
89  amps_handle transport;
90  char lastError[1024];
91 
92  amps_predisconnect_handler predisconnectHandler;
93  void* predisconnectHandlerUserData;
94  amps_handler disconnectHandler;
95  void* disconnectHandlerUserData;
96 
97  amps_handler messageHandler;
98  void* messageHandlerUserData;
99 
100  amps_transport_filter_function transportFilterFunction;
101  void* transportFilterFunctionUserData;
102 
103  amps_thread_created_callback threadCreatedCallback;
104  void* threadCreatedCallbackUserData;
105 
106  amps_thread_exit_callback threadExitCallback;
107  void* threadExitCallbackUserData;
108 
109  amps_http_preflight_callback httpPreflightCallback;
110  void* httpPreflightCallbackUserData;
111 
112  amps_uint64_t batchSendSize;
113  amps_uint64_t batchSendTimeout;
114 }
115 amps_client_t;
116 
117 typedef struct
118 {
119  char name[8];
120  amps_handle(*createFunc)(void);
121  amps_result(*connectFunc)(amps_handle, const amps_char*);
122  amps_result(*predisconnectFunc)(amps_handle, amps_predisconnect_handler, void*);
123  amps_result(*disconnectFunc)(amps_handle, amps_handler, void*);
124  amps_result(*receiveFunc)(amps_handle, amps_handler, void*);
125  amps_result(*sendFunc)(amps_handle, amps_handle);
126  amps_result(*sendWithVersionFunc)(amps_handle, amps_handle, unsigned*);
127  amps_result(*sendBatchFunc)(amps_handle, amps_handle, unsigned*, int);
128  const amps_char* (*getError)(amps_handle);
129  void(*closeFunc)(amps_handle);
130  void(*destroyFunc)(amps_handle);
131  amps_result(*reconnectFunc)(amps_handle, unsigned);
132  amps_result(*setReadTimeoutFunc)(amps_handle, int);
133  amps_result(*setIdleTimeFunc)(amps_handle, int);
134  void(*setTransportFilterFunc)(amps_handle, amps_transport_filter_function, void*);
135  void(*setThreadCreatedCallback)(amps_handle, amps_thread_created_callback, void*);
136  void(*setThreadExitCallback)(amps_handle, amps_thread_exit_callback, void*);
137  void(*setHttpPreflightCallback)(amps_handle, amps_http_preflight_callback, void*);
138  void(*setBatchSend)(amps_handle, amps_uint64_t, amps_uint64_t);
139  AMPS_SOCKET(*getSocket)(amps_handle);
140 } transport_entry_t;
141 
142 extern transport_entry_t g_transports[];
143 extern size_t g_transport_count;
144 typedef struct
145 {
146  char* begin;
147  size_t length;
148  short owner;
149  size_t capacity;
150 } amps_field_t;
151 
152 
153 typedef struct
154 {
155  const char* rawBuffer;
156  size_t length;
157  size_t capacity;
158  unsigned long long bitmask;
159  amps_field_t fields[MESSAGEFIELDS];
160  amps_field_t data;
161 } amps_message_t;
162 
163 typedef struct
164 {
165  char name[8];
166  int(*serializeFunc)(amps_handle message, amps_char* buffer, size_t length);
167  amps_result(*preDeserializeFunc)(amps_handle message, const amps_char* buffer, size_t length);
168  amps_result(*deserializeFunc)(amps_handle message, size_t startingPosition, unsigned long* bytesRead);
169 } protocol_entry_t;
170 extern protocol_entry_t g_message_protocols[];
171 extern size_t g_message_protocol_count;
172 
173 
174 
180 AMPSDLL amps_int64_t amps_message_get_protocol(
181  const amps_char* protocolname);
182 
183 AMPSDLL int amps_message_serialize(
184  amps_handle message,
185  amps_int64_t serializer,
186  amps_char* buffer,
187  size_t length);
188 
189 AMPSDLL amps_result amps_message_pre_deserialize(
190  amps_handle message,
191  amps_int64_t serializer,
192  const amps_char* buffer,
193  size_t length);
194 
195 AMPSDLL amps_result amps_message_deserialize(
196  amps_handle message,
197  amps_int64_t serializer,
198  size_t startingPosition,
199  unsigned long* bytesRead);
200 
201 
205 amps_result amps_protocol_pre_deserialize(amps_handle message, const amps_char* buffer, size_t length);
206 amps_result amps_protocol_deserialize(amps_handle message, size_t startingPosition, unsigned long* bytesRead);
207 int amps_protocol_serialize(amps_handle message, amps_char* buffer, size_t length);
208 
209 #ifndef _WIN32
210  int amps_spin_lock_counted(pthread_mutex_t* lock_);
211  void amps_spin_lock_unlimited(pthread_mutex_t* lock_);
212  void amps_cleanup_unlock_mutex(void* m);
213  void amps_cleanup_free_buffer(void* data);
214  void amps_mutex_pair_atfork(void* vpMutex_, int code_);
215 #endif
216 
217 typedef void(*_amps_atfork_callback_function)(void*, int);
218 AMPSDLL amps_result amps_atfork_init(void);
219 AMPSDLL amps_result amps_atfork_add(void*, _amps_atfork_callback_function);
220 AMPSDLL void amps_atfork_remove(void*, _amps_atfork_callback_function);
221 
222 #ifdef AMPS_CPP_COUNT_THREADS
223 #if __STDC_VERSION__ >= 201100
224  extern _Atomic size_t amps_thread_create_count;
225  extern _Atomic size_t amps_thread_join_count;
226  extern _Atomic size_t amps_thread_detach_count;
227 #else
228  extern volatile size_t amps_thread_create_count;
229  extern volatile size_t amps_thread_join_count;
230  extern volatile size_t amps_thread_detach_count;
231 #endif
232 #endif
233 
234 
235 #endif