abyss_socket.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <sys/types.h>
00043 #include <assert.h>
00044 #include <netinet/in.h>
00045 #include <arpa/inet.h>
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048
00049 #include "abyss_mallocvar.h"
00050 #include "abyss_xmlrpc_int.h"
00051 #include <xmlrpc-c/abyss.h>
00052 #ifdef WIN32
00053 #include "socket_win.h"
00054 #else
00055 #include "abyss_socket_unix.h"
00056 #endif
00057 #include "abyss_socket.h"
00058
00059
00060 #ifdef WIN32
00061 #include "socket_win.h"
00062 #else
00063 #include "abyss_socket_unix.h"
00064 #endif
00065
00066 #include "abyss_socket.h"
00067
00068
00069 static void
00070 socketOsInit(abyss_bool * const succeededP) {
00071
00072 #ifdef WIN32
00073 SocketWinInit(succeededP);
00074 #else
00075 SocketUnixInit(succeededP);
00076 #endif
00077 }
00078
00079
00080
00081 static void
00082 socketOsTerm(void) {
00083
00084 #ifdef WIN32
00085 SocketWinTerm();
00086 #else
00087 SocketUnixTerm();
00088 #endif
00089 }
00090
00091
00092
00093 abyss_bool SocketTraceIsActive;
00094
00095 abyss_bool
00096 SocketInit(void) {
00097 abyss_bool retval;
00098
00099 socketOsInit(&retval);
00100
00101 SocketTraceIsActive = (getenv("ABYSS_TRACE_SOCKET") != NULL);
00102 if (SocketTraceIsActive)
00103 fprintf(stderr, "Abyss socket layer will trace socket traffic "
00104 "due to ABYSS_TRACE_SOCKET environment variable\n");
00105
00106 return retval;
00107 }
00108
00109
00110
00111 void
00112 SocketTerm(void) {
00113
00114 socketOsTerm();
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 static uint const socketSignature = 0x060609;
00129
00130 void
00131 SocketCreate(const struct TSocketVtbl * const vtblP,
00132 void * const implP,
00133 TSocket ** const socketPP) {
00134
00135 TSocket * socketP;
00136
00137 MALLOCVAR(socketP);
00138
00139 if (socketP) {
00140 socketP->implP = implP;
00141 socketP->vtbl = *vtblP;
00142 socketP->signature = socketSignature;
00143 *socketPP = socketP;
00144 }
00145 }
00146
00147
00148
00149 void
00150 SocketDestroy(TSocket * const socketP) {
00151
00152 assert(socketP->signature == socketSignature);
00153
00154 socketP->vtbl.destroy(socketP);
00155
00156 socketP->signature = 0;
00157
00158 free(socketP);
00159 }
00160
00161
00162
00163 void
00164 SocketWrite(TSocket * const socketP,
00165 const unsigned char * const buffer,
00166 uint32_t const len,
00167 abyss_bool * const failedP) {
00168
00169 (*socketP->vtbl.write)(socketP, buffer, len, failedP );
00170 }
00171
00172
00173
00174 uint32_t
00175 SocketRead(TSocket * const socketP,
00176 unsigned char * const buffer,
00177 uint32_t const len) {
00178
00179 return (*socketP->vtbl.read)(socketP, (char*)buffer, len);
00180 }
00181
00182
00183
00184 abyss_bool
00185 SocketConnect(TSocket * const socketP,
00186 TIPAddr * const addrP,
00187 uint16_t const portNumber) {
00188
00189 return (*socketP->vtbl.connect)(socketP, addrP, portNumber);
00190 }
00191
00192
00193
00194 abyss_bool
00195 SocketBind(TSocket * const socketP,
00196 TIPAddr * const addrP,
00197 uint16_t const portNumber) {
00198
00199 return (*socketP->vtbl.bind)(socketP, addrP, portNumber);
00200 }
00201
00202
00203
00204 abyss_bool
00205 SocketListen(TSocket * const socketP,
00206 uint32_t const backlog) {
00207
00208 return (*socketP->vtbl.listen)(socketP, backlog);
00209 }
00210
00211
00212
00213 void
00214 SocketAccept(TSocket * const listenSocketP,
00215 abyss_bool * const connectedP,
00216 abyss_bool * const failedP,
00217 TSocket ** const acceptedSocketPP,
00218 TIPAddr * const ipAddrP) {
00219
00220 (*listenSocketP->vtbl.accept)(listenSocketP,
00221 connectedP,
00222 failedP,
00223 acceptedSocketPP,
00224 ipAddrP);
00225 }
00226
00227
00228
00229 uint32_t
00230 SocketWait(TSocket * const socketP,
00231 abyss_bool const rd,
00232 abyss_bool const wr,
00233 uint32_t const timems) {
00234
00235 return (*socketP->vtbl.wait)(socketP, rd, wr, timems);
00236 }
00237
00238
00239
00240 uint32_t
00241 SocketAvailableReadBytes(TSocket * const socketP) {
00242
00243 return (*socketP->vtbl.availableReadBytes)(socketP);
00244 }
00245
00246
00247
00248 void
00249 SocketGetPeerName(TSocket * const socketP,
00250 TIPAddr * const ipAddrP,
00251 uint16_t * const portNumberP,
00252 abyss_bool * const successP) {
00253
00254 (*socketP->vtbl.getPeerName)(socketP, ipAddrP, portNumberP, successP);
00255 }
00256
00257
00258
00259 uint32_t
00260 SocketError(TSocket * const socketP) {
00261
00262 return (*socketP->vtbl.error)(socketP);
00263 }