You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
4.9 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "github.com/aceld/zinx/zinx_app_demo/mmo_game/pb"
  7. "github.com/golang/protobuf/proto"
  8. "io"
  9. "math/rand"
  10. "net"
  11. "os"
  12. "os/signal"
  13. "time"
  14. )
  15. type Message struct {
  16. Len uint32
  17. MsgId uint32
  18. Data []byte
  19. }
  20. type TcpClient struct {
  21. conn net.Conn
  22. X float32
  23. Y float32
  24. Z float32
  25. V float32
  26. Pid int32
  27. isOnline chan bool
  28. }
  29. func (this *TcpClient) Unpack(headdata []byte) (head *Message, err error) {
  30. headbuf := bytes.NewReader(headdata)
  31. head = &Message{}
  32. // 读取Len
  33. if err = binary.Read(headbuf, binary.LittleEndian, &head.Len); err != nil {
  34. return nil, err
  35. }
  36. // 读取MsgId
  37. if err = binary.Read(headbuf, binary.LittleEndian, &head.MsgId); err != nil {
  38. return nil, err
  39. }
  40. // 封包太大
  41. //if head.Len > MaxPacketSize {
  42. // return nil, packageTooBig
  43. //}
  44. return head, nil
  45. }
  46. func (this *TcpClient) Pack(msgId uint32, dataBytes []byte) (out []byte, err error) {
  47. outbuff := bytes.NewBuffer([]byte{})
  48. // 写Len
  49. if err = binary.Write(outbuff, binary.LittleEndian, uint32(len(dataBytes))); err != nil {
  50. return
  51. }
  52. // 写MsgId
  53. if err = binary.Write(outbuff, binary.LittleEndian, msgId); err != nil {
  54. return
  55. }
  56. //all pkg data
  57. if err = binary.Write(outbuff, binary.LittleEndian, dataBytes); err != nil {
  58. return
  59. }
  60. out = outbuff.Bytes()
  61. return
  62. }
  63. func (this *TcpClient) SendMsg(msgID uint32, data proto.Message) {
  64. // 进行编码
  65. binary_data, err := proto.Marshal(data)
  66. if err != nil {
  67. fmt.Println(fmt.Sprintf("marshaling error: %s", err))
  68. return
  69. }
  70. sendData, err := this.Pack(msgID, binary_data)
  71. if err == nil {
  72. this.conn.Write(sendData)
  73. } else {
  74. fmt.Println(err)
  75. }
  76. return
  77. }
  78. func (this *TcpClient) AIRobotAction() {
  79. //聊天或者移动
  80. //随机获得动作
  81. tp := rand.Intn(2)
  82. if tp == 0 {
  83. content := fmt.Sprintf("hello 我是player %d, 你是谁?", this.Pid)
  84. msg := &pb.Talk{
  85. Content: content,
  86. }
  87. this.SendMsg(2, msg)
  88. } else {
  89. //移动
  90. x := this.X
  91. z := this.Z
  92. randpos := rand.Intn(2)
  93. if randpos == 0 {
  94. x -= float32(rand.Intn(10))
  95. z -= float32(rand.Intn(10))
  96. } else {
  97. x += float32(rand.Intn(10))
  98. z += float32(rand.Intn(10))
  99. }
  100. //纠正坐标位置
  101. if x > 410 {
  102. x = 410
  103. } else if x < 85 {
  104. x = 85
  105. }
  106. if z > 400 {
  107. z = 400
  108. } else if z < 75 {
  109. z = 75
  110. }
  111. //移动方向角度
  112. randv := rand.Intn(2)
  113. v := this.V
  114. if randv == 0 {
  115. v = 25
  116. } else {
  117. v = 335
  118. }
  119. //封装Postsition消息
  120. msg := &pb.Position{
  121. X: x,
  122. Y: this.Y,
  123. Z: z,
  124. V: v,
  125. }
  126. fmt.Println(fmt.Sprintf("player ID: %d. Walking...", this.Pid))
  127. //发送移动MsgID:3的指令
  128. this.SendMsg(3, msg)
  129. }
  130. }
  131. /*
  132. 处理一个回执业务
  133. */
  134. func (this *TcpClient) DoMsg(msg *Message) {
  135. //处理消息
  136. //fmt.Println(fmt.Sprintf("msg id :%d, data len: %d", msg.MsgId, msg.Len))
  137. if msg.MsgId == 1 {
  138. //服务器回执给客户端 分配ID
  139. //解析proto
  140. syncpid := &pb.SyncPid{}
  141. proto.Unmarshal(msg.Data, syncpid)
  142. //给当前客户端ID进行赋值
  143. this.Pid = syncpid.Pid
  144. } else if msg.MsgId == 200 {
  145. //服务器回执客户端广播数据
  146. //解析proto
  147. bdata := &pb.BroadCast{}
  148. proto.Unmarshal(msg.Data, bdata)
  149. //初次玩家上线 广播位置消息
  150. if bdata.Tp == 2 && bdata.Pid == this.Pid {
  151. //本人
  152. //更新客户端坐标
  153. this.X = bdata.GetP().X
  154. this.Y = bdata.GetP().Y
  155. this.Z = bdata.GetP().Z
  156. this.V = bdata.GetP().V
  157. fmt.Println(fmt.Sprintf("player ID: %d online.. at(%f,%f,%f,%f)", bdata.Pid, this.X, this.Y, this.Z, this.V))
  158. //玩家已经成功上线
  159. this.isOnline <- true
  160. } else if bdata.Tp == 1 {
  161. fmt.Println(fmt.Sprintf("世界聊天,玩家%d说的话是: %s", bdata.Pid, bdata.GetContent()))
  162. }
  163. }
  164. }
  165. func (this *TcpClient) Start() {
  166. go func() {
  167. for {
  168. //read per head data
  169. headdata := make([]byte, 8)
  170. if _, err := io.ReadFull(this.conn, headdata); err != nil {
  171. fmt.Println(err)
  172. return
  173. }
  174. pkgHead, err := this.Unpack(headdata)
  175. if err != nil {
  176. return
  177. }
  178. //data
  179. if pkgHead.Len > 0 {
  180. pkgHead.Data = make([]byte, pkgHead.Len)
  181. if _, err := io.ReadFull(this.conn, pkgHead.Data); err != nil {
  182. return
  183. }
  184. }
  185. //处理服务器回执业务
  186. this.DoMsg(pkgHead)
  187. }
  188. }()
  189. select {
  190. case <-this.isOnline:
  191. //自动AI业务
  192. go func() {
  193. for {
  194. this.AIRobotAction()
  195. time.Sleep(3 * time.Second)
  196. }
  197. }()
  198. }
  199. }
  200. func NewTcpClient(ip string, port int) *TcpClient {
  201. addrStr := fmt.Sprintf("%s:%d", ip, port)
  202. conn, err := net.Dial("tcp", addrStr)
  203. if err == nil {
  204. client := &TcpClient{
  205. conn: conn,
  206. Pid: 0,
  207. X: 0,
  208. Y: 0,
  209. Z: 0,
  210. V: 0,
  211. isOnline: make(chan bool),
  212. }
  213. return client
  214. } else {
  215. panic(err)
  216. }
  217. }
  218. func main() {
  219. for i := 0; i < 1000; i++ {
  220. client := NewTcpClient("127.0.0.1", 8999)
  221. client.Start()
  222. time.Sleep(1 * time.Second)
  223. }
  224. // close
  225. c := make(chan os.Signal, 1)
  226. signal.Notify(c, os.Interrupt, os.Kill)
  227. sig := <-c
  228. fmt.Println("=======", sig)
  229. }