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

tcp_client.h 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_PS_COMM_TCP_CLIENT_H_
  17. #define MINDSPORE_CCSRC_PS_COMM_TCP_CLIENT_H_
  18. #include "ps/comm/tcp_message_handler.h"
  19. #include <event2/event.h>
  20. #include <event2/bufferevent.h>
  21. #include <functional>
  22. #include <string>
  23. namespace mindspore {
  24. namespace ps {
  25. namespace comm {
  26. class TcpClient {
  27. public:
  28. using OnMessage = std::function<void(const TcpClient &, const void *, size_t)>;
  29. using OnConnected = std::function<void(const TcpClient &)>;
  30. using OnDisconnected = std::function<void(const TcpClient &, int)>;
  31. using OnRead = std::function<void(const TcpClient &, const void *, size_t)>;
  32. using OnTimeout = std::function<void(const TcpClient &)>;
  33. explicit TcpClient(std::string address, std::uint16_t port);
  34. virtual ~TcpClient();
  35. std::string GetServerAddress() const;
  36. void SetCallback(const OnConnected &conn, const OnDisconnected &disconn, const OnRead &read,
  37. const OnTimeout &timeout);
  38. void InitTcpClient();
  39. void StartWithDelay(int seconds);
  40. void Stop();
  41. void ReceiveMessage(const OnMessage &cb);
  42. void SendMessage(const void *buf, size_t num) const;
  43. void Start();
  44. protected:
  45. static void SetTcpNoDelay(const evutil_socket_t &fd);
  46. static void TimeoutCallback(evutil_socket_t fd, std::int16_t what, void *arg);
  47. static void ReadCallback(struct bufferevent *bev, void *ctx);
  48. static void EventCallback(struct bufferevent *bev, std::int16_t events, void *ptr);
  49. virtual void OnReadHandler(const void *buf, size_t num);
  50. private:
  51. TcpMessageHandler message_handler_;
  52. OnMessage message_callback_;
  53. OnConnected connected_callback_;
  54. OnDisconnected disconnected_callback_;
  55. OnRead read_callback_;
  56. OnTimeout timeout_callback_;
  57. event_base *event_base_;
  58. event *event_timeout_;
  59. bufferevent *buffer_event_;
  60. std::string server_address_;
  61. std::uint16_t server_port_;
  62. };
  63. } // namespace comm
  64. } // namespace ps
  65. } // namespace mindspore
  66. #endif // MINDSPORE_CCSRC_PS_COMM_TCP_CLIENT_H_