AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.0.5
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 _snprintf
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_client_t;
78 
79 typedef struct
80 {
81  char name[8];
82  amps_handle(*createFunc)(void);
83  amps_result(*connectFunc)(amps_handle, const amps_char*);
84  amps_result(*predisconnectFunc)(amps_handle, amps_predisconnect_handler, void*);
85  amps_result(*disconnectFunc)(amps_handle, amps_handler, void*);
86  amps_result(*receiveFunc)(amps_handle, amps_handler, void*);
87  amps_result(*sendFunc)(amps_handle, amps_handle);
88  amps_result(*sendWithVersionFunc)(amps_handle, amps_handle, unsigned*);
89  const amps_char*(*getError)(amps_handle);
90  void(*closeFunc)(amps_handle);
91  void(*destroyFunc)(amps_handle);
92  amps_result(*reconnectFunc)(amps_handle, unsigned);
93  amps_result(*setReadTimeoutFunc)(amps_handle,int);
94  amps_result(*setIdleTimeFunc)(amps_handle,int);
95  amps_result(*setTransportFilterFunc)(amps_handle, amps_transport_filter_function, void*);
96 } transport_entry_t;
97 
98 extern transport_entry_t g_transports[];
99 extern size_t g_transport_count;
100 typedef struct
101 {
102  char* begin;
103  size_t length;
104  short owner;
105  size_t capacity;
106 } amps_field_t;
107 
108 
109 typedef struct
110 {
111  const char* rawBuffer;
112  size_t length;
113  size_t capacity;
114  unsigned long long bitmask;
115  amps_field_t fields[MESSAGEFIELDS];
116  amps_field_t data;
117 } amps_message_t;
118 
119 typedef struct
120 {
121  char name[8];
122  int(*serializeFunc)(amps_handle message, amps_char* buffer, size_t length);
123  amps_result(*preDeserializeFunc)(amps_handle message, const amps_char* buffer, size_t length);
124  amps_result(*deserializeFunc)(amps_handle message, size_t startingPosition, unsigned long* bytesRead);
125 } protocol_entry_t;
126 extern protocol_entry_t g_message_protocols[];
127 extern size_t g_message_protocol_count;
128 
129 
130 
136 int amps_message_get_protocol(
137  const amps_char* protocolname);
138 
139 int amps_message_serialize(
140  amps_handle message,
141  int serializer,
142  amps_char* buffer,
143  size_t length);
144 
145 amps_result amps_message_pre_deserialize(
146  amps_handle message,
147  int serializer,
148  const amps_char* buffer,
149  size_t length);
150 
151 amps_result amps_message_deserialize(
152  amps_handle message,
153  int serializer,
154  size_t startingPosition,
155  unsigned long* bytesRead);
156 
157 
161 amps_result amps_protocol_pre_deserialize(amps_handle message,const amps_char* buffer,size_t length);
162 amps_result amps_protocol_deserialize(amps_handle message,size_t startingPosition,unsigned long* bytesRead);
163 int amps_protocol_serialize(amps_handle message,amps_char* buffer,size_t length);
164 
165 #ifndef _WIN32
166 int amps_spin_lock_counted(pthread_mutex_t* lock_);
167 void amps_spin_lock_unlimited(pthread_mutex_t* lock_);
168 void amps_cleanup_unlock_mutex(void* m);
169 void amps_cleanup_free_buffer(void* data);
170 #endif
171 
172 typedef void(*_amps_atfork_callback_function)(void*,int);
173 void amps_atfork_init(void);
174 void amps_atfork_add(void*, _amps_atfork_callback_function);
175 void amps_atfork_remove(void*);
176 
177 
178 #endif