TCPChannels.icl 24.6 KB
Newer Older
1 2 3
implementation module TCPChannels

import	StdEnv
4
from StdFunc import seq
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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
import	TCPDef, TCPChannelClass
import	tcp, ostcp, tcp_bytestreams

:: TCP_Void = TCP_Void
:: TCP_Pair a b = TCP_Pair a b

//////////////////////// Listeners ////////////////////////////////////

instance Receive	TCP_Listener_
  where
	receive_MT mbTimeout ch env
		# endpointRef					= unpack_tcplistener ch
		  (toReport, mbHostDuplexChan, env)	= receive_mb_Listener mbTimeout endpointRef env
		= (toReport, Cast mbHostDuplexChan, pack_tcplistener endpointRef, env)
	receiveUpTo max ch env
		= receiveUpToGeneral [] max ch env
	available ch env
		# endpointRef			= unpack_tcplistener ch
		  (cReqAvail, env)		= os_connectrequestavailable endpointRef env
		= (cReqAvail, pack_tcplistener endpointRef, env)
	eom ch env
		= (False, ch, env)

receiveUpToGeneral akku max ch env
	|	max<=0
		= (u_reverse akku, ch, env)
	#!	(tReport, mbData, ch, env)	= receive_MT (Just 0) ch env
	|	tReport<>TR_Success
		= (u_reverse akku, ch, env)
	= receiveUpToGeneral [fromJust mbData:akku] (dec max) ch env

instance closeRChannel	TCP_Listener_
  where
	closeRChannel ch env
		# endpointRef			= unpack_tcplistener ch
		= close_listener endpointRef env

receive_mb_Listener	::	!(Maybe Timeout) !EndpointRef !*env
						->	(!TimeoutReport, !Maybe (!IPAddress, !TCP_DuplexChannel), !*env) | ChannelEnv env
receive_mb_Listener mbTimeout endpointRef env
	|	isJust mbTimeout && (fromJust mbTimeout)<0
		= (TR_Expired, Nothing, env)
	#!	(cReqAvail, env)		= os_connectrequestavailable endpointRef env
	= if cReqAvail (accept endpointRef env) (wait endpointRef env)
  where
	wait endpointRef env
		#!	(isIOProg, env)		= channelEnvKind env
			(mbStopTime, env)	= getMbStopTime mbTimeout env
			(errCode, env)		= selectChC isIOProg False mbStopTime {endpointRef} {LISTENER} {} env
		| errCode<>0	// the timeout expired (or some fatal error)
			= (TR_Expired, Nothing, env)
		= accept endpointRef env
	accept endpointRef env
		# ((errCode,host,newEpRef), env)
					= acceptC endpointRef env	
		| errCode<>0
			= (TR_NoSuccess, Nothing, env)
		# (duplexChan, env)		= createDuplexChan newEpRef env
		= (TR_Success, Just (pack_ipaddr host, duplexChan), env)

		
/////////////////////// TCP channels /////////////////////////////////

instance Receive	TCP_RChannel_
  where
	receive_MT mbTimeout ch env
		# rchan						= unpack_tcprchan ch
		  (toReport, mbByteSeq, env)= receive_mb_TCP mbTimeout rchan env
		= (toReport, Cast mbByteSeq, pack_tcprchan rchan, env)
	receiveUpTo max ch env
		= receiveUpToGeneral [] max ch env
	available ch env
		# (rchan=:(endpointRef,_))	= unpack_tcprchan ch
		  (isAvailable, env)		= data_availableC endpointRef env
		= (isAvailable, pack_tcprchan rchan, env)
	eom ch env
		# (rchan=:(endpointRef,_))	= unpack_tcprchan ch
		  (isEom, env)				= os_eom endpointRef env
		= (isEom, pack_tcprchan rchan, env)

instance closeRChannel	TCP_RChannel_
  where
	closeRChannel ch env
		# (endpointRef, _)			= unpack_tcprchan ch
		= close_tcprchan endpointRef env

receive_mb_TCP ::	!(Maybe Timeout) !(!EndpointRef,!Int) !*env 
					->	(!TimeoutReport, !Maybe ByteSeq, !*env)
					| 	ChannelEnv env
