项目作者: WKHAllen

项目描述 :
A cross platform networking library written in Go
高级语言: Go
项目地址: git://github.com/WKHAllen/godtp.git
创建时间: 2020-03-12T16:35:48Z
项目社区:https://github.com/WKHAllen/godtp

开源协议:MIT License

下载


Data Transfer Protocol for Go

Cross-platform networking interfaces for Go.

Data Transfer Protocol

The Data Transfer Protocol (DTP) is a larger project to make ergonomic network programming available in any language.
See the full project here.

Installation

Install the package:

  1. $ go get -u github.com/wkhallen/godtp

Creating a server

A server can be built using the Server implementation:

  1. package example
  2. import (
  3. "fmt"
  4. "github.com/wkhallen/godtp"
  5. )
  6. func main() {
  7. // Create a server that receives strings and returns the length of each string
  8. server, serverEvent := godtp.NewServer[int, string]()
  9. err := server.Start("127.0.0.1", 29275)
  10. if err != nil {
  11. // Handle server start error
  12. }
  13. // Iterate over events
  14. for event := range serverEvent {
  15. switch event.EventType {
  16. case godtp.ServerConnect:
  17. fmt.Printf("Client with ID %d connected\n", event.ClientID)
  18. case godtp.ServerDisconnect:
  19. fmt.Printf("Client with ID %d disconnected\n", event.ClientID)
  20. case godtp.ServerReceive:
  21. // Send back the length of the string
  22. err := server.Send(len(event.Data), event.ClientID)
  23. if err != nil {
  24. // Handle send error
  25. }
  26. }
  27. }
  28. }

Creating a client

A client can be built using the Client implementation:

  1. package example
  2. import (
  3. "fmt"
  4. "github.com/wkhallen/godtp"
  5. )
  6. func main() {
  7. // Create a client that send a message to the server and receives the length of the message
  8. client, clientEvent := godtp.NewClient[string, int]()
  9. err := client.Connect("127.0.0.1", 29275)
  10. if err != nil {
  11. // Handle client connect error
  12. }
  13. // Send a message to the server
  14. message := "Hello, server!"
  15. err = client.Send(message)
  16. if err != nil {
  17. // Handle send error
  18. }
  19. // Receive the response
  20. event := <-clientEvent
  21. switch event.EventType {
  22. case godtp.ClientReceive:
  23. // Validate the response
  24. fmt.Printf("Received response from server: %d", event.Data)
  25. if event.Data != len(message) {
  26. fmt.Printf("Invalid response: expected %d, received %d", len(message), event.Data)
  27. }
  28. default:
  29. // Unexpected response
  30. fmt.Printf("Expected to receive a response from the server, instead got %#v\n", event)
  31. }
  32. }

Security

Information security comes included. Every message sent over a network interface is encrypted with AES-256. Key
exchanges are performed using a 2048-bit RSA key-pair.