AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.4.1
amps_impl.h
1 /*//////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2010-2023 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 
47 #else
48  #include <pthread.h>
49  #include <unistd.h>
50  #include <sys/socket.h>
51  #include <netinet/in.h>
52  #include <netinet/tcp.h>
53  #include <arpa/inet.h>
54  #define _AMPS_SNPRINTF snprintf
55  #define _AMPS_KEYCREATE pthread_key_create
56  #define _AMPS_TLS_KEY pthread_key_t
57  #define closesocket close
58  #define _AMPS_STRNDUP strndup
59 
60  #ifndef SO_DOMAIN
61  #define AMPS_SO_DOMAIN 39
62  #else
63  #define AMPS_SO_DOMAIN (SO_DOMAIN)
64  #endif
65 
66 #endif
67 
68 #define AMPS_MIN(x,y) ((x)<(y)?(x):(y))
69 #define AMPS_MAX(x,y) ((x)>(y)?(x):(y))
70 
71 typedef enum
72 {
73  AMPS_PREFER_IPV4,
74  AMPS_PREFER_IPV6
75 }
76 amps_ip_protocol_preference;
77 #define AMPS_DEFAULT_IP_PROTOCOL_PREFERENCE AMPS_PREFER_IPV4
78 
79 typedef struct
80 {
81  char clientName[128];
82  size_t transportType;
83  amps_handle transport;
84  char lastError[1024];
85 
86  amps_predisconnect_handler predisconnectHandler;
87  void* predisconnectHandlerUserData;
88  amps_handler disconnectHandler;
89  void* disconnectHandlerUserData;
90 
91  amps_handler messageHandler;
92  void* messageHandlerUserData;
93 
94  amps_transport_filter_function transportFilterFunction;
95  void* transportFilterFunctionUserData;
96 
97  amps_thread_created_callback threadCreatedCallback;
98  void* threadCreatedCallbackUserData;
99 }
100 amps_client_t;
101 
102 typedef struct
103 {
104  char name[8];
105  amps_handle(*createFunc)(void);
106  amps_result(*connectFunc)(amps_handle, const amps_char*);
107  amps_result(*predisconnectFunc)(amps_handle, amps_predisconnect_handler, void*);
108  amps_result(*disconnectFunc)(amps_handle, amps_handler, void*);
109  amps_result(*receiveFunc)(amps_handle, amps_handler, void*);
110  amps_result(*sendFunc)(amps_handle, amps_handle);
111  amps_result(*sendWithVersionFunc)(amps_handle, amps_handle, unsigned*);
112  const amps_char* (*getError)(amps_handle);
113  void(*closeFunc)(amps_handle);
114  void(*destroyFunc)(amps_handle);
115  amps_result(*reconnectFunc)(amps_handle, unsigned);
116  amps_result(*setReadTimeoutFunc)(amps_handle, int);
117  amps_result(*setIdleTimeFunc)(amps_handle, int);
118  void(*setTransportFilterFunc)(amps_handle, amps_transport_filter_function, void*);
119  void(*setThreadCreatedCallback)(amps_handle, amps_thread_created_callback, void*);
120  AMPS_SOCKET(*getSocket)(amps_handle);
121 } transport_entry_t;
122 
123 extern transport_entry_t g_transports[];
124 extern size_t g_transport_count;
125 typedef struct
126 {
127  char* begin;
128  size_t length;
129  short owner;
130  size_t capacity;
131 } amps_field_t;
132 
133 
134 typedef struct
135 {
136  const char* rawBuffer;
137  size_t length;
138  size_t capacity;
139  unsigned long long bitmask;
140  amps_field_t fields[MESSAGEFIELDS];
141  amps_field_t data;
142 } amps_message_t;
143 
144 typedef struct
145 {
146  char name[8];
147  int(*serializeFunc)(amps_handle message, amps_char* buffer, size_t length);
148  amps_result(*preDeserializeFunc)(amps_handle message, const amps_char* buffer, size_t length);
149  amps_result(*deserializeFunc)(amps_handle message, size_t startingPosition, unsigned long* bytesRead);
150 } protocol_entry_t;
151 extern protocol_entry_t g_message_protocols[];
152 extern size_t g_message_protocol_count;
153 
154 
155 
161 amps_int64_t amps_message_get_protocol(
162  const amps_char* protocolname);
163 
164 int amps_message_serialize(
165  amps_handle message,
166  amps_int64_t serializer,
167  amps_char* buffer,
168  size_t length);
169 
170 amps_result amps_message_pre_deserialize(
171  amps_handle message,
172  amps_int64_t serializer,
173  const amps_char* buffer,
174  size_t length);
175 
176 amps_result amps_message_deserialize(
177  amps_handle message,
178  amps_int64_t serializer,
179  size_t startingPosition,
180  unsigned long* bytesRead);
181 
182 
186 amps_result amps_protocol_pre_deserialize(amps_handle message, const amps_char* buffer, size_t length);
187 amps_result amps_protocol_deserialize(amps_handle message, size_t startingPosition, unsigned long* bytesRead);
188 int amps_protocol_serialize(amps_handle message, amps_char* buffer, size_t length);
189 
190 #ifndef _WIN32
191  int amps_spin_lock_counted(pthread_mutex_t* lock_);
192  void amps_spin_lock_unlimited(pthread_mutex_t* lock_);
193  void amps_cleanup_unlock_mutex(void* m);
194  void amps_cleanup_free_buffer(void* data);
195  void amps_mutex_pair_atfork(void* vpMutex_, int code_);
196 #endif
197 
198 typedef void(*_amps_atfork_callback_function)(void*, int);
199 AMPSDLL void amps_atfork_init(void);
200 AMPSDLL void amps_atfork_add(void*, _amps_atfork_callback_function);
201 AMPSDLL void amps_atfork_remove(void*, _amps_atfork_callback_function);
202 
203 
204 #endif