AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.2.0
amps_impl.h
1 /*//////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2010-2020 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.h>
32 #include <assert.h>
33 #include <stdlib.h>
34 
35 #define AMPSASSERT assert
36 
37 #ifdef _WIN32
38 
39 #include <Windows.h>
40 #define _AMPS_SNPRINTF(buf_, count_, ...) _snprintf_s(buf_, count_, _TRUNCATE, __VA_ARGS__)
41 #define _AMPS_KEYCREATE TlsAlloc
42 #define _AMPS_TLS_KEY DWORD
43 
44 #else
45 #include <pthread.h>
46 #include <unistd.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <netinet/tcp.h>
50 #include <arpa/inet.h>
51 #include <malloc.h>
52 #define _AMPS_SNPRINTF snprintf
53 #define _AMPS_KEYCREATE pthread_key_create
54 #define _AMPS_TLS_KEY pthread_key_t
55 #define closesocket close
56 #endif
57 
58 #define AMPS_MIN(x,y) (x<y?x:y)
59 #define AMPS_MAX(x,y) (x>y?x:y)
60 
61 
62 typedef struct
63 {
64  char clientName[128];
65  size_t transportType;
66  amps_handle transport;
67  char lastError[1024];
68 
69  amps_predisconnect_handler predisconnectHandler;
70  void* predisconnectHandlerUserData;
71  amps_handler disconnectHandler;
72  void* disconnectHandlerUserData;
73 
74  amps_handler messageHandler;
75  void* messageHandlerUserData;
76 
77  amps_transport_filter_function transportFilterFunction;
78  void* transportFilterFunctionUserData;
79 
80  amps_thread_created_callback threadCreatedCallback;
81  void* threadCreatedCallbackUserData;
82 }
83 amps_client_t;
84 
85 typedef struct
86 {
87  char name[8];
88  amps_handle(*createFunc)(void);
89  amps_result(*connectFunc)(amps_handle, const amps_char*);
90  amps_result(*predisconnectFunc)(amps_handle, amps_predisconnect_handler, void*);
91  amps_result(*disconnectFunc)(amps_handle, amps_handler, void*);
92  amps_result(*receiveFunc)(amps_handle, amps_handler, void*);
93  amps_result(*sendFunc)(amps_handle, amps_handle);
94  amps_result(*sendWithVersionFunc)(amps_handle, amps_handle, unsigned*);
95  const amps_char*(*getError)(amps_handle);
96  void(*closeFunc)(amps_handle);
97  void(*destroyFunc)(amps_handle);
98  amps_result(*reconnectFunc)(amps_handle, unsigned);
99  amps_result(*setReadTimeoutFunc)(amps_handle,int);
100  amps_result(*setIdleTimeFunc)(amps_handle,int);
101  void(*setTransportFilterFunc)(amps_handle, amps_transport_filter_function, void*);
102  void(*setThreadCreatedCallback)(amps_handle, amps_thread_created_callback, void*);
103 } transport_entry_t;
104 
105 extern transport_entry_t g_transports[];
106 extern size_t g_transport_count;
107 typedef struct
108 {
109  char* begin;
110  size_t length;
111  short owner;
112  size_t capacity;
113 } amps_field_t;
114 
115 
116 typedef struct
117 {
118  const char* rawBuffer;
119  size_t length;
120  size_t capacity;
121  unsigned long long bitmask;
122  amps_field_t fields[MESSAGEFIELDS];
123  amps_field_t data;
124 } amps_message_t;
125 
126 typedef struct
127 {
128  char name[8];
129  int(*serializeFunc)(amps_handle message, amps_char* buffer, size_t length);
130  amps_result(*preDeserializeFunc)(amps_handle message, const amps_char* buffer, size_t length);
131  amps_result(*deserializeFunc)(amps_handle message, size_t startingPosition, unsigned long* bytesRead);
132 } protocol_entry_t;
133 extern protocol_entry_t g_message_protocols[];
134 extern size_t g_message_protocol_count;
135 
136 
137 
143 int amps_message_get_protocol(
144  const amps_char* protocolname);
145 
146 int amps_message_serialize(
147  amps_handle message,
148  int serializer,
149  amps_char* buffer,
150  size_t length);
151 
152 amps_result amps_message_pre_deserialize(
153  amps_handle message,
154  int serializer,
155  const amps_char* buffer,
156  size_t length);
157 
158 amps_result amps_message_deserialize(
159  amps_handle message,
160  int serializer,
161  size_t startingPosition,
162  unsigned long* bytesRead);
163 
164 
168 amps_result amps_protocol_pre_deserialize(amps_handle message,const amps_char* buffer,size_t length);
169 amps_result amps_protocol_deserialize(amps_handle message,size_t startingPosition,unsigned long* bytesRead);
170 int amps_protocol_serialize(amps_handle message,amps_char* buffer,size_t length);
171 
172 #ifndef _WIN32
173 int amps_spin_lock_counted(pthread_mutex_t* lock_);
174 void amps_spin_lock_unlimited(pthread_mutex_t* lock_);
175 void amps_cleanup_unlock_mutex(void* m);
176 void amps_cleanup_free_buffer(void* data);
177 #endif
178 
179 typedef void(*_amps_atfork_callback_function)(void*,int);
180 void amps_atfork_init(void);
181 void amps_atfork_add(void*, _amps_atfork_callback_function);
182 void amps_atfork_remove(void*);
183 
184 
185 #endif