receive_mb_TCP mbTimeout rchan=:(endpointRef,maxSize) env
	|	isJust mbTimeout && (fromJust mbTimeout)<0
		= (TR_Expired, Nothing, env)
	#!	(receivedData, env)	= receiveC endpointRef maxSize env	
	|	size receivedData>0
		= (TR_Success, Just (toByteSeq receivedData), env)
  	#!	(mbStopTime, env)	= getMbStopTime mbTimeout env
		(isIOProg, env)		= channelEnvKind env
		(errCode, env) = selectChC isIOProg False mbStopTime {endpointRef} {RCHANNEL} {} env
	|	errCode	== 1	// timeout expired
		= (TR_Expired, Nothing, env)
	|	errCode == 3	// some error
		= (TR_NoSuccess, Nothing, env)
	#!	(receivedData, env)	= receiveC endpointRef maxSize env	
	|	size receivedData>0
		= (TR_Success, Just (toByteSeq receivedData), env)
	= (TR_NoSuccess, Nothing, env)

instance Send		TCP_SChannel_
  where
	send_MT mbTimeout byteSeqPolymorphic ch env
		= nsend_MT mbTimeout [byteSeqPolymorphic] ch env
	nsend_MT mbTimeout byteSeqsPolymorphic ch env
		#!	byteSeqs			= castToByteSeqList byteSeqsPolymorphic
			chan				= unpack_tcpschan ch
			chan				= seq (map addToBuffer byteSeqs) chan
			(toExpired, sentBytes, bNormal, env)
								= flushBuffers	mbTimeout chan.bEndpointRef chan.bNormal env
			chan				= { chan & 	bNormal=bNormal, bUnsent=chan.bUnsent-sentBytes}
		= (toExpired, sentBytes, pack_tcpschan chan, env)
	flushBuffer_MT mbTimeout ch env 
		#!	chan						= unpack_tcpschan ch
			(toExpired, flushedBytes, bNormal, env)	= flushBuffers mbTimeout chan.bEndpointRef chan.bNormal env
		= (toExpired, flushedBytes, pack_tcpschan { chan & bNormal =bNormal, bUnsent=chan.bUnsent-flushedBytes}, env)
	closeChannel_MT mbTimeout ch env
		#!	chan				= unpack_tcpschan ch
			endpointRef			= chan.bEndpointRef
			(toExpired, sentBytes, _, env)
								= flushBuffers mbTimeout endpointRef chan.bNormal env
			((referenceCount,hr,hs,aborted),env)
							= getEndpointDataC endpointRef env
			env				= mb_close_inet_receiver_without_id	hs (endpointRef, SChanReceiver) env
			env = setEndpointData_no_new_notifiersC endpointRef (dec referenceCount) hr False aborted env
			env	= case referenceCount of
					1	->	disconnectGracefulC endpointRef env
					_	->	env
			env	= garbageCollectEndpointC endpointRef env
		= (toExpired, sentBytes, env)
	abortConnection ch env
		#	chan				= unpack_tcpschan ch
			endpointRef			= chan.bEndpointRef
			((referenceCount,hr,hs,_),env)
							= getEndpointDataC endpointRef env
			env				= mb_close_inet_receiver_without_id	hs (endpointRef, SChanReceiver) env
			env = setEndpointData_no_new_notifiersC endpointRef (dec referenceCount) hr False True env
			env	= case referenceCount of
						1	-> disconnectBrutalC endpointRef env
						_	-> env
			env	= garbageCollectEndpointC endpointRef env
		= env
	disconnected ch env
		# chan	= unpack_tcpschan ch
		  (isDisconnected, env)	= os_disconnected chan.bEndpointRef env
		= (isDisconnected, pack_tcpschan chan, env)
	bufferSize ch
		# chan				= unpack_tcpschan ch
		= (chan.bUnsent, pack_tcpschan chan)


addToBuffer :: !ByteSeq !u:Buffered_SChan -> v:Buffered_SChan, [u <= v]
addToBuffer byteSeq chan
	# data		= toString byteSeq
	| size data==0
		= chan 
	= { chan & bNormal=addPacket data chan.bNormal, bUnsent=chan.bUnsent+(size data)}
  where
	addPacket data {bPackets=[]}
		= { bPackets=[data], bBegin=0 }
	addPacket data buffer=:{bPackets}
		= { buffer & bPackets=bPackets++[data] }

flushBuffers mbTimeout endpointRef buffer env
	#!	(isIOProg,env)		= channelEnvKind env
		(mbStopTime, env)	= getMbStopTime mbTimeout env
		nonBlocking			= isJust mbTimeout && fromJust mbTimeout==0
	= flushBuffers_Loop nonBlocking mbStopTime isIOProg endpointRef buffer 0 env
	
flushBuffers_Loop	::	!Bool !(!Bool, !Int) !Int !EndpointRef !Buffer !Int !*env
				->	(!TimeoutReport, !Int, !Buffer, !*env)
				|	ChannelEnv env
flushBuffers_Loop _ _ _ _ buffer=:{bPackets=[]} akku env
	= (TR_Success, akku, buffer, env)
flushBuffers_Loop	nonBlocking mbStopTime isIOProg endpointRef 
				buffer=:{bPackets=bPackets=:[first_packet:packets], bBegin} akku env
	#!	(isDisconnected, env)	=  os_disconnected endpointRef env
	|	isDisconnected
		= (TR_NoSuccess, akku, buffer, env)
	#!	bytesToSend				= (size first_packet) - bBegin
		((errCode, bytesSent), env)		= sendC endpointRef first_packet bBegin bytesToSend env
	|	errCode<>0
		= (TR_NoSuccess, akku, buffer, env)
	|	bytesSent==bytesToSend
		= flushBuffers_Loop nonBlocking mbStopTime isIOProg endpointRef {bPackets=packets, bBegin=0} (bytesSent+akku) env
	|	nonBlocking
		= (TR_Expired, bytesSent+akku, {buffer & bBegin=bBegin+bytesSent}, env)
	#!	(errCode, env)			= selectChC isIOProg False mbStopTime {} {} {endpointRef} env
	|	errCode	== 1	// timeout expired
		= (TR_Expired, bytesSent+akku, {buffer & bBegin=bBegin+bytesSent}, env)
	|	errCode == 3	// some error
		= (TR_NoSuccess, bytesSent+akku, {buffer & bBegin=bBegin+bytesSent}, env)
	= flushBuffers_Loop	nonBlocking mbStopTime isIOProg endpointRef {buffer & bBegin=bBegin+bytesSent} 
						(bytesSent+akku) env
lookupIPAddress		::	!String !*env
				->	(!Maybe IPAddress, !*env)
				|	ChannelEnv env
lookupIPAddress inetAddr env
	# ((errCode, inetHost), env)	= lookupHost_syncC (inetAddr+++"\0") env
	| errCode<>0
		= (Nothing, env)
	= (Just (pack_ipaddr inetHost), env)

connectTCP_MT		::	!(Maybe Timeout) !(!IPAddress,!Port) !*env
					->	(!TimeoutReport, !Maybe TCP_DuplexChannel, !*env) 
					|	ChannelEnv env
connectTCP_MT mbTimeout (inetHost,inetPort) env
	#!	destination						= (unpack_ipaddr inetHost, inetPort)
		(chanEnvKind,env)				= channelEnvKind env
		(mbStopTime, env)				= getMbStopTime mbTimeout env
		((errCode,timeoutExpired,endpointRef), env)
			= os_connectTCP_sync chanEnvKind mbStopTime destination env
	| timeoutExpired
		= (TR_Expired, Nothing, env)
	| errCode<>0
		= (TR_NoSuccess, Nothing, env)
	#!	(duplexChan, env)					= createDuplexChan endpointRef env
	= (TR_Success, Just duplexChan, env)

openTCP_Listener	::	!Port !*env
					->	(!Bool, !Maybe TCP_Listener, !*env)
					|	ChannelEnv env
openTCP_Listener port env
	# ((errCode,endpointRef), env)		= openTCP_ListenerC port env
	| errCode<>0
		= (False, Nothing, env)
	= (True, Just (pack_tcplistener endpointRef), env)
	
instance MaxSize TCP_RChannel_
  where
	setMaxSize newMaxLength tcp_rchan
		# (endpointRef,_)	= unpack_tcprchan tcp_rchan
		= pack_tcprchan (endpointRef, max newMaxLength 0)
	getMaxSize tcp_rchan
		# (ch=:(_,maxSize))	= unpack_tcprchan tcp_rchan
		= (maxSize, pack_tcprchan ch)
	clearMaxSize tcp_rchan
		# (endpointRef,_)	= unpack_tcprchan tcp_rchan
		= pack_tcprchan (endpointRef, 0)

//////////////// helpers //////////////////////////

createDuplexChan	::	!EndpointRef !*env	->	(!TCP_DuplexChannel, !*env)	| ChannelEnv env
createDuplexChan endpointRef env
//	# (id, env)	= openId env
	# tcp_SChan	=	{	bEndpointRef	=	endpointRef
					,	bNormal			=	emptyBuffer
					,	bUnsent			=	0
					,	bId				=	0 // id
					}
	= ({sChannel= pack_tcpschan tcp_SChan, rChannel=pack_tcprchan (endpointRef, 0)}, env)

emptyBuffer = {bPackets=[], bBegin=0}

/*	Conversion functions:
	Cast contains abc code because it can't be typed conventionally. (JVG/RWS)
*/

castToByteSeqList :: !.a -> [ByteSeq]
castToByteSeqList x
	= Cast x

castToChList :: !.a -> .[Char]
castToChList x
	= Cast x

castToChar :: !.a -> .Char
castToChar x
	= Cast x

Cast :: !.a -> .b
Cast a
	= code
		{
			pop_a 0
		}

u_reverse::![.a] -> [.a]
u_reverse list = reverse_ list []
where 
	reverse_ [hd:tl] list	= reverse_ tl [hd:list]
	reverse_ [] list		= list

u_length l
	= u_length_ l [] 0
  where
	u_length_ [] akku n
		= (n, u_reverse akku)
	u_length_ [h:t] akku n
		= u_length_ t [h:akku] (inc n)
	
///////////////////// for selecting on channels ///////////////////////

class SelectReceive channels
  where
	accRChannels	:: (PrimitiveRChannel -> (x, PrimitiveRChannel)) !*channels 
					-> (![x], !*channels)
309 310
	getRState		:: !Int !*channels !*env
					-> (!Maybe SelectResult, !*channels, !*env) | ChannelEnv env
311 312 313 314 315

class SelectSend channels
  where
	accSChannels	:: (TCP_SChannel -> *(.x, TCP_SChannel)) !*channels
					-> (![.x], !*channels)
316 317
	appDisconnected	:: !Int !*channels !*env
					-> (!Bool, !*channels, !*env) | ChannelEnv env
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501

class getNrOfChannels channels	:: !*channels
								-> (!Int, !*channels)

instance SelectSend (TCP_Pair *x *y)			| SelectSend, getNrOfChannels x & SelectSend y
  where
	accSChannels f (TCP_Pair l_channels r_channels)
		#!	(l, l_channels)	= accSChannels f l_channels
			(r, r_channels)	= accSChannels f r_channels
		= (l++r, TCP_Pair l_channels r_channels)
	appDisconnected n (TCP_Pair l_channels r_channels) env
		#!	(l_length, l_channels)	= getNrOfChannels l_channels
		|	n<l_length
			#!	(result, l_channels, env)	= appDisconnected n l_channels env
			= (result, TCP_Pair l_channels r_channels, env)
		#!	(result, r_channels, env)	= appDisconnected (n-l_length) r_channels env
		= (result, TCP_Pair l_channels r_channels, env)

instance SelectSend TCP_SChannels
  where
	accSChannels f (TCP_SChannels channels)
		#!	(l,channels)	=  unzip (map f channels)
		= (l, TCP_SChannels channels)
	appDisconnected n (TCP_SChannels channels) env
		#!	(l,[channel:r])			= splitAt n channels
			(isDisconnected, channel, env)	= disconnected channel env
		= (isDisconnected, TCP_SChannels (l ++ [channel:r]), env)

instance SelectSend TCP_SCharStreams
  where
	accSChannels f (TCP_SCharStreams channels)
		= accSChannelsA f channels []
	  where
		accSChannelsA f [] akku
			#! (l, channels)	= unzip (u_reverse akku)
			= (l, TCP_SCharStreams channels)
		accSChannelsA f [{sbs_schan}:channels] akku
			#!	(x, sbs_schan)	= f sbs_schan
			= accSChannelsA f channels [(x,{sbs_schan=sbs_schan}): akku]
	appDisconnected n (TCP_SCharStreams channels) env
		#!	(l,[channel:r])			= splitAt n channels
			(isDisconnected, channel, env)	= disconnected channel env
		= (isDisconnected, TCP_SCharStreams (l ++ [channel:r]), env)

instance SelectSend TCP_Void
  where
	accSChannels _ void
		= ([],void)
	appDisconnected _ _ _
		= abort "StdTCPChannels: error: tried to apply appDisconnected to an object of type TCP_Void"

instance SelectReceive (TCP_Pair *x *y)			| SelectReceive, getNrOfChannels x & SelectReceive y
  where
	accRChannels f (TCP_Pair l_channels r_channels)
		#!	(l, l_channels)	= accRChannels f l_channels
			(r, r_channels)	= accRChannels f r_channels
		= (l++r, TCP_Pair l_channels r_channels)
	getRState n (TCP_Pair l_channels r_channels) env
		#!	(l_length, l_channels)	= getNrOfChannels l_channels
		|	n<l_length
			#!	(result, l_channels, env)	= getRState n l_channels env
			= (result, TCP_Pair l_channels r_channels, env)
		#!	(result, r_channels, env)	= getRState (n-l_length) r_channels env
		= (result, TCP_Pair l_channels r_channels, env)

instance SelectReceive TCP_RChannels
  where
	accRChannels f (TCP_RChannels channels)
		#!	(l,channels)	=  unzip (map f (map TCP_RCHANNEL channels))
		= (l, TCP_RChannels (map fromTCP_RCHANNEL channels))
	  where
		fromTCP_RCHANNEL (TCP_RCHANNEL channel)
			= channel
	getRState n (TCP_RChannels channels) env
		#!	(l,[channel:r])			= splitAt n channels
			(state, channel, env)	= getStateRchan channel env
		= (state, TCP_RChannels (l ++ [channel:r]), env)

instance SelectReceive TCP_Listeners
  where
	accRChannels f (TCP_Listeners channels)
		#!	(l,channels)	=  unzip (map f (map TCP_LISTENER channels))
		= (l, TCP_Listeners (map fromTCP_LISTENER channels))
	  where
		fromTCP_LISTENER (TCP_LISTENER channel)
			= channel
	getRState n (TCP_Listeners channels) env
		#!	(l,[channel:r])			= splitAt n channels
			(state, channel, env)	= getState channel env
		= (state, TCP_Listeners (l ++ [channel:r]), env)
	  where
		getState listener env
			#!	endpointRef				= unpack_tcplistener listener
				(cReqAvail, env)		= os_connectrequestavailable endpointRef env
			= ( if cReqAvail (Just SR_Available) Nothing,
				pack_tcplistener endpointRef,
				env)

instance SelectReceive TCP_RCharStreams
  where
	accRChannels f (TCP_RCharStreams channels)
		= accRChannelsA f channels []
	  where
		accRChannelsA f [] akku
			#! (l, channels)	= unzip (u_reverse akku)
			= (l, TCP_RCharStreams channels)
		accRChannelsA f [rbs=:{rbs_rchan}:channels] akku
			#!	(x, TCP_RCHANNEL rbs_rchan)	= f (TCP_RCHANNEL rbs_rchan)
			= accRChannelsA f channels [(x,{rbs & rbs_rchan=rbs_rchan}): akku]
	getRState n (TCP_RCharStreams channels) env
		#!	(l,[channel:r])			= splitAt n channels
			(state, channel, env)	= getStateRchan channel env
		= (state, TCP_RCharStreams (l ++ [channel:r]), env)

instance SelectReceive TCP_Void
  where
	accRChannels _ void
		= ([],void)
	getRState _ void env
		= (Nothing, void, env)
		
getStateRchan channel env
	#!	(isAvailable, channel, env) = available channel env
	|	isAvailable
		= (Just SR_Available, channel, env)
	#!	(isEom, channel, env) = eom channel env
	|	isEom
		= (Just SR_EOM, channel, env)
	= (Nothing, channel, env)

instance getNrOfChannels (TCP_Pair *x *y)			| getNrOfChannels x & getNrOfChannels y
  where
	getNrOfChannels (TCP_Pair l r)
		#!	(nl, l)	= 	getNrOfChannels l
			(nr, r)	= 	getNrOfChannels r
		= (nl+nr, TCP_Pair l r)
instance getNrOfChannels TCP_RChannels
  where
	getNrOfChannels (TCP_RChannels channels)
		#!	(n, channels)	= u_length channels
		= (n, TCP_RChannels channels)
instance getNrOfChannels TCP_Listeners
  where
	getNrOfChannels (TCP_Listeners channels)
		#!	(n, channels)	= u_length channels
		= (n, TCP_Listeners channels)
instance getNrOfChannels TCP_RCharStreams
  where
	getNrOfChannels (TCP_RCharStreams channels)
		#!	(n, channels)	= u_length channels
		= (n, TCP_RCharStreams channels)
instance getNrOfChannels TCP_SChannels
  where
	getNrOfChannels (TCP_SChannels channels)
		#!	(n, channels)	= u_length channels
		= (n, TCP_SChannels channels)
instance getNrOfChannels TCP_SCharStreams
  where
	getNrOfChannels (TCP_SCharStreams channels)
		#!	(n, channels)	= u_length channels
		= (n, TCP_SCharStreams channels)
instance getNrOfChannels TCP_Void
  where
	getNrOfChannels void
		= (0, void)

instance == SelectResult
  where
	(==) SR_Available x		= case x of	SR_Available	-> True
										_				-> False
	(==) SR_EOM x			= case x of	SR_EOM			-> True
										_				-> False
	(==) SR_Sendable x		= case x of	SR_Sendable 	-> True
										_				-> False
	(==) SR_Disconnected x	= case x of	SR_Disconnected	-> True
										_				-> False

instance toString SelectResult
  where
  	toString SR_Available		= "SR_Available"
  	toString SR_EOM				= "SR_EOM"
  	toString SR_Sendable		= "SR_Sendable"
  	toString SR_Disconnected	= "SR_Disconnected"

502 503 504
selectChannel_MT		:: !(Maybe Timeout) !*r_channels !*s_channels !*env
					-> (![(!Int, !SelectResult)], !*r_channels, !*s_channels, !*env) 
					|	SelectReceive r_channels & SelectSend s_channels & ChannelEnv env
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
selectChannel_MT mbTimeout r_channels s_channels env
	|	isJust mbTimeout && fromJust mbTimeout<0
		= ([], r_channels, s_channels, env)
	#!	(mbStopTime, env) 	= getMbStopTime mbTimeout env
		nonBlocking			= isJust mbTimeout && fromJust mbTimeout==0
		(rcvPairs,r_channels) = accRChannels getREndpointRef r_channels
		(sndEndpoints,s_channels) = accSChannels getSEndpointRef s_channels
	|	isEmpty rcvPairs && isEmpty sndEndpoints
		= abort "StdTCPChannels: error: selectChannel called with zero channels"
	#!	(rcvEndpoints, rcvKinds)	= unzip rcvPairs
	= selectLoop nonBlocking mbStopTime rcvEndpoints { kind \\ kind <- rcvKinds } sndEndpoints r_channels s_channels env
  where
	getREndpointRef (TCP_RCHANNEL tcp_RChan)
		#!	(rchan=:(endpointRef,_))	= unpack_tcprchan tcp_RChan
		= ((endpointRef,RCHANNEL),TCP_RCHANNEL (pack_tcprchan rchan))
	getREndpointRef (TCP_LISTENER tcp_Listener)
		#!	endpointRef	= unpack_tcplistener tcp_Listener
		= ((endpointRef, LISTENER),TCP_LISTENER (pack_tcplistener endpointRef))
	getSEndpointRef tcp_SChan
		#!	ch	= unpack_tcpschan tcp_SChan
		= (ch.bEndpointRef, pack_tcpschan ch)
  
