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.

70 lines
1.3 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "net"
  7. "reflect"
  8. //"encoding/hex"
  9. . "github.com/tendermint/go-common"
  10. "github.com/tendermint/tmsp/types"
  11. )
  12. func main() {
  13. conn, err := Connect("unix://test.sock")
  14. if err != nil {
  15. Exit(err.Error())
  16. }
  17. // Make a bunch of requests
  18. counter := 0
  19. for i := 0; ; i++ {
  20. req := types.ToRequestEcho("foobar")
  21. _, err := makeRequest(conn, req)
  22. if err != nil {
  23. Exit(err.Error())
  24. }
  25. counter += 1
  26. if counter%1000 == 0 {
  27. fmt.Println(counter)
  28. }
  29. }
  30. }
  31. func makeRequest(conn net.Conn, req *types.Request) (*types.Response, error) {
  32. var bufWriter = bufio.NewWriter(conn)
  33. // Write desired request
  34. err := types.WriteMessage(req, bufWriter)
  35. if err != nil {
  36. return nil, err
  37. }
  38. err = types.WriteMessage(types.ToRequestFlush(), bufWriter)
  39. if err != nil {
  40. return nil, err
  41. }
  42. err = bufWriter.Flush()
  43. if err != nil {
  44. return nil, err
  45. }
  46. // Read desired response
  47. var res = &types.Response{}
  48. err = types.ReadMessage(conn, res)
  49. if err != nil {
  50. return nil, err
  51. }
  52. var resFlush = &types.Response{}
  53. err = types.ReadMessage(conn, resFlush)
  54. if err != nil {
  55. return nil, err
  56. }
  57. if _, ok := resFlush.Value.(*types.Response_Flush); !ok {
  58. return nil, errors.New(Fmt("Expected flush response but got something else: %v", reflect.TypeOf(resFlush)))
  59. }
  60. return res, nil
  61. }