Browse Source

udp server测试编写完成

yjp 1 year ago
parent
commit
a88842b078
2 changed files with 62 additions and 13 deletions
  1. 13 13
      network/connection.go
  2. 49 0
      network/udp_test.go

+ 13 - 13
network/connection.go

@@ -6,11 +6,11 @@ import (
 	"time"
 )
 
-// ConnectionReadOptions 从连接读取数据的选项
-type ConnectionReadOptions func(conn net.Conn) error
+// connectionReadOptions 从连接读取数据的选项
+type connectionReadOptions func(conn net.Conn) error
 
-// WithReadDeadline 读超时选项
-func WithReadDeadline(duration time.Duration) ConnectionReadOptions {
+// withReadDeadline 读超时选项
+func withReadDeadline(duration time.Duration) connectionReadOptions {
 	return func(conn net.Conn) error {
 		if duration == 0 {
 			return nil
@@ -25,11 +25,11 @@ func WithReadDeadline(duration time.Duration) ConnectionReadOptions {
 	}
 }
 
-// ConnectionWriteOptions 从连接写入数据的选项
-type ConnectionWriteOptions func(conn net.Conn) error
+// connectionWriteOptions 从连接写入数据的选项
+type connectionWriteOptions func(conn net.Conn) error
 
-// WithWriteDeadline 写超时选项
-func WithWriteDeadline(duration time.Duration) ConnectionWriteOptions {
+// withWriteDeadline 写超时选项
+func withWriteDeadline(duration time.Duration) connectionWriteOptions {
 	return func(conn net.Conn) error {
 		if duration == 0 {
 			return nil
@@ -45,7 +45,7 @@ func WithWriteDeadline(duration time.Duration) ConnectionWriteOptions {
 }
 
 // readUDP 读取UDP包
-func readUDP(conn *net.UDPConn, bufferSize int, opts ...ConnectionReadOptions) ([]byte, *net.UDPAddr, error) {
+func readUDP(conn *net.UDPConn, bufferSize int, opts ...connectionReadOptions) ([]byte, *net.UDPAddr, error) {
 	buffer := make([]byte, bufferSize)
 
 	for _, opt := range opts {
@@ -64,7 +64,7 @@ func readUDP(conn *net.UDPConn, bufferSize int, opts ...ConnectionReadOptions) (
 }
 
 // writeUDPWithRemoteAddr 指定远端(对端)地址写出UDP包
-func writeUDPWithRemoteAddr(conn *net.UDPConn, rAddr *net.UDPAddr, data []byte, opts ...ConnectionWriteOptions) error {
+func writeUDPWithRemoteAddr(conn *net.UDPConn, rAddr *net.UDPAddr, data []byte, opts ...connectionWriteOptions) error {
 	for _, opt := range opts {
 		err := opt(conn)
 		if err != nil {
@@ -81,7 +81,7 @@ func writeUDPWithRemoteAddr(conn *net.UDPConn, rAddr *net.UDPAddr, data []byte,
 }
 
 // WriteUDP 写出UDP包
-func WriteUDP(conn *net.UDPConn, data []byte, opts ...ConnectionWriteOptions) error {
+func WriteUDP(conn *net.UDPConn, data []byte, opts ...connectionWriteOptions) error {
 	for _, opt := range opts {
 		err := opt(conn)
 		if err != nil {
@@ -98,7 +98,7 @@ func WriteUDP(conn *net.UDPConn, data []byte, opts ...ConnectionWriteOptions) er
 }
 
 // ReadTCP 读取TCP数据
-func ReadTCP(conn net.Conn, bufferSize int, readCallback func(data []byte) (bool, error), opts ...ConnectionReadOptions) error {
+func ReadTCP(conn net.Conn, bufferSize int, readCallback func(data []byte) (bool, error), opts ...connectionReadOptions) error {
 	for {
 		buffer := make([]byte, bufferSize)
 
@@ -128,7 +128,7 @@ func ReadTCP(conn net.Conn, bufferSize int, readCallback func(data []byte) (bool
 }
 
 // WriteTCP 写TCP数据
-func WriteTCP(conn net.Conn, data []byte, opts ...ConnectionWriteOptions) error {
+func WriteTCP(conn net.Conn, data []byte, opts ...connectionWriteOptions) error {
 	writeBytesCount := 0
 
 	for {

+ 49 - 0
network/udp_test.go

@@ -0,0 +1,49 @@
+package network
+
+import (
+	"bytes"
+	"strings"
+	"testing"
+	"time"
+)
+
+const (
+	testUDPServerAddress           = "127.0.0.1:10060"
+	testUDPServerTimeout           = time.Second
+	testUDPServerReceiveBufferSize = 1024
+)
+
+func TestUDP(t *testing.T) {
+	server := &UDPServer{}
+
+	err := server.Connect(testUDPServerAddress, NewUDPServerOptions(
+		WithUDPServerReadTimeout(testUDPServerTimeout),
+		WithUDPServerWriteTimeout(testUDPServerTimeout),
+		WithUDPServerReceiveBufferSize(testUDPServerReceiveBufferSize),
+		WithUDPServerRequestCallback(func(data []byte) ([]byte, error) {
+			requestBuffer := bytes.NewReader(data)
+			requestDataReader := NewDataReader(requestBuffer)
+			requestBytes, err := requestDataReader.Bytes(len(data))
+			if err != nil {
+				return nil, err
+			}
+
+			responseBytes := []byte(strings.ToUpper(string(requestBytes)))
+			responseBuffer := &bytes.Buffer{}
+			responseReader := NewDataWriter(responseBuffer)
+			err = responseReader.Bytes(responseBytes)
+			if err != nil {
+				return nil, err
+			}
+
+			return responseBuffer.Bytes(), nil
+		}),
+	))
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	defer server.Disconnect()
+
+	// TODO 完成客户端后补充
+}