Created
May 29, 2024 12:53
-
-
Save amlwwalker/028a15cce5bd2375d25b9feb2c59c85c to your computer and use it in GitHub Desktop.
attempt at c-shared go plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#cgo LDFLAGS: -ldl | |
#include <dlfcn.h> | |
#include <stdlib.h> | |
typedef void (*initializeFunc)(); | |
typedef void (*handleMessageFunc)(const char* message, char** response); | |
void* loadSO(const char* path) { | |
return dlopen(path, RTLD_LAZY); | |
} | |
initializeFunc getInitializeFunc(void* handle) { | |
return (initializeFunc)dlsym(handle, "Initialize"); | |
} | |
handleMessageFunc getHandleMessageFunc(void* handle) { | |
return (handleMessageFunc)dlsym(handle, "HandleMessage"); | |
} | |
void handle_message(handleMessageFunc fn, const char* message, char** response) { | |
fn(message, response); | |
} | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"unsafe" | |
) | |
type Plugin struct { | |
handle unsafe.Pointer | |
initializeFn unsafe.Pointer | |
handleMessageFn unsafe.Pointer | |
} | |
func LoadPlugin(path string) (*Plugin, error) { | |
cpath := C.CString(path) | |
defer C.free(unsafe.Pointer(cpath)) | |
handle := C.loadSO(cpath) | |
if handle == nil { | |
return nil, fmt.Errorf("failed to load plugin: %s", path) | |
} | |
initializeFn := unsafe.Pointer(C.getInitializeFunc(handle)) | |
if initializeFn == nil { | |
return nil, fmt.Errorf("failed to find Initialize function in plugin: %s", path) | |
} | |
handleMessageFn := unsafe.Pointer(C.getHandleMessageFunc(handle)) | |
if handleMessageFn == nil { | |
return nil, fmt.Errorf("failed to find HandleMessage function in plugin: %s", path) | |
} | |
return &Plugin{handle: handle, initializeFn: initializeFn, handleMessageFn: handleMessageFn}, nil | |
} | |
func (p *Plugin) Initialize() { | |
initializeFn := *(*func())(unsafe.Pointer(&p.initializeFn)) | |
initializeFn() | |
} | |
func (p *Plugin) SendMessage(message []byte) ([]byte, error) { | |
cmessage := C.CString(string(message)) | |
defer C.free(unsafe.Pointer(cmessage)) | |
var cresponse *C.char | |
handleMessageFn := *(*func(*C.char, **C.char))(unsafe.Pointer(&p.handleMessageFn)) | |
handleMessageFn(cmessage, &cresponse) | |
defer C.free(unsafe.Pointer(cresponse)) | |
response := C.GoString(cresponse) | |
return []byte(response), nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
plugin, err := LoadPlugin("./examples/example/plugin.so") | |
if err != nil { | |
panic(err) | |
} | |
plugin.Initialize() | |
message := []byte("Hello Plugin") | |
response, err := plugin.SendMessage(message) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("Received response: %s\n", string(response)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "C" | |
import "fmt" | |
//export Initialize | |
func Initialize() { | |
fmt.Println("Plugin initialized") | |
} | |
//export HandleMessage | |
func HandleMessage(message *C.char, response **C.char) { | |
receivedMessage := C.GoString(message) | |
fmt.Printf("Received message: %s\n", receivedMessage) | |
responseMessage := "Response to: " + receivedMessage | |
*response = C.CString(responseMessage) | |
} | |
func main() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The crash error