Feat: done tcp

This commit is contained in:
2025-11-10 10:02:55 +08:00
parent 78d89595a1
commit 9a39bcda40
17 changed files with 689 additions and 0 deletions

53
tcp/request.go Normal file
View File

@@ -0,0 +1,53 @@
package tcp
import (
"encoding/json"
"net"
)
type Method string
const (
MethodGET = "GET"
MethodPOST = "POST"
MethodPUT = "PUT"
MethodDELETE = "DELETE"
MethodSOCKET = "SOCKET"
)
type RequestHeader struct {
Method Method `json:"method"`
Route string `json:"route"`
}
type Request struct {
Method Method
Route string
RemoteAddr string
Body []byte
}
func (self *Request) Header() ([]byte, error) {
b, err := json.Marshal(RequestHeader{
Method: self.Method,
Route: self.Route,
})
if err != nil {
return []byte{}, err
}
return b, nil
}
func NewRequest(conn net.Conn, header RequestHeader, body []byte) *Request {
return &Request{
Method: header.Method,
Route: header.Route,
RemoteAddr: conn.RemoteAddr().String(),
Body: body,
}
}