selectLoop nonBlocking mbStopTime rcvEndpoints rcvKindArray sndEndpoints r_channels s_channels env
	#!	(isIOProg, env) = channelEnvKind env
		rcvEndpointArray	= { ep \\ ep <- rcvEndpoints }
		sndEndpointArray	= { ep \\ ep <- sndEndpoints }
		(errCode, env)	
			= selectChC isIOProg nonBlocking mbStopTime rcvEndpointArray rcvKindArray sndEndpointArray env
		// SIDEEFFECT: this will also alter the contents of rcvEndpointArray and sndEndpointArray
	|	errCode	== 1 || errCode==3	// timeout expired or some other error
		= ([], r_channels, s_channels, env)
	#!	readableIndices	= [ i \\ endpointRef<-:rcvEndpointArray & i<-[0..] | endpointRef==0]
		(rStates, r_channels, env)	= pollRState readableIndices [] r_channels env
		sendableIndices	= [ i \\ endpointRef<-:sndEndpointArray & i<-[0..] | endpointRef==0]
		(sStates, s_channels, env)	= pollSState sendableIndices [] s_channels env
		states	= rStates++sStates
	|	isEmpty states
		= case nonBlocking of
			False	-> selectLoop nonBlocking mbStopTime rcvEndpoints rcvKindArray sndEndpoints r_channels s_channels env
			_		-> ([], r_channels, s_channels, env)
	= (states,  r_channels, s_channels, env)

pollRState [] akku r_channels env
	= (reverse akku, r_channels, env)
pollRState [rcvIndex:rcvIndices] akku r_channels env
	#!	(mbResult, r_channels, env)	= getRState rcvIndex r_channels env
	|	isNothing mbResult
		= pollRState rcvIndices akku r_channels env
	#!	result	= fromJust mbResult
	| result<>SR_Available && result<>SR_EOM
		= abort (	"StdTCPChannels: error an instance of getRState delivered something else"
				 +++" than SR_Available or SR_EOM")
	= pollRState rcvIndices [(rcvIndex, result):akku] r_channels env

pollSState [] akku s_channels env
	= (reverse akku, s_channels, env)
pollSState [sndIndex:sndIndices] akku s_channels env
	#!	(isDisconnected, s_channels, env)	= appDisconnected sndIndex s_channels env
		sState	= if isDisconnected SR_Disconnected SR_Sendable
	= pollSState sndIndices [(sndIndex, sState):akku] s_channels env

////////////////////// TCP byte streams to receive ////////////////////////////////////

::	*TCP_RCharStream	:==		TCP_RCharStream_ Char
::	*TCP_RCharStreams	= 		TCP_RCharStreams *[TCP_RCharStream]

toRCharStream	::	!TCP_RChannel -> TCP_RCharStream
toRCharStream tcp_rchan
	=	{	rbs_rchan	= tcp_rchan
		,	rbs_buffer	= ""
		,	rbs_index	= 0
		}

instance Receive			TCP_RCharStream_
  where
	receive_MT mbTimeout rbs=:{ rbs_buffer, rbs_index } env
		|	isJust mbTimeout && (fromJust mbTimeout)<0
			= (TR_Expired, Nothing, rbs, env)
		|	rbs_index < (size rbs_buffer)
			= ( TR_Success, Cast (Just rbs_buffer.[rbs_index]), { rbs & rbs_index=inc rbs_index }, env)
		#!	rbs_rchan	= rbs.rbs_rchan
			(toReport, mbData, rbs_rchan, env)	= receive_MT mbTimeout rbs_rchan env
		|	toReport==TR_Success
			#!	data	= toString (fromJust mbData)
			= ( toReport, Cast (Just data.[0]),
			    { rbs & rbs_rchan=rbs_rchan, rbs_index=1, rbs_buffer=data }, env)
		= ( toReport, Nothing, { rbs & rbs_rchan=rbs_rchan, rbs_buffer="", rbs_index=0 }, env)
	receiveUpTo maxLength rbs=:{ rbs_buffer, rbs_index } env
		|	maxLength<=0
			=([], rbs, env)
		#!	nrOfCharsInBuffer	= max (size rbs_buffer - rbs_index) 0
		|	nrOfCharsInBuffer>=maxLength	// enough data is buffered in rbs_buffer
			= (Cast [rbs_buffer.[i] \\ i<-[rbs_index..rbs_index+maxLength-1]], { rbs & rbs_index=rbs_index+maxLength }, env)
		#!	(_, mbData, rbs_rchan, env)	= receive_MT (Just 0) rbs.rbs_rchan env
			charsInBuffer	= [rbs_buffer.[i] \\ i<-[rbs_index..(size rbs_buffer)-1]]
		|	isNothing mbData	// available is False->return the rbs_buffer
			= (	Cast charsInBuffer, { rbs & rbs_rchan=rbs_rchan, rbs_buffer="", rbs_index=0 }, env)
		#!	(l, ch, env)	= receiveUpTo	(maxLength-nrOfCharsInBuffer)
											{rbs & rbs_rchan=rbs_rchan, rbs_buffer=toString (fromJust mbData),
													rbs_index=0 }
											env
		= ((Cast charsInBuffer)++l, ch, env)
	available rbs=:{ rbs_buffer, rbs_index } env
		|	rbs_index < (size rbs_buffer)
			= (True, rbs, env)
		#!	rbs_rchan	= rbs.rbs_rchan
			(isAvailable, rbs_rchan, env)	= available rbs_rchan env
		= (isAvailable, { rbs & rbs_rchan=rbs_rchan }, env)
	eom rbs=:{ rbs_buffer, rbs_index } env
		|	rbs_index < (size rbs_buffer)
			= (False, rbs, env)
		#!	rbs_rchan	= rbs.rbs_rchan
			(isEom, rbs_rchan, env)	= eom rbs_rchan env
		= (isEom, { rbs & rbs_rchan=rbs_rchan }, env)

instance closeRChannel			TCP_RCharStream_
  where
	closeRChannel { rbs_rchan } env
		= closeRChannel rbs_rchan env

////////////////////// TCP byte streams to send ////////////////////////////////////

::	*TCP_SCharStream	:==		TCP_SCharStream_ Char
::	*TCP_SCharStreams	= 		TCP_SCharStreams *[TCP_SCharStream]

toSCharStream	::	!TCP_SChannel -> TCP_SCharStream
toSCharStream tcp_schan
	=	{	sbs_schan	= tcp_schan }

instance Send				TCP_SCharStream_
  where
	send_MT mbTimeout charPolymorphic sbs=:{ sbs_schan } env
		#!	char	= castToChar charPolymorphic
			(toReport, sentBytes, sbs_schan, env)	= send_MT mbTimeout (toByteSeq char) sbs_schan env
		= (toReport, sentBytes, {sbs & sbs_schan=sbs_schan}, env)
	nsend_MT mbTimeout chListPolymorphic sbs=:{ sbs_schan } env
		#!	(toReport, sentBytes, sbs_schan, env)	= send_MT mbTimeout (toByteSeq string) sbs_schan env
		= (toReport, sentBytes, {sbs & sbs_schan=sbs_schan}, env)
	  where
		chList	= castToChList chListPolymorphic
		string :: String
		string	= { ch \\ ch<-chList }
	flushBuffer_MT mbTimeout sbs=:{ sbs_schan } env
		#!	(tReport, sentBytes, sbs_schan, env)	= flushBuffer_MT mbTimeout sbs_schan env
		= (tReport, sentBytes, {sbs & sbs_schan=sbs_schan}, env)
	closeChannel_MT mbTimeout sbs=:{ sbs_schan } env
		= closeChannel_MT mbTimeout sbs_schan env
	abortConnection sbs=:{ sbs_schan } env
		= abortConnection sbs_schan env
	disconnected sbs=:{ sbs_schan } env
		#!	(isDisconnected, sbs_schan, env)	= disconnected sbs_schan env
		= (isDisconnected, {sbs & sbs_schan=sbs_schan}, env)
	bufferSize sbs=:{ sbs_schan }
		#!	(buffSize, sbs_schan)	= bufferSize sbs_schan
		= (buffSize, {sbs & sbs_schan=sbs_schan})

tcpPossible			::	!*env
					->	(!Bool, !*env)
					|	ChannelEnv env
tcpPossible env
	= tcpPossibleC env