~ubuntu-branches/ubuntu/natty/xmlrpc-c/natty

« back to all changes in this revision

Viewing changes to lib/abyss/src/channel.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-01-06 18:56:02 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20110106185602-09og2x3suqlzbf6s
Tags: 1.16.32-0ubuntu1
* New upstream version (stable release). LP: #659591.
  - No unresolved symbols in the shared libraries. LP: #690779.
  - Builds with --no-add-needed and --as-needed.
* Rename shared library packages.
* Add symbols files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*============================================================================
 
2
  socket.c
 
3
==============================================================================
 
4
  Implementation of TChannel class: A generic channel over which one can
 
5
  transport a bidirectional stream of bytes.
 
6
 
 
7
  A TChannel is a lot like a POSIX stream socket in "connected" state.
 
8
============================================================================*/
 
9
 
 
10
#include <sys/types.h>
 
11
#include <assert.h>
 
12
#include <stdio.h>
 
13
#include <stdlib.h>
 
14
 
 
15
#include "bool.h"
 
16
#include "int.h"
 
17
#include "mallocvar.h"
 
18
#include "xmlrpc-c/util_int.h"
 
19
#include "xmlrpc-c/abyss.h"
 
20
#ifdef WIN32
 
21
  #include "socket_win.h"
 
22
#else
 
23
  #include "socket_unix.h"
 
24
#endif
 
25
#include "channel.h"
 
26
 
 
27
 
 
28
 
 
29
static void
 
30
socketOsInit(const char ** const errorP) {
 
31
 
 
32
#ifdef WIN32
 
33
    SocketWinInit(errorP);
 
34
#else
 
35
    SocketUnixInit(errorP);
 
36
#endif
 
37
}
 
38
 
 
39
 
 
40
 
 
41
static void
 
42
socketOsTerm(void) {
 
43
 
 
44
#ifdef WIN32
 
45
    SocketWinTerm();
 
46
#else
 
47
    SocketUnixTerm();
 
48
#endif
 
49
}
 
50
    
 
51
 
 
52
 
 
53
bool ChannelTraceIsActive;
 
54
 
 
55
void
 
56
ChannelInit(const char ** const errorP) {
 
57
 
 
58
    socketOsInit(errorP);
 
59
 
 
60
    if (!*errorP) {
 
61
        ChannelTraceIsActive = (getenv("ABYSS_TRACE_CHANNEL") != NULL);
 
62
        if (ChannelTraceIsActive)
 
63
            fprintf(stderr, "Abyss channel layer will trace channel traffic "
 
64
                    "due to ABYSS_TRACE_CHANNEL environment variable\n");
 
65
    }
 
66
}
 
67
 
 
68
 
 
69
 
 
70
void
 
71
ChannelTerm(void) {
 
72
 
 
73
    socketOsTerm();
 
74
}
 
75
 
 
76
 
 
77
 
 
78
/* ChannelCreate() is not exported to the Abyss user.  It is meant to
 
79
   be used by an implementation-specific TChannel generator which is
 
80
   exported to the Abyss user, e.g. ChannelCreateUnix() in
 
81
   socket_unix.c
 
82
 
 
83
   The TChannel generator functions are the _only_ user-accessible
 
84
   functions that are particular to an implementation.
 
85
*/
 
86
 
 
87
static unsigned int const channelSignature = 0x06060B;
 
88
 
 
89
void
 
90
ChannelCreate(const struct TChannelVtbl * const vtblP,
 
91
              void *                      const implP,
 
92
              TChannel **                 const channelPP) {
 
93
 
 
94
    TChannel * channelP;
 
95
 
 
96
    MALLOCVAR(channelP);
 
97
 
 
98
    if (channelP) {
 
99
        channelP->implP = implP;
 
100
        channelP->vtbl = *vtblP;
 
101
        channelP->signature = channelSignature;
 
102
        *channelPP = channelP;
 
103
 
 
104
        if (ChannelTraceIsActive)
 
105
            fprintf(stderr, "Created channel %p\n", channelP);
 
106
    }
 
107
}
 
108
 
 
109
 
 
110
 
 
111
void
 
112
ChannelDestroy(TChannel * const channelP) {
 
113
 
 
114
    if (ChannelTraceIsActive)
 
115
        fprintf(stderr, "Destroying channel %p\n", channelP);
 
116
 
 
117
    assert(channelP->signature == channelSignature);
 
118
 
 
119
    channelP->vtbl.destroy(channelP);
 
120
 
 
121
    channelP->signature = 0;  /* For debuggability */
 
122
 
 
123
    free(channelP);
 
124
}
 
125
 
 
126
 
 
127
 
 
128
void
 
129
ChannelWrite(TChannel *            const channelP,
 
130
             const unsigned char * const buffer,
 
131
             uint32_t              const len,
 
132
             bool *                const failedP) {
 
133
 
 
134
    if (ChannelTraceIsActive)
 
135
        fprintf(stderr, "Writing %u bytes to channel %p\n", len, channelP);
 
136
 
 
137
    (*channelP->vtbl.write)(channelP, buffer, len, failedP);
 
138
}
 
139
 
 
140
 
 
141
 
 
142
void
 
143
ChannelRead(TChannel *      const channelP, 
 
144
            unsigned char * const buffer, 
 
145
            uint32_t        const len,
 
146
            uint32_t *      const bytesReceivedP,
 
147
            bool *          const failedP) {
 
148
    
 
149
    if (ChannelTraceIsActive)
 
150
        fprintf(stderr, "Reading %u bytes from channel %p\n", len, channelP);
 
151
 
 
152
    (*channelP->vtbl.read)(channelP, buffer, len, bytesReceivedP, failedP);
 
153
}
 
154
 
 
155
 
 
156
 
 
157
void
 
158
ChannelWait(TChannel * const channelP,
 
159
            bool       const waitForRead,
 
160
            bool       const waitForWrite,
 
161
            uint32_t   const timems,
 
162
            bool *     const readyToReadP,
 
163
            bool *     const readyToWriteP,
 
164
            bool *     const failedP) {
 
165
 
 
166
    if (ChannelTraceIsActive) {
 
167
        if (waitForRead)
 
168
            fprintf(stderr, "Waiting %u milliseconds for data from "
 
169
                    "channel %p\n", timems, channelP);
 
170
        if (waitForWrite)
 
171
            fprintf(stderr, "Waiting %u milliseconds for channel %p "
 
172
                    "to be writable\n", timems, channelP);
 
173
    }
 
174
    (*channelP->vtbl.wait)(channelP, waitForRead, waitForWrite, timems,
 
175
                           readyToReadP, readyToWriteP, failedP);
 
176
}
 
177
 
 
178
 
 
179
 
 
180
void
 
181
ChannelInterrupt(TChannel * const channelP) {
 
182
 
 
183
    if (ChannelTraceIsActive)
 
184
        fprintf(stderr, "Interrupting channel waits");
 
185
 
 
186
    (*channelP->vtbl.interrupt)(channelP);
 
187
}
 
188
 
 
189
 
 
190
 
 
191
void
 
192
ChannelFormatPeerInfo(TChannel *    const channelP,
 
193
                      const char ** const peerStringP) {
 
194
 
 
195
    (*channelP->vtbl.formatPeerInfo)(channelP, peerStringP);
 
196
}
 
197