Commit f63f95ab authored by wei.xuan's avatar wei.xuan

feat: 调整结构 支持动态库生成

parent 7e97decb
project_name: echocli project_name: majora-go
before: before:
hooks: hooks:
# You may remove this if you don't use go modules. # You may remove this if you don't use go modules.
...@@ -30,7 +30,7 @@ builds: ...@@ -30,7 +30,7 @@ builds:
gcflags: gcflags:
- all=-trimpath={{.Env.GOPATH}} - all=-trimpath={{.Env.GOPATH}}
ldflags: ldflags:
- -s -w -X app.Version={{.Version}} - -s -w -X virjar.com/majora/main.Version=v0.01.beta
# tags: # tags:
# - osusergo # - osusergo
# - netgo # - netgo
...@@ -42,10 +42,12 @@ archives: ...@@ -42,10 +42,12 @@ archives:
linux: Linux linux: Linux
windows: Windows windows: Windows
amd64: x86_64 amd64: x86_64
checksum: checksum:
name_template: 'checksums.txt' name_template: 'checksums.txt'
snapshot: snapshot:
name_template: "{{ incpatch .Version }}-next" name_template: "{{ incpatch .Version }}-next"
changelog: changelog:
sort: asc sort: asc
filters: filters:
......
build: build:
CGO_ENABLE=0 go build -ldflags '-w -s' -o majora-cli CGO_ENABLE=0 go build -trimpath -ldflags '-w -s' -o majora-cli
release: release:
goreleaser build --rm-dist --snapshot --single-target goreleaser build --rm-dist --snapshot --single-target
...@@ -8,7 +8,11 @@ release: ...@@ -8,7 +8,11 @@ release:
releaseall: releaseall:
goreleaser build --rm-dist --snapshot goreleaser build --rm-dist --snapshot
sdk:
CGO_ENABLED=1 go build -trimpath -ldflags '-s -w --extldflags "-static -fpic"' --buildmode=c-shared -o libmajora.so lib/sdk.go
clean: clean:
rm -fr dist rm -fr dist
rm -fr majora-cli rm -fr majora-cli
\ No newline at end of file rm -fr libmajora.so
rm -fr libmajora.h
\ No newline at end of file
...@@ -2,14 +2,13 @@ package main ...@@ -2,14 +2,13 @@ package main
import ( import (
"flag" "flag"
"fmt" "log"
"net/http" "net/http"
_ "net/http/pprof" //nolint:gosec _ "net/http/pprof" //nolint:gosec
)
var ( "virjar.com/majora/client"
Version string "virjar.com/majora/common"
BuildDate string "virjar.com/majora/logger"
) )
var ( var (
...@@ -17,13 +16,14 @@ var ( ...@@ -17,13 +16,14 @@ var (
pprof bool pprof bool
debugAddr string debugAddr string
natServer string natServer string
Version string
) )
func init() { func init() {
flag.IntVar(&logLevel, "log", 1, "log logLevel") flag.IntVar(&logLevel, "log", 1, "log logLevel")
flag.BoolVar(&pprof, "pprof", false, "pprof") flag.BoolVar(&pprof, "pprof", false, "enable pprof")
flag.StringVar(&debugAddr, "debugPort", "127.0.0.1:6060", "debugPort") flag.StringVar(&debugAddr, "debugPort", common.PprofAddr, "debugPort")
flag.StringVar(&natServer, "natServer", fmt.Sprintf("%s:%d", DefNatServerHost, DefPort), "natServer") flag.StringVar(&natServer, "natServer", common.DefNatAddr, "natServer")
flag.Parse() flag.Parse()
} }
...@@ -33,15 +33,13 @@ func initPprof() { ...@@ -33,15 +33,13 @@ func initPprof() {
return return
} }
go func() { go func() {
logger.Fatal().Err(http.ListenAndServe(debugAddr, nil)) log.Fatal(http.ListenAndServe(debugAddr, nil))
}() }()
} }
func main() { func main() {
InitLogger(logLevel)
initPprof() initPprof()
//logger.Info().Msgf("build in %s, version %s", BuildDate, Version) logger.Info().Msgf("current version %s", Version)
NewClient(WithNatServerAddr(natServer)).StartUp() client.NewClient(client.WithNatServerAddr(natServer))
select {} select {}
} }
package main package client
import ( import (
"bufio" "bufio"
...@@ -11,48 +11,40 @@ import ( ...@@ -11,48 +11,40 @@ import (
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
) "virjar.com/majora/common"
"virjar.com/majora/logger"
const ( "virjar.com/majora/protocol"
DefNatServerHost = "majora.virjar.com"
DefPort = 5879
BufSize = 1000
) )
func NewOptions() *Options { func NewOptions() *Options {
return &Options{ return &Options{
natHostPort: fmt.Sprintf("%s:%d", DefNatServerHost, DefPort), NatHostPort: common.DefNatAddr,
clientID: uuid.New().String(), ClientID: uuid.New().String(),
bufSize: BufSize,
} }
} }
type Client struct { type Client struct {
options *Options Options *Options
natTunnel atomic.Value natTunnel atomic.Value
codec ICodec Codec protocol.ICodec
connStore sync.Map connStore sync.Map
} }
func NewClient(opts ...Option) *Client { func NewClient(opts ...Option) {
options := NewOptions() options := NewOptions()
for _, opt := range opts { for _, opt := range opts {
opt(options) opt(options)
} }
return &Client{options: options, codec: NewDefCodec()} client := &Client{Options: options, Codec: protocol.NewDefCodec()}
} client.StartUp()
func NewDefClient() *Client {
options := NewOptions()
return &Client{options: options, codec: NewDefCodec()}
} }
func (client *Client) StartUp() { func (client *Client) StartUp() {
if client.options == nil { if client.Options == nil {
client.options = NewOptions() client.Options = NewOptions()
logger.Warn().Msgf("use default nat host %s port %d", DefNatServerHost, DefPort) logger.Warn().Msgf("use default nat addr %s", common.DefNatServerHost)
} }
client.connect() client.connect()
client.register() client.register()
...@@ -60,9 +52,9 @@ func (client *Client) StartUp() { ...@@ -60,9 +52,9 @@ func (client *Client) StartUp() {
} }
func (client *Client) register() { func (client *Client) register() {
packet := TypeRegister.CreatePacket() packet := protocol.TypeRegister.CreatePacket()
packet.Extra = client.options.clientID packet.Extra = client.Options.ClientID
encode, _ := client.codec.Encode(packet) encode, _ := client.Codec.Encode(packet)
if err := client.WriteAndFlush(encode); err != nil { if err := client.WriteAndFlush(encode); err != nil {
logger.Error().Msgf("register to nat server with error %s", err.Error()) logger.Error().Msgf("register to nat server with error %s", err.Error())
} else { } else {
...@@ -74,7 +66,7 @@ func (client *Client) handleNatEvent() { ...@@ -74,7 +66,7 @@ func (client *Client) handleNatEvent() {
go func() { go func() {
for { for {
reader := bufio.NewReader(client.natTunnel.Load().(net.Conn)) reader := bufio.NewReader(client.natTunnel.Load().(net.Conn))
majoraPacket, err := client.codec.Decode(reader) majoraPacket, err := client.Codec.Decode(reader)
if errors.Is(err, io.EOF) { if errors.Is(err, io.EOF) {
logger.Error().Msgf("*********disconnect******") logger.Error().Msgf("*********disconnect******")
client.reConnect() client.reConnect()
...@@ -88,17 +80,17 @@ func (client *Client) handleNatEvent() { ...@@ -88,17 +80,17 @@ func (client *Client) handleNatEvent() {
logger.Debug().Msgf("receive packet type %s", majoraPacket.Ttype.ToString()) logger.Debug().Msgf("receive packet type %s", majoraPacket.Ttype.ToString())
switch majoraPacket.Ttype { switch majoraPacket.Ttype {
case TypeHeartbeat: case protocol.TypeHeartbeat:
client.handleHeartbeatMessage() client.handleHeartbeatMessage()
case TypeConnect: case protocol.TypeConnect:
go client.handleConnect(majoraPacket) go client.handleConnect(majoraPacket)
case TypeTransfer: case protocol.TypeTransfer:
go client.handleTransfer(majoraPacket) go client.handleTransfer(majoraPacket)
case TypeDisconnect: case protocol.TypeDisconnect:
go client.handleDisconnectMessage(majoraPacket) go client.handleDisconnectMessage(majoraPacket)
case TypeControl: case protocol.TypeControl:
go client.handleControlMessage(majoraPacket) go client.handleControlMessage(majoraPacket)
case TypeDestroy: case protocol.TypeDestroy:
go client.handleDestroyMessage() go client.handleDestroyMessage()
} }
} }
...@@ -107,18 +99,18 @@ func (client *Client) handleNatEvent() { ...@@ -107,18 +99,18 @@ func (client *Client) handleNatEvent() {
func (client *Client) reConnect() { func (client *Client) reConnect() {
// 已经check 过 // 已经check 过
hostPort := client.options.natHostPort hostPort := client.Options.NatHostPort
var ( var (
conn net.Conn conn net.Conn
err error err error
) )
for { for {
conn, err = net.DialTimeout(TCP, hostPort, connTimeout) conn, err = net.DialTimeout(common.TCP, hostPort, common.ConnTimeout)
if err != nil || conn == nil { if err != nil || conn == nil {
// 不断重试 // 不断重试
logger.Info().Msgf("reconnect to nathost with error %+v ...", err) logger.Info().Msgf("reconnect to nathost with error %+v ...", err)
time.Sleep(reConnInterval) time.Sleep(common.ReConnInterval)
} else { } else {
break break
} }
...@@ -130,13 +122,13 @@ func (client *Client) reConnect() { ...@@ -130,13 +122,13 @@ func (client *Client) reConnect() {
} }
func (client *Client) connect() { func (client *Client) connect() {
hostPort := client.options.natHostPort hostPort := client.Options.NatHostPort
if len(hostPort) == 0 { if len(hostPort) == 0 {
panic("invalid nat host/port info") panic("invalid nat host/port info")
} }
conn, err := net.DialTimeout(TCP, hostPort, connTimeout) conn, err := net.DialTimeout(common.TCP, hostPort, common.ConnTimeout)
if err != nil || conn == nil { if err != nil || conn == nil {
panic(fmt.Sprintf("connect to nathost %s with err %s", hostPort, err.Error())) panic(fmt.Sprintf("connect to nathost %s with err %s", hostPort, err.Error()))
} }
......
package main package client
import ( import (
"bufio" "bufio"
...@@ -7,20 +7,24 @@ import ( ...@@ -7,20 +7,24 @@ import (
"io" "io"
"net" "net"
"strings" "strings"
"virjar.com/majora/common"
"virjar.com/majora/logger"
"virjar.com/majora/protocol"
) )
func (client *Client) handleHeartbeatMessage() { func (client *Client) handleHeartbeatMessage() {
go func() { go func() {
logger.Debug().Msg("receive heartbeat message from nat server") logger.Debug().Msg("receive heartbeat message from nat server")
packet := TypeHeartbeat.CreatePacket() packet := protocol.TypeHeartbeat.CreatePacket()
encode, _ := client.codec.Encode(packet) encode, _ := client.Codec.Encode(packet)
if err := client.WriteAndFlush(encode); err != nil { if err := client.WriteAndFlush(encode); err != nil {
logger.Error().Msgf("flush heart beat message error %s", err.Error()) logger.Error().Msgf("flush heart beat message error %s", err.Error())
} }
}() }()
} }
func (client *Client) handleConnect(packet *MajoraPacket) { func (client *Client) handleConnect(packet *protocol.MajoraPacket) {
if len(packet.Extra) == 0 { if len(packet.Extra) == 0 {
client.disconnect(packet, "empty extra") client.disconnect(packet, "empty extra")
client.connStore.Delete(packet.SerialNumber) client.connStore.Delete(packet.SerialNumber)
...@@ -41,18 +45,18 @@ func (client *Client) handleConnect(packet *MajoraPacket) { ...@@ -41,18 +45,18 @@ func (client *Client) handleConnect(packet *MajoraPacket) {
) )
addr := fmt.Sprintf("%s:%s", hostPort[0], hostPort[1]) addr := fmt.Sprintf("%s:%s", hostPort[0], hostPort[1])
conn, err = net.DialTimeout(TCP, addr, connTimeout) conn, err = net.DialTimeout(common.TCP, addr, common.ConnTimeout)
if err != nil { if err != nil {
client.disconnect(packet, "connect to target host error "+err.Error()) client.disconnect(packet, "connect to target host error "+err.Error())
return return
} }
client.connStore.Store(packet.SerialNumber, conn) client.connStore.Store(packet.SerialNumber, conn)
majoraPacket := TypeConnectReady.CreatePacket() majoraPacket := protocol.TypeConnectReady.CreatePacket()
majoraPacket.SerialNumber = packet.SerialNumber majoraPacket.SerialNumber = packet.SerialNumber
majoraPacket.Extra = client.options.clientID majoraPacket.Extra = client.Options.ClientID
encode, _ := client.codec.Encode(majoraPacket) encode, _ := client.Codec.Encode(majoraPacket)
if err := client.WriteAndFlush(encode); err != nil { if err := client.WriteAndFlush(encode); err != nil {
logger.Error().Msgf("handleConnect message error %s", err.Error()) logger.Error().Msgf("handleConnect message error %s", err.Error())
_ = conn.Close() _ = conn.Close()
...@@ -71,7 +75,7 @@ func (client *Client) WriteAndFlush(packet []byte) error { ...@@ -71,7 +75,7 @@ func (client *Client) WriteAndFlush(packet []byte) error {
return writer.Flush() return writer.Flush()
} }
func (client *Client) handleTransfer(packet *MajoraPacket) { func (client *Client) handleTransfer(packet *protocol.MajoraPacket) {
load, ok := client.connStore.Load(packet.SerialNumber) load, ok := client.connStore.Load(packet.SerialNumber)
if !ok || load == nil { if !ok || load == nil {
...@@ -90,7 +94,7 @@ func (client *Client) handleTransfer(packet *MajoraPacket) { ...@@ -90,7 +94,7 @@ func (client *Client) handleTransfer(packet *MajoraPacket) {
} }
} }
func (client *Client) handleConnection(conn net.Conn, packet *MajoraPacket) { func (client *Client) handleConnection(conn net.Conn, packet *protocol.MajoraPacket) {
logger.Debug().Msg("handleConnection start...") logger.Debug().Msg("handleConnection start...")
reader := bufio.NewReader(conn) reader := bufio.NewReader(conn)
for { for {
...@@ -109,17 +113,17 @@ func (client *Client) handleConnection(conn net.Conn, packet *MajoraPacket) { ...@@ -109,17 +113,17 @@ func (client *Client) handleConnection(conn net.Conn, packet *MajoraPacket) {
break break
} }
pack := TypeTransfer.CreatePacket() pack := protocol.TypeTransfer.CreatePacket()
pack.Data = buf pack.Data = buf
pack.SerialNumber = packet.SerialNumber pack.SerialNumber = packet.SerialNumber
encode, _ := client.codec.Encode(pack) encode, _ := client.Codec.Encode(pack)
if err = client.WriteAndFlush(encode); err != nil { if err = client.WriteAndFlush(encode); err != nil {
logger.Error().Msgf("write to nat server error %+v", err) logger.Error().Msgf("write to nat server error %+v", err)
} }
} }
} }
func (client *Client) handleDisconnectMessage(packet *MajoraPacket) { func (client *Client) handleDisconnectMessage(packet *protocol.MajoraPacket) {
go func() { go func() {
// delete from local cache // delete from local cache
load, ok := client.connStore.Load(packet.SerialNumber) load, ok := client.connStore.Load(packet.SerialNumber)
...@@ -132,7 +136,7 @@ func (client *Client) handleDisconnectMessage(packet *MajoraPacket) { ...@@ -132,7 +136,7 @@ func (client *Client) handleDisconnectMessage(packet *MajoraPacket) {
}() }()
} }
func (client *Client) handleControlMessage(packet *MajoraPacket) { func (client *Client) handleControlMessage(packet *protocol.MajoraPacket) {
logger.Debug().Msgf("handleControlMessage %s", string(packet.Data)) logger.Debug().Msgf("handleControlMessage %s", string(packet.Data))
} }
...@@ -140,11 +144,11 @@ func (client *Client) handleDestroyMessage() { ...@@ -140,11 +144,11 @@ func (client *Client) handleDestroyMessage() {
} }
func (client *Client) disconnect(packet *MajoraPacket, msg string) { func (client *Client) disconnect(packet *protocol.MajoraPacket, msg string) {
logger.Info().Msgf("disconnect to server %s", msg) logger.Info().Msgf("disconnect to server %s", msg)
disconnectCmd := TypeDisconnect.CreatePacket() disconnectCmd := protocol.TypeDisconnect.CreatePacket()
disconnectCmd.SerialNumber = packet.SerialNumber disconnectCmd.SerialNumber = packet.SerialNumber
disconnectCmd.Data = []byte(msg) disconnectCmd.Data = []byte(msg)
encode, _ := client.codec.Encode(disconnectCmd) encode, _ := client.Codec.Encode(disconnectCmd)
_ = client.WriteAndFlush(encode) _ = client.WriteAndFlush(encode)
} }
package main package client
type ( type (
Options struct { Options struct {
natHostPort string NatHostPort string
clientID string ClientID string
bufSize int
} }
Option func(*Options) Option func(*Options)
...@@ -12,12 +11,12 @@ type ( ...@@ -12,12 +11,12 @@ type (
func WithNatServerAddr(natHostPort string) Option { func WithNatServerAddr(natHostPort string) Option {
return func(options *Options) { return func(options *Options) {
options.natHostPort = natHostPort options.NatHostPort = natHostPort
} }
} }
func WithClientID(clientID string) Option { func WithClientID(clientID string) Option {
return func(options *Options) { return func(options *Options) {
options.clientID = clientID options.ClientID = clientID
} }
} }
package main package common
import ( import (
"bytes" "bytes"
...@@ -10,17 +10,10 @@ import ( ...@@ -10,17 +10,10 @@ import (
const ( const (
MagicSize = 8 MagicSize = 8
HeaderSize = 12
BodySize = 4
TypeSize = 1 TypeSize = 1
ExtraSize = 1 ExtraSize = 1
SerialNumberSize = 8 SerialNumberSize = 8
HeaderSizeSize = 1 MAGIC = int64(0x6D616A6F72613031)
KeyLenSize = 1
ValueLenSize = 1
MaxFrameLength = 8 * 1024
MAGIC = int64(0x6D616A6F72613031)
) )
const ( const (
...@@ -28,8 +21,15 @@ const ( ...@@ -28,8 +21,15 @@ const (
) )
const ( const (
connTimeout = time.Second * 10 DefNatServerHost = "majora.virjar.com"
reConnInterval = time.Second * 5 DefPort = 5879
DefNatAddr = "majora.virjar.com:5879"
PprofAddr = "127.0.0.1:6060"
)
const (
ConnTimeout = time.Second * 10
ReConnInterval = time.Second * 5
) )
var ( var (
......
module ints.com/goecho module virjar.com/majora
go 1.17 go 1.17
require ( require (
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/rs/zerolog v1.24.0 github.com/rs/zerolog v1.25.0
) )
...@@ -4,8 +4,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= ...@@ -4,8 +4,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.24.0 h1:76ivFxmVSRs1u2wUwJVg5VZDYQgeH1JpoS6ndgr9Wy8= github.com/rs/zerolog v1.25.0 h1:Rj7XygbUHKUlDPcVdoLyR91fJBsduXj5fRxyqIQj/II=
github.com/rs/zerolog v1.24.0/go.mod h1:7KHcEGe0QZPOm2IE4Kpb5rTh6n1h2hIgS5OOnu1rUaI= github.com/rs/zerolog v1.25.0/go.mod h1:7KHcEGe0QZPOm2IE4Kpb5rTh6n1h2hIgS5OOnu1rUaI=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
......
package main
import "C"
import (
"virjar.com/majora/client"
"virjar.com/majora/protocol"
)
//export NewDefClient
func NewDefClient(asyn C.int) {
options := client.NewOptions()
cli := &client.Client{Options: options, Codec: protocol.NewDefCodec()}
if int(asyn) > 0 {
go cli.StartUp()
} else {
cli.StartUp()
}
}
//export NewClientWithNatServer
func NewClientWithNatServer(addr *C.char, clientID *C.char, asyn C.int) {
options := client.NewOptions()
newAddr := C.GoString(addr)
newClientID := C.GoString(clientID)
if len(newAddr) > 0 {
options.NatHostPort = newAddr
}
if len(newClientID) > 0 {
options.ClientID = newClientID
}
cli := &client.Client{Options: options, Codec: protocol.NewDefCodec()}
if int(asyn) > 0 {
go cli.StartUp()
} else {
cli.StartUp()
}
}
func main() {}
/* Code generated by cmd/cgo; DO NOT EDIT. */
/* package command-line-arguments */
#line 1 "cgo-builtin-export-prolog"
#include <stddef.h> /* for ptrdiff_t below */
#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H
#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif
#endif
/* Start of preamble from import "C" comments. */
/* End of preamble from import "C" comments. */
/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"
#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
#endif
/* End of boilerplate cgo prologue. */
#ifdef __cplusplus
extern "C" {
#endif
extern void NewDefClient(int asyn);
extern void NewClientWithNatServer(char* addr, char* clientID, int asyn);
#ifdef __cplusplus
}
#endif
File added
package main package logger
import ( import (
"os" "os"
...@@ -11,8 +11,15 @@ var ( ...@@ -11,8 +11,15 @@ var (
logger zerolog.Logger logger zerolog.Logger
) )
func InitLogger(level int) { func init() {
zerolog.SetGlobalLevel(zerolog.Level(level)) zerolog.SetGlobalLevel(zerolog.InfoLevel)
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339, NoColor: true} output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339, NoColor: true}
logger = zerolog.New(output).With().Caller().Timestamp().Logger() logger = zerolog.New(output).With().Caller().Timestamp().Logger()
} }
var (
Error = logger.Error
Info = logger.Info
Warn = logger.Warn
Debug = logger.Debug
)
package main
import (
"sync"
)
const (
bufSize = 1024 * 4 // 4k
)
var (
bytesPool = sync.Pool{
New: func() interface{} {
return make([]byte, bufSize)
},
}
)
func GetBuffer() *[]byte {
bytes := bytesPool.Get().([]byte)
return &bytes
}
func PutBuffer(buf *[]byte) {
bytesPool.Put((*buf)[:0])
}
package main package protocol
import ( import (
"bufio" "bufio"
......
package main package protocol
import ( import (
"bufio" "bufio"
"virjar.com/majora/common"
"virjar.com/majora/logger"
) )
type Decoder interface { type Decoder interface {
...@@ -13,60 +16,60 @@ type MajoraPacketDecoder struct { ...@@ -13,60 +16,60 @@ type MajoraPacketDecoder struct {
} }
func (mpd *MajoraPacketDecoder) Decode(reader *bufio.Reader) (pack *MajoraPacket, err error) { func (mpd *MajoraPacketDecoder) Decode(reader *bufio.Reader) (pack *MajoraPacket, err error) {
magicbs := make([]byte, MagicSize) magicbs := make([]byte, common.MagicSize)
_, err = reader.Read(magicbs) _, err = reader.Read(magicbs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !ReadMagic(magicbs) { if !common.ReadMagic(magicbs) {
return nil, InvalidMagicError return nil, common.InvalidMagicError
} }
frameLen, err := ReadInt32(reader) frameLen, err := common.ReadInt32(reader)
if err != nil { if err != nil {
return nil, InvalidSizeError return nil, common.InvalidSizeError
} }
// type // type
msgType, err := ReadByte(reader) msgType, err := common.ReadByte(reader)
if err != nil { if err != nil {
logger.Error().Msgf("read type error %+v", err) logger.Error().Msgf("read type error %+v", err)
return nil, InvalidSizeError return nil, common.InvalidSizeError
} }
pack = &MajoraPacket{} pack = &MajoraPacket{}
pack.Ttype = MajoraPacketType(msgType) pack.Ttype = MajoraPacketType(msgType)
// num // num
pack.SerialNumber, err = ReadInt64(reader) pack.SerialNumber, err = common.ReadInt64(reader)
if err != nil { if err != nil {
logger.Error().Msgf("read type error %+v", err) logger.Error().Msgf("read type error %+v", err)
return nil, InvalidSizeError return nil, common.InvalidSizeError
} }
// extra size // extra size
extraSize, err := ReadByte(reader) extraSize, err := common.ReadByte(reader)
if err != nil { if err != nil {
logger.Error().Msgf("read type error %+v", err) logger.Error().Msgf("read type error %+v", err)
return nil, InvalidSizeError return nil, common.InvalidSizeError
} }
extra, err := ReadN(int(extraSize), reader) extra, err := common.ReadN(int(extraSize), reader)
if err != nil { if err != nil {
logger.Error().Msgf("read type error %+v", err) logger.Error().Msgf("read type error %+v", err)
return nil, InvalidSizeError return nil, common.InvalidSizeError
} }
pack.Extra = string(extra) pack.Extra = string(extra)
// dataFrame // dataFrame
dataSize := int(frameLen) - TypeSize - SerialNumberSize - ExtraSize - int(extraSize) dataSize := int(frameLen) - common.TypeSize - common.SerialNumberSize - common.ExtraSize - int(extraSize)
if dataSize < 0 { if dataSize < 0 {
logger.Error().Msgf("read type error %+v", err) logger.Error().Msgf("read type error %+v", err)
return nil, InvalidSizeError return nil, common.InvalidSizeError
} }
if dataSize > 0 { if dataSize > 0 {
data, err := ReadN(dataSize, reader) data, err := common.ReadN(dataSize, reader)
if err != nil { if err != nil {
logger.Error().Msgf("read type error %+v", err) logger.Error().Msgf("read type error %+v", err)
} }
......
package main package protocol
import ( import (
"bytes" "bytes"
"virjar.com/majora/common"
) )
type Encoder interface { type Encoder interface {
...@@ -13,9 +15,9 @@ type MajoraPacketEncoder struct { ...@@ -13,9 +15,9 @@ type MajoraPacketEncoder struct {
func (s *MajoraPacketEncoder) Encode(packet *MajoraPacket) ([]byte, error) { func (s *MajoraPacketEncoder) Encode(packet *MajoraPacket) ([]byte, error) {
if packet == nil { if packet == nil {
return nil, NilPacketError return nil, common.NilPacketError
} }
bodyLength := TypeSize + SerialNumberSize + ExtraSize bodyLength := common.TypeSize + common.SerialNumberSize + common.ExtraSize
bodyLength += len(packet.Data) bodyLength += len(packet.Data)
...@@ -29,13 +31,13 @@ func (s *MajoraPacketEncoder) Encode(packet *MajoraPacket) ([]byte, error) { ...@@ -29,13 +31,13 @@ func (s *MajoraPacketEncoder) Encode(packet *MajoraPacket) ([]byte, error) {
) )
// magic 8byte // magic 8byte
buffer.Write(ConvertInt64ToBytes(MAGIC)) buffer.Write(common.ConvertInt64ToBytes(common.MAGIC))
// body length 4byte // body length 4byte
buffer.Write(ConvertInt32ToBytes(int32(bodyLength))) buffer.Write(common.ConvertInt32ToBytes(int32(bodyLength)))
// type 1byte // type 1byte
buffer.WriteByte(byte(packet.Ttype)) buffer.WriteByte(byte(packet.Ttype))
// serial num 4byte // serial num 4byte
buffer.Write(ConvertInt64ToBytes(packet.SerialNumber)) buffer.Write(common.ConvertInt64ToBytes(packet.SerialNumber))
// extra // extra
if len(packet.Extra) > 0 { if len(packet.Extra) > 0 {
......
package main package protocol
type MajoraPacket struct { type MajoraPacket struct {
Ttype MajoraPacketType // 消息类型 Ttype MajoraPacketType // 消息类型
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment