ZeroMQ Go 오픈소스를 활용하여 C코드에서 사용 할 수 있는 Library 테스트

 

(ZeroMQ callback function)

  • Make a library for using at C code.
  • Make a C code.
  • Build by linking a library to C code.

ZeroMq Opensource URL

 

go-zeromq/zmq4

[WIP] Pure-Go implementation of ZeroMQ-4. Contribute to go-zeromq/zmq4 development by creating an account on GitHub.

github.com

download go command

  • go get github.com/go-zeromq/zmq4
package main
/*
#include <stdio.h>
typedef int (*FuncPtr)(int, int);

static inline int Shoot(FuncPtr func,int a,int b)
{
        func(a,b);
}
*/
import "C"
import (
        "context"
        "github.com/go-zeromq/zmq4"
        "log"
)

//export Rcv
func Rcv(t C.FuncPtr) {   
        log.SetPrefix("psenvsub: ")
        sub := zmq4.NewSub(context.Background())
        defer sub.Close()
        err := sub.Dial("tcp://localhost:5563")
        if err != nil {
                log.Fatalf("could not dial: %v", err)
        }
        err = sub.SetOption(zmq4.OptionSubscribe, "test1") //Received only named "test1" key.
        if err != nil {
                log.Fatalf("could not subscribe: %v", err)
        }
        var a int =0
        var b int =0
        for {
                msg, err := sub.Recv()
                if err != nil {
                        log.Fatalf("could not receive message: %v", err)
                }
                log.Printf("[%s] %s\n", msg.Frames[0], msg.Frames[1])
                C.Shoot(t,C.int(a),C.int(b))

                a+=1
                b+=1
        }
}

func main() {} // 반드시 main function 작성.

library.go(수신용 라이브러리 Go파일)

go tool 명령어를 실행하여 라이브러리 빌드

  • command 실행-> go build -o library.so -buildmode=c-shared library.go
#include <stdio.h>
#include "library.h"

int plus (int first, int second)
{
    printf("first:%d second:%d!!!!!!!!!!!!!!!!!!!!!!!!!!!\n",first,second);
        return first + second;
}

int main()
{
        Rcv(plus);
        return 0;
}

rev.c(라이브러리 callback함수)

gcc를 이용하여 빌드

  • gcc -o rcv ./rcv.c ./library.so

 

 

'언어 > Golang' 카테고리의 다른 글

Pure zmq4 based on golang 오픈소스를 활용하여 PUB/SUB 테스트  (0) 2020.11.23
Golang  (0) 2017.05.22

+ Recent posts