Tcl Source Code

Hex Artifact Content
Login

Artifact f600e520b917408c622f0493268a410594921796:

Attachment "server.tcl" to ticket [1028264fff] added by dgp 2004-09-15 06:30:21.
0000: 69 66 20 7b 5b 6c 6c 65 6e 67 74 68 20 24 61 72  if {[llength $ar
0010: 67 76 5d 21 3d 31 7d 20 7b 0a 20 20 20 20 70 75  gv]!=1} {.    pu
0020: 74 73 20 73 74 64 65 72 72 20 22 55 73 61 67 65  ts stderr "Usage
0030: 3a 20 74 63 6c 73 68 20 73 65 72 76 65 72 2e 74  : tclsh server.t
0040: 63 6c 20 3c 50 41 55 53 45 5f 54 49 4d 45 3e 22  cl <PAUSE_TIME>"
0050: 0a 20 20 20 20 70 75 74 73 20 73 74 64 65 72 72  .    puts stderr
0060: 20 22 20 20 77 68 65 72 65 20 50 41 55 53 45 5f   "  where PAUSE_
0070: 54 49 4d 45 20 69 73 20 69 6e 20 6d 73 2e 22 0a  TIME is in ms.".
0080: 20 20 20 20 65 78 69 74 0a 7d 0a 0a 73 65 74 20      exit.}..set 
0090: 50 41 55 53 45 5f 54 49 4d 45 20 5b 6c 69 6e 64  PAUSE_TIME [lind
00a0: 65 78 20 24 61 72 67 76 20 30 5d 0a 0a 70 72 6f  ex $argv 0]..pro
00b0: 63 20 52 65 61 64 4d 65 20 7b 20 63 68 61 6e 20  c ReadMe { chan 
00c0: 7d 20 7b 0a 20 20 20 20 67 6c 6f 62 61 6c 20 69  } {.    global i
00d0: 6e 70 75 74 5f 63 6f 75 6e 74 20 77 72 69 74 65  nput_count write
00e0: 5f 71 75 65 75 65 20 77 72 69 74 65 5f 65 72 72  _queue write_err
00f0: 6f 72 20 50 41 55 53 45 5f 54 49 4d 45 0a 20 20  or PAUSE_TIME.  
0100: 20 20 69 66 20 7b 5b 63 61 74 63 68 20 7b 67 65    if {[catch {ge
0110: 74 73 20 24 63 68 61 6e 20 6c 69 6e 65 7d 20 62  ts $chan line} b
0120: 79 74 65 73 5d 7d 20 7b 0a 09 70 75 74 73 20 22  ytes]} {..puts "
0130: 49 4e 50 55 54 20 45 52 52 4f 52 20 6f 6e 20 73  INPUT ERROR on s
0140: 6f 63 6b 65 74 20 24 63 68 61 6e 3a 20 24 62 79  ocket $chan: $by
0150: 74 65 73 22 0a 09 72 65 74 75 72 6e 0a 20 20 20  tes"..return.   
0160: 20 7d 0a 20 20 20 20 69 66 20 7b 24 62 79 74 65   }.    if {$byte
0170: 73 20 3c 20 30 7d 20 7b 0a 09 69 66 20 7b 5b 66  s < 0} {..if {[f
0180: 62 6c 6f 63 6b 65 64 20 24 63 68 61 6e 5d 7d 20  blocked $chan]} 
0190: 7b 0a 09 20 20 20 20 23 20 27 67 65 74 73 27 20  {..    # 'gets' 
01a0: 63 61 6c 6c 20 69 73 20 62 6c 6f 63 6b 65 64 20  call is blocked 
01b0: 75 6e 74 69 6c 20 61 20 77 68 6f 6c 65 20 6c 69  until a whole li
01c0: 6e 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a  ne is available.
01d0: 09 20 20 20 20 72 65 74 75 72 6e 0a 09 7d 0a 09  .    return..}..
01e0: 69 66 20 7b 5b 65 6f 66 20 24 63 68 61 6e 5d 7d  if {[eof $chan]}
01f0: 20 7b 0a 09 20 20 20 20 70 75 74 73 20 22 49 4e   {..    puts "IN
0200: 50 55 54 20 45 4f 46 20 64 65 74 65 63 74 65 64  PUT EOF detected
0210: 20 6f 6e 20 73 6f 63 6b 65 74 20 24 63 68 61 6e   on socket $chan
0220: 3b 20 63 6c 6f 73 69 6e 67 20 73 6f 63 6b 65 74  ; closing socket
0230: 2e 22 0a 09 20 20 20 20 63 6c 6f 73 65 20 24 63  ."..    close $c
0240: 68 61 6e 0a 09 20 20 20 20 75 6e 73 65 74 20 69  han..    unset i
0250: 6e 70 75 74 5f 63 6f 75 6e 74 28 24 63 68 61 6e  nput_count($chan
0260: 29 0a 09 20 20 20 20 75 6e 73 65 74 20 77 72 69  )..    unset wri
0270: 74 65 5f 65 72 72 6f 72 28 24 63 68 61 6e 29 0a  te_error($chan).
0280: 09 20 20 20 20 75 6e 73 65 74 20 77 72 69 74 65  .    unset write
0290: 5f 71 75 65 75 65 28 24 63 68 61 6e 29 0a 09 20  _queue($chan).. 
02a0: 20 20 20 72 65 74 75 72 6e 0a 09 7d 0a 20 20 20     return..}.   
02b0: 20 7d 0a 20 20 20 20 70 75 74 73 20 5b 66 6f 72   }.    puts [for
02c0: 6d 61 74 20 22 5c 5b 25 32 64 5c 5d 3d 3d 3e 24  mat "\[%2d\]==>$
02d0: 6c 69 6e 65 22 20 5b 69 6e 63 72 20 69 6e 70 75  line" [incr inpu
02e0: 74 5f 63 6f 75 6e 74 28 24 63 68 61 6e 29 5d 5d  t_count($chan)]]
02f0: 0a 20 20 20 20 73 65 74 20 6f 75 74 6d 73 67 20  .    set outmsg 
0300: 5b 66 6f 72 6d 61 74 20 22 4d 65 73 73 61 67 65  [format "Message
0310: 20 25 32 64 20 72 65 63 65 69 76 65 64 3a 20 24   %2d received: $
0320: 6c 69 6e 65 22 20 24 69 6e 70 75 74 5f 63 6f 75  line" $input_cou
0330: 6e 74 28 24 63 68 61 6e 29 5d 0a 20 20 20 20 69  nt($chan)].    i
0340: 66 20 7b 21 24 77 72 69 74 65 5f 65 72 72 6f 72  f {!$write_error
0350: 28 24 63 68 61 6e 29 7d 20 7b 0a 09 6c 61 70 70  ($chan)} {..lapp
0360: 65 6e 64 20 77 72 69 74 65 5f 71 75 65 75 65 28  end write_queue(
0370: 24 63 68 61 6e 29 20 24 6f 75 74 6d 73 67 0a 09  $chan) $outmsg..
0380: 66 69 6c 65 65 76 65 6e 74 20 24 63 68 61 6e 20  fileevent $chan 
0390: 77 72 69 74 61 62 6c 65 20 5b 6c 69 73 74 20 57  writable [list W
03a0: 72 69 74 65 4d 65 20 24 63 68 61 6e 5d 0a 20 20  riteMe $chan].  
03b0: 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 30    }.    return 0
03c0: 0a 7d 0a 0a 70 72 6f 63 20 57 72 69 74 65 4d 65  .}..proc WriteMe
03d0: 20 7b 20 63 68 61 6e 20 7d 20 7b 0a 20 20 20 20   { chan } {.    
03e0: 67 6c 6f 62 61 6c 20 77 72 69 74 65 5f 71 75 65  global write_que
03f0: 75 65 20 77 72 69 74 65 5f 65 72 72 6f 72 0a 0a  ue write_error..
0400: 20 20 20 20 69 66 20 7b 21 24 77 72 69 74 65 5f      if {!$write_
0410: 65 72 72 6f 72 28 24 63 68 61 6e 29 7d 20 7b 0a  error($chan)} {.
0420: 09 73 65 74 20 72 65 73 70 6f 6e 73 65 20 5b 6c  .set response [l
0430: 69 6e 64 65 78 20 24 77 72 69 74 65 5f 71 75 65  index $write_que
0440: 75 65 28 24 63 68 61 6e 29 20 30 5d 0a 09 70 75  ue($chan) 0]..pu
0450: 74 73 20 22 20 52 65 73 70 6f 6e 73 65 3a 20 24  ts " Response: $
0460: 72 65 73 70 6f 6e 73 65 22 0a 09 69 66 20 7b 5b  response"..if {[
0470: 63 61 74 63 68 20 7b 70 75 74 73 20 24 63 68 61  catch {puts $cha
0480: 6e 20 24 72 65 73 70 6f 6e 73 65 7d 20 65 72 72  n $response} err
0490: 6d 73 67 5d 7d 20 7b 0a 09 20 20 20 20 70 75 74  msg]} {..    put
04a0: 73 20 22 4f 55 54 50 55 54 20 45 52 52 4f 52 20  s "OUTPUT ERROR 
04b0: 6f 6e 20 73 6f 63 6b 65 74 20 24 63 68 61 6e 3a  on socket $chan:
04c0: 20 24 65 72 72 6d 73 67 22 0a 09 20 20 20 20 73   $errmsg"..    s
04d0: 65 74 20 77 72 69 74 65 5f 65 72 72 6f 72 28 24  et write_error($
04e0: 63 68 61 6e 29 20 31 0a 09 20 20 20 20 72 65 74  chan) 1..    ret
04f0: 75 72 6e 0a 09 7d 0a 20 20 20 20 7d 0a 20 20 20  urn..}.    }.   
0500: 20 73 65 74 20 77 72 69 74 65 5f 71 75 65 75 65   set write_queue
0510: 28 24 63 68 61 6e 29 20 5b 6c 72 61 6e 67 65 20  ($chan) [lrange 
0520: 24 77 72 69 74 65 5f 71 75 65 75 65 28 24 63 68  $write_queue($ch
0530: 61 6e 29 20 31 20 65 6e 64 5d 0a 20 20 20 20 69  an) 1 end].    i
0540: 66 20 7b 5b 6c 6c 65 6e 67 74 68 20 24 77 72 69  f {[llength $wri
0550: 74 65 5f 71 75 65 75 65 28 24 63 68 61 6e 29 5d  te_queue($chan)]
0560: 3c 31 7d 20 7b 0a 09 66 69 6c 65 65 76 65 6e 74  <1} {..fileevent
0570: 20 24 63 68 61 6e 20 77 72 69 74 61 62 6c 65 20   $chan writable 
0580: 7b 7d 0a 20 20 20 20 7d 0a 0a 7d 0a 0a 70 72 6f  {}.    }..}..pro
0590: 63 20 53 65 72 76 69 63 65 20 7b 20 63 68 61 6e  c Service { chan
05a0: 20 61 64 64 72 20 70 6f 72 74 20 7d 20 7b 0a 20   addr port } {. 
05b0: 20 20 20 67 6c 6f 62 61 6c 20 69 6e 70 75 74 5f     global input_
05c0: 63 6f 75 6e 74 20 77 72 69 74 65 5f 65 72 72 6f  count write_erro
05d0: 72 20 77 72 69 74 65 5f 71 75 65 75 65 0a 20 20  r write_queue.  
05e0: 20 20 66 63 6f 6e 66 69 67 75 72 65 20 24 63 68    fconfigure $ch
05f0: 61 6e 20 2d 62 6c 6f 63 6b 69 6e 67 20 30 20 2d  an -blocking 0 -
0600: 62 75 66 66 65 72 69 6e 67 20 6c 69 6e 65 20 2d  buffering line -
0610: 74 72 61 6e 73 6c 61 74 69 6f 6e 20 7b 61 75 74  translation {aut
0620: 6f 20 63 72 6c 66 7d 0a 20 20 20 20 66 69 6c 65  o crlf}.    file
0630: 65 76 65 6e 74 20 24 63 68 61 6e 20 72 65 61 64  event $chan read
0640: 61 62 6c 65 20 22 5b 6c 69 73 74 20 52 65 61 64  able "[list Read
0650: 4d 65 20 24 63 68 61 6e 5d 3b 20 61 66 74 65 72  Me $chan]; after
0660: 20 5c 24 50 41 55 53 45 5f 54 49 4d 45 22 0a 20   \$PAUSE_TIME". 
0670: 20 20 20 73 65 74 20 69 6e 70 75 74 5f 63 6f 75     set input_cou
0680: 6e 74 28 24 63 68 61 6e 29 20 30 0a 20 20 20 20  nt($chan) 0.    
0690: 73 65 74 20 77 72 69 74 65 5f 65 72 72 6f 72 28  set write_error(
06a0: 24 63 68 61 6e 29 20 30 0a 20 20 20 20 73 65 74  $chan) 0.    set
06b0: 20 77 72 69 74 65 5f 71 75 65 75 65 28 24 63 68   write_queue($ch
06c0: 61 6e 29 20 7b 7d 0a 7d 0a 0a 73 65 74 20 73 6f  an) {}.}..set so
06d0: 63 6b 20 5b 73 6f 63 6b 65 74 20 2d 73 65 72 76  ck [socket -serv
06e0: 65 72 20 53 65 72 76 69 63 65 20 31 31 31 31 31  er Service 11111
06f0: 5d 0a 0a 76 77 61 69 74 20 66 6f 72 65 76 65 72  ]..vwait forever
0700: 0a                                               .