client.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package server
  2. import (
  3. // "fmt"
  4. "net"
  5. "time"
  6. // "errors"
  7. // "io"
  8. "github.com/beoran/woe/monolog"
  9. "github.com/beoran/woe/telnet"
  10. "github.com/beoran/woe/world"
  11. )
  12. /* Specific properties of a client. */
  13. type ClientInfo struct {
  14. w int
  15. h int
  16. mtts int
  17. naws bool
  18. compress2 bool
  19. mssp bool
  20. zmp bool
  21. msp bool
  22. msdp bool
  23. mxp bool
  24. ttype bool
  25. terminals []string
  26. terminal string
  27. }
  28. type Client struct {
  29. server * Server
  30. id int
  31. conn net.Conn
  32. alive bool
  33. timeout int
  34. datachan chan []byte
  35. errchan chan error
  36. timechan chan time.Time
  37. telnet * telnet.Telnet
  38. info ClientInfo
  39. account* world.Account
  40. }
  41. func NewClient(server * Server, id int, conn net.Conn) * Client {
  42. datachan := make (chan []byte, 1024)
  43. errchan := make (chan error, 1)
  44. timechan := make (chan time.Time, 32)
  45. telnet := telnet.New()
  46. info := ClientInfo{w : -1, h : -1, terminal: "none"}
  47. return &Client{server, id, conn, true, -1, datachan, errchan, timechan, telnet, info, nil}
  48. }
  49. func (me * Client) Close() {
  50. me.conn.Close()
  51. me.alive = false
  52. }
  53. /** Goroutine that does the actual reading of input data, and sends it to the
  54. * needed channels. */
  55. func (me * Client) ServeRead() {
  56. for (me.alive) {
  57. buffer := make([]byte, 1024, 1024)
  58. read , err := me.conn.Read(buffer);
  59. if err != nil {
  60. me.errchan <- err
  61. return
  62. }
  63. // reply will be stored in me.telnet.Events
  64. me.telnet.ProcessBytes(buffer[:read])
  65. }
  66. }
  67. /* Goroutine that sends any data that must be sent through the Telnet protocol
  68. * (that any data, really) other to the connected client.
  69. */
  70. func (me * Client) ServeWrite() {
  71. for (me.alive) {
  72. select {
  73. case data := <- me.telnet.ToClient:
  74. monolog.Debug("Will send to client: %v", data)
  75. me.conn.Write(data)
  76. }
  77. }
  78. }
  79. func (me * Client) TryReadEvent(millis int) (event telnet.Event, timeout bool, close bool) {
  80. var timerchan <-chan(time.Time)
  81. if millis >= 0 {
  82. timerchan = time.Tick(time.Millisecond * time.Duration(millis))
  83. } else {
  84. /* If time is negative, block by using a fake time channel that never gets sent anyting */
  85. timerchan = make(<-chan(time.Time))
  86. }
  87. select {
  88. case event := <- me.telnet.Events:
  89. return event, false, false
  90. case err := <- me.errchan:
  91. monolog.Info("Connection closed: %s\n", err)
  92. me.Close()
  93. return nil, false, true
  94. case _ = <- timerchan:
  95. return nil, true, false
  96. }
  97. }
  98. func (me * Client) TryRead(millis int) (data []byte, timeout bool, close bool) {
  99. for (me.alive) {
  100. event, timeout, close := me.TryReadEvent(millis)
  101. if event == nil && (timeout || close) {
  102. return nil, timeout, close
  103. }
  104. switch event := event.(type) {
  105. case * telnet.DataEvent:
  106. monolog.Debug("Telnet data event %T : %d.", event, len(event.Data))
  107. return event.Data, false, false
  108. default:
  109. monolog.Info("Ignoring telnet event %T : %v for now.", event, event)
  110. }
  111. }
  112. return nil, false, true
  113. }
  114. func (me * Client) Serve() (err error) {
  115. // buffer := make([]byte, 1024, 1024)
  116. go me.ServeWrite()
  117. go me.ServeRead()
  118. me.SetupTelnet()
  119. if me.server.World != nil {
  120. me.Printf(me.server.World.MOTD)
  121. }
  122. if (!me.AccountDialog()) {
  123. time.Sleep(3);
  124. // sleep so output gets flushed, hopefully.
  125. // Also slow down brute force attacks.
  126. me.Close()
  127. return nil
  128. }
  129. me.Printf("Welcome, %s\n", me.account.Name)
  130. for (me.alive) {
  131. me.HandleCommand()
  132. /*
  133. data, _, _ := me.TryRead(3000)
  134. if data == nil {
  135. // me.telnet.TelnetPrintf("Too late!\r\n")
  136. } else {
  137. me.server.Broadcast(string(data))
  138. }
  139. */
  140. }
  141. return nil
  142. }
  143. func (me * Client) IsAlive() bool {
  144. return me.alive
  145. }
  146. func (me * Client) WriteString(str string) {
  147. me.conn.Write([]byte(str))
  148. }