1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*-----------------------------------------------------------------------
File : cio_multiplexer.c
Author: Stephan Schulz (schulz@eprover.org)
Contents
Code for convenient multiplexing of many channels.
Copyright 2011 by the author.
This code is released under the GNU General Public Licence.
See the file COPYING in the main CLIB directory for details.
Run "eprover -h" for contact information.
Changes
<1> Fri Mar 11 20:35:33 CET 2011
New
-----------------------------------------------------------------------*/
#include "cio_multiplexer.h"
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: TCPChannelAlloc()
//
// Allocate a TCPChannelCell for the provided socket.
//
// Global Variables: -
//
// Side Effects : Memory management
//
/----------------------------------------------------------------------*/
TCPChannel_p TCPChannelAlloc(int sock)
{
TCPChannel_p handle = TCPChannelCellAlloc();
handle->sock = sock;
handle->in = PQueueAlloc();
handle->out = PQueueAlloc();
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: TCPChannelFree()
//
// Free a TCPChannel.
//
// Global Variables: -
//
// Side Effects : Memory management
//
/----------------------------------------------------------------------*/
void TCPChannelFree(TCPChannel_p junk)
{
TCPMsg_p handle;
while(!PQueueEmpty(junk->in))
{
handle = PQueueGetNextP(junk->in);
TCPMsgFree(handle);
}
PQueueFree(junk->in);
while(!PQueueEmpty(junk->out))
{
handle = PQueueGetNextP(junk->out);
TCPMsgFree(handle);
}
PQueueFree(junk->out);
if(junk->sock>=0)
{
close(junk->sock);
}
TCPChannelCellFree(junk);
}
/*-----------------------------------------------------------------------
//
// Function: TCPChannelClose()
//
// Close a TCP channel
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void TCPChannelClose(TCPChannel_p channel)
{
assert(channel->sock>=0);
VERBOSE(fprintf(stderr, "Closing channel %d\n", channel->sock););
close(channel->sock);
channel->sock = -1;
}
/*-----------------------------------------------------------------------
//
// Function: TCPChannelHasInMsg()
//
// Return true if there is a complete message in the channel.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
bool TCPChannelHasInMsg(TCPChannel_p channel)
{
TCPMsg_p handle;
if(PQueueEmpty(channel->in))
{
return false;
}
handle = PQueueLookP(channel->in);
if(TCP_MSG_COMPLETE(handle))
{
return true;
}
return false;
}
/*-----------------------------------------------------------------------
//
// Function: TCPChannelSendMsg()
//
// Add the message to the out queue.
//
// Global Variables: -
//
// Side Effects : IO, memory
//
/----------------------------------------------------------------------*/
void TCPChannelSendMsg(TCPChannel_p channel, TCPMsg_p msg)
{
PQueueStoreP(channel->out, msg);
}
/*-----------------------------------------------------------------------
//
// Function: TCPChannelSendStr()
//
// Add the string as a message to the out queue.
//
// Global Variables: -
//
// Side Effects : IO, memory
//
/----------------------------------------------------------------------*/
void TCPChannelSendStr(TCPChannel_p channel, char* str)
{
TCPMsg_p msg = TCPMsgPack(str);
TCPChannelSendMsg(channel, msg);
}
/*-----------------------------------------------------------------------
//
// Function: TCPChannelRead()
//
// Read data for one message from the socket - either the latest
// incomplete message, or a newly allocated one.
//
// Global Variables: -
//
// Side Effects : IO, memory.
//
/----------------------------------------------------------------------*/
MsgStatus TCPChannelRead(TCPChannel_p channel)
{
TCPMsg_p current;
MsgStatus res;
if(PQueueEmpty(channel->in))
{
current = TCPMsgAlloc();
PQueueStoreP(channel->in, current);
}
else
{
current = PQueueLookLastP(channel->in);
if(TCP_MSG_COMPLETE(current))
{
current = TCPMsgAlloc();
PQueueStoreP(channel->in, current);
}
}
res = TCPMsgRead(channel->sock, current);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: TCPChannelWrite()
//
// Write the current message (if any) as far as possible without
// blocking (i.e. try a single "write()"). If the message is
// complete, remove it from the queue.
//
// Global Variables: -
//
// Side Effects : IO, Memory
//
/----------------------------------------------------------------------*/
MsgStatus TCPChannelWrite(TCPChannel_p channel)
{
TCPMsg_p current;
MsgStatus res;
if(PQueueEmpty(channel->in))
{
return NWSuccess;
}
current = PQueueLookP(channel->in);
res = TCPMsgWrite(channel->sock, current);
if(res == NWSuccess)
{
current = PQueueGetNextP(channel->in);
TCPMsgFree(current);
}
return res;
}
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|