client.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if me.account != nil {
  53. me.server.World.RemoveAccount(me.account.Name)
  54. }
  55. me.account = nil
  56. }
  57. /** Goroutine that does the actual reading of input data, and sends it to the
  58. * needed channels. */
  59. func (me * Client) ServeRead() {
  60. for (me.alive) {
  61. buffer := make([]byte, 1024, 1024)
  62. read , err := me.conn.Read(buffer);
  63. if err != nil {
  64. me.errchan <- err
  65. return
  66. }
  67. // reply will be stored in me.telnet.Events channel
  68. me.telnet.ProcessBytes(buffer[:read])
  69. }
  70. }
  71. /* Goroutine that sends any data that must be sent through the Telnet protocol
  72. * (that any data, really) other to the connected client.
  73. */
  74. func (me * Client) ServeWrite() {
  75. for (me.alive) {
  76. select {
  77. case data := <- me.telnet.ToClient:
  78. monolog.Debug("Will send to client: %v", data)
  79. me.conn.Write(data)
  80. }
  81. }
  82. }
  83. func (me * Client) TryReadEvent(millis int) (event telnet.Event, timeout bool, close bool) {
  84. var timerchan <-chan(time.Time)
  85. if millis >= 0 {
  86. timerchan = time.Tick(time.Millisecond * time.Duration(millis))
  87. } else {
  88. /* If time is negative, block by using a fake time channel that never gets sent anyting */
  89. timerchan = make(<-chan(time.Time))
  90. }
  91. select {
  92. case event := <- me.telnet.Events:
  93. return event, false, false
  94. case err := <- me.errchan:
  95. monolog.Info("Connection closed: %s\n", err)
  96. me.Close()
  97. return nil, false, true
  98. case _ = <- timerchan:
  99. return nil, true, false
  100. }
  101. }
  102. func (me * Client) TryRead(millis int) (data []byte, timeout bool, close bool) {
  103. for (me.alive) {
  104. event, timeout, close := me.TryReadEvent(millis)
  105. if event == nil && (timeout || close) {
  106. return nil, timeout, close
  107. }
  108. switch event := event.(type) {
  109. case * telnet.DataEvent:
  110. monolog.Debug("Telnet data event %T : %d.", event, len(event.Data))
  111. return event.Data, false, false
  112. default:
  113. monolog.Info("Ignoring telnet event %T : %v for now.", event, event)
  114. }
  115. }
  116. return nil, false, true
  117. }
  118. func (me * Client) Serve() (err error) {
  119. // buffer := make([]byte, 1024, 1024)
  120. go me.ServeWrite()
  121. go me.ServeRead()
  122. me.SetupTelnet()
  123. if me.server.World != nil {
  124. me.Printf(me.server.World.MOTD)
  125. }
  126. if (!me.AccountDialog()) {
  127. time.Sleep(3);
  128. // sleep so output gets flushed, hopefully.
  129. // Also slow down brute force attacks.
  130. me.Close()
  131. return nil
  132. }
  133. me.Printf("Welcome, %s\n", me.account.Name)
  134. for (me.alive) {
  135. me.HandleCommand()
  136. /*
  137. data, _, _ := me.TryRead(3000)
  138. if data == nil {
  139. // me.telnet.TelnetPrintf("Too late!\r\n")
  140. } else {
  141. me.server.Broadcast(string(data))
  142. }
  143. */
  144. }
  145. return nil
  146. }
  147. func (me * Client) IsAlive() bool {
  148. return me.alive
  149. }
  150. func (me * Client) WriteString(str string) {
  151. me.conn.Write([]byte(str))
  152. }