1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-11 05:30:24 +08:00

finish Init Connect

This commit is contained in:
源文雨
2023-10-15 01:07:38 +09:00
parent 9918dd8ec8
commit 5e3e113fdf
9 changed files with 361 additions and 75 deletions

View File

@@ -1,8 +1,11 @@
package nano
import (
"encoding/base64"
"net/url"
"runtime"
"strings"
"unsafe"
)
func getFuncNameWithSkip(n int) string {
@@ -66,3 +69,49 @@ func UnderlineToCamel(s string) string {
}
return sb.String()
}
// resolveURI github.com/wdvxdr1123/ZeroBot/driver/uri.go
func resolveURI(addr string) (network, address string) {
network, address = "tcp", addr
uri, err := url.Parse(addr)
if err == nil && uri.Scheme != "" {
scheme, ext, _ := strings.Cut(uri.Scheme, "+")
if ext != "" {
network = ext
uri.Scheme = scheme // remove `+unix`/`+tcp4`
if ext == "unix" {
uri.Host, uri.Path, _ = strings.Cut(uri.Path, ":")
uri.Host = base64.StdEncoding.EncodeToString(StringToBytes(uri.Host)) // special handle for unix
}
address = uri.String()
}
}
return
}
// slice is the runtime representation of a slice.
// It cannot be used safely or portably and its representation may
// change in a later release.
//
// Unlike reflect.SliceHeader, its Data field is sufficient to guarantee the
// data it references will not be garbage collected.
type slice struct {
data unsafe.Pointer
len int
cap int
}
// BytesToString 没有内存开销的转换
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// StringToBytes 没有内存开销的转换
func StringToBytes(s string) (b []byte) {
bh := (*slice)(unsafe.Pointer(&b))
sh := (*slice)(unsafe.Pointer(&s))
bh.data = sh.data
bh.len = sh.len
bh.cap = sh.len
return b
}