1 Star 0 Fork 0

似是故人来/v8go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
inspector.go 4.91 KB
一键复制 编辑 原始数据 按行查看 历史
似是故人来 提交于 2024-11-04 09:11 . inspector
package v8go
//#include <stdlib.h>
//#include "v8go.h"
import "C"
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"sync/atomic"
"time"
"unsafe"
"golang.org/x/net/context"
"golang.org/x/net/websocket"
)
var globalInspectorIdGenerator int32 = 0
var globalInspectorMap sync.Map
type InspectorInfo struct {
Description string `json:"description"`
Title string `json:"title"`
Type string `json:"type"`
Url string `json:"url"`
WebSocketDebuggerUrl string `json:"webSocketDebuggerUrl"`
}
type InspectorVersion struct {
Browser string `json:"Browser"`
ProtocolVersion string `json:"Protocol-Version"`
}
type InspectorServer struct {
InspectorClientPtr C.RawInspectorClientPtr
InspectorId int32
Isolate *Isolate
Context *Context
CurrentClientConnection atomic.Pointer[ClientConnection]
Lock sync.Mutex
JSONVersion *InspectorVersion
JSONLIST []*InspectorInfo
Server *http.Server
connected chan struct{}
}
func (i *InspectorServer) WaitDebugger() {
select {
case <-i.connected:
time.Sleep(time.Second)
return
}
}
func (i *InspectorServer) WebSocketServe(conn *websocket.Conn) {
defer func() {
i.CurrentClientConnection.Store(nil)
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if i.CurrentClientConnection.Load() != nil {
fmt.Printf("Debugger Already In Use \n")
return
}
clientConn, childCtx := NewClientConnection(ctx, conn, i.ProcessClientCall)
oldConnection := i.CurrentClientConnection.Swap(clientConn)
if oldConnection != nil {
oldConnection.Stop()
}
select {
case <-childCtx.Done():
}
}
func (i *InspectorServer) ProcessClientCall(connection *ClientConnection, data []byte) {
mes := string(data)
//直接调用C的函数
fmt.Printf("Receive Message %s \n", mes)
cstr := C.CString(mes)
defer C.free(unsafe.Pointer(cstr))
C.OnReceiveMessage(i.InspectorClientPtr, cstr)
}
func (i *InspectorServer) StartWebSocketServer(port uint32) {
handler := &websocket.Server{
Config: websocket.Config{},
Handler: i.WebSocketServe,
}
address := fmt.Sprintf("127.0.0.1:%d", port)
i.JSONVersion = &InspectorVersion{
Browser: "Puerts/v1.0.0",
ProtocolVersion: "1.1",
}
i.JSONLIST = append(i.JSONLIST, &InspectorInfo{
Description: "go instance",
Title: "debug tools for V8",
Type: "node",
Url: "",
WebSocketDebuggerUrl: fmt.Sprintf("ws://%s", address),
})
mux := &http.ServeMux{}
mux.Handle("/json/version", http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
s, _ := json.Marshal(i.JSONVersion)
writer.Write(s)
}))
mux.Handle("/json/list", http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
s, _ := json.Marshal(i.JSONLIST)
writer.Write(s)
}))
mux.Handle("/json", http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
s, _ := json.Marshal(i.JSONLIST)
writer.Write(s)
}))
mux.Handle("/", http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
handler.ServeHTTP(writer, request)
}))
server := &http.Server{Addr: address, Handler: mux}
i.Server = server
go server.ListenAndServe()
}
func (i *InspectorServer) Destroy() {
FreeCPtr(unsafe.Pointer(i.InspectorClientPtr))
i.InspectorClientPtr = nil
globalInspectorMap.Delete(i.InspectorId)
i.Server.Shutdown(context.TODO())
}
func NewInspectorServer(iso *Isolate, ctx *Context, port uint32) *InspectorServer {
inspectorId := atomic.AddInt32(&globalInspectorIdGenerator, 1)
cl := &InspectorServer{
InspectorClientPtr: C.NewInspectorClient(ctx.ptr, C.int32_t(inspectorId)),
InspectorId: inspectorId,
Isolate: iso,
Context: ctx,
connected: make(chan struct{}, 1000),
}
globalInspectorMap.Store(inspectorId, cl)
cl.StartWebSocketServer(port)
return cl
}
//export goSendMessage
func goSendMessage(inspectorId C.int32_t, message C.RawCharPtr) {
id := int32(inspectorId)
inspectorClientInterface, ok := globalInspectorMap.Load(id)
if !ok {
return
}
inspectorClient := inspectorClientInterface.(*InspectorServer)
conn := inspectorClient.CurrentClientConnection.Load()
if conn == nil {
return
}
msg := C.GoString(message)
fmt.Printf("Send Message %s \n", msg)
websocket.Message.Send(conn.Connection, msg)
}
//export goRunIfWaitingForDebuggerFunc
func goRunIfWaitingForDebuggerFunc(inspectorId C.int32_t) {
id := int32(inspectorId)
inspectorClientInterface, ok := globalInspectorMap.Load(id)
if !ok {
return
}
inspectorClient := inspectorClientInterface.(*InspectorServer)
inspectorClient.connected <- struct{}{}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/hasika/v8go.git
[email protected]:hasika/v8go.git
hasika
v8go
v8go
master

搜索帮助