no message
parent
801bdd69bd
commit
8a086a77b3
Binary file not shown.
6
main.go
6
main.go
|
@ -2,11 +2,11 @@ package main
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"go-tcpFramework-rudy/config"
|
||||
"go-tcpFramework-rudy/logger"
|
||||
"go-tcpFramework-rudy/network"
|
||||
"log"
|
||||
"runtime"
|
||||
"tcpteamplate/config"
|
||||
"tcpteamplate/logger"
|
||||
"tcpteamplate/network"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -4,14 +4,16 @@ import (
|
|||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"go-tcpFramework-rudy/logger"
|
||||
"go-tcpFramework-rudy/util"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"tcpteamplate/config"
|
||||
"tcpteamplate/logger"
|
||||
"tcpteamplate/util"
|
||||
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
@ -43,13 +45,16 @@ type ConnectionManager struct {
|
|||
|
||||
func withTimeoutConnectContext(parent context.Context, timeOut time.Duration) ConnectionContext {
|
||||
ctx, _ := context.WithTimeout(parent, timeOut)
|
||||
ret := ConnectionContext{ctx, "0", "socket", 0, "", 0, nil, nil, nil, DefaultPackageHandler}
|
||||
if config.IfSecure() {
|
||||
|
||||
}
|
||||
ret := ConnectionContext{ctx, "0", "socket", 0, "", 0, nil, nil, nil, DefaultPackageHandler, false, []byte{}}
|
||||
return ret
|
||||
}
|
||||
|
||||
func withCalcelConnectContext(parent context.Context, timeOut time.Duration) ConnectionContext {
|
||||
ctx, _ := context.WithCancel(parent)
|
||||
ret := ConnectionContext{ctx, "0", "socket", 0, "", 0, nil, nil, nil, DefaultPackageHandler}
|
||||
ret := ConnectionContext{ctx, "0", "socket", 0, "", 0, nil, nil, nil, DefaultPackageHandler, false, []byte{}}
|
||||
return ret
|
||||
}
|
||||
|
||||
|
|
|
@ -8,17 +8,14 @@ import (
|
|||
_ "encoding/binary"
|
||||
"errors"
|
||||
_ "fmt"
|
||||
"go-tcpFramework-rudy/logger"
|
||||
"go-tcpFramework-rudy/util"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
"tcpteamplate/config"
|
||||
"tcpteamplate/logger"
|
||||
"tcpteamplate/util"
|
||||
"time"
|
||||
_ "time"
|
||||
|
||||
"gobase/utils"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
_ "github.com/golang/protobuf/proto"
|
||||
"github.com/hprose/hprose-golang/io"
|
||||
|
|
|
@ -4,15 +4,15 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go-tcpFramework-rudy/config"
|
||||
"go-tcpFramework-rudy/logger"
|
||||
"go-tcpFramework-rudy/util"
|
||||
"go-tcpFramework-rudy/zk"
|
||||
"gobase/utils"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"tcpteamplate/config"
|
||||
"tcpteamplate/logger"
|
||||
"tcpteamplate/util"
|
||||
"tcpteamplate/zk"
|
||||
"time"
|
||||
|
||||
zkdriver "github.com/samuel/go-zookeeper/zk"
|
||||
|
|
|
@ -3,19 +3,19 @@ package test
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
_ "github.com/golang/protobuf/proto"
|
||||
_ "github.com/hprose/hprose-golang/io"
|
||||
_ "github.com/trysh/ttb"
|
||||
_ "go-tcpFramework-rudy/protocol/pb"
|
||||
_ "io"
|
||||
"log"
|
||||
"net"
|
||||
_ "net/http/pprof"
|
||||
_ "runtime"
|
||||
_ "tcpteamplate/protocol/pb"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
_ "github.com/golang/protobuf/proto"
|
||||
_ "github.com/hprose/hprose-golang/io"
|
||||
_ "github.com/trysh/ttb"
|
||||
)
|
||||
|
||||
/*
|
||||
tcp 测试程序,测试以下场景
|
||||
|
@ -61,7 +61,7 @@ func SendPackage(writer *net.TCPConn, payload []byte, length int) error {
|
|||
//log.Println("Send length",wb)
|
||||
b = append(b, packbuf.Bytes()...)
|
||||
b = append(b, payload[0:length]...)
|
||||
log.Println(b[0:5],b[5:])
|
||||
log.Println(b[0:5], b[5:])
|
||||
_, e = writer.Write(b[0:5])
|
||||
if e != nil {
|
||||
log.Println(e.Error())
|
||||
|
|
|
@ -1,34 +1,35 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"go-tcpFramework-rudy/tools/mq"
|
||||
"qiniupkg.com/x/log.v7"
|
||||
"tcpteamplate/tools/mq"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"qiniupkg.com/x/log.v7"
|
||||
)
|
||||
|
||||
func Callback(key string,msg string) {
|
||||
log.Print(key,msg)
|
||||
func Callback(key string, msg string) {
|
||||
log.Print(key, msg)
|
||||
}
|
||||
func TestMq(t *testing.T) {
|
||||
func TestMq(t *testing.T) {
|
||||
cli := mq.RedisMqClient{}
|
||||
e := cli.Connect("49.235.25.67:16379",0,"")
|
||||
if nil != e{
|
||||
e := cli.Connect("49.235.25.67:16379", 0, "")
|
||||
if nil != e {
|
||||
log.Print(e.Error())
|
||||
t.Error(e)
|
||||
}
|
||||
cli.Subscribe("msg",Callback)
|
||||
cli.Public("msg","shit")
|
||||
cli.Public("msg","shit2")
|
||||
cli.Public("msg","shit3")
|
||||
cli.Subscribe("msg", Callback)
|
||||
cli.Public("msg", "shit")
|
||||
cli.Public("msg", "shit2")
|
||||
cli.Public("msg", "shit3")
|
||||
|
||||
mqcli := mq.MqttClient{}
|
||||
e = mqcli.Connect("tcp://" + "49.235.25.67:1883",0,"122")
|
||||
e = mqcli.Connect("tcp://"+"49.235.25.67:1883", 0, "122")
|
||||
log.Print(e)
|
||||
e = mqcli.Subscribe("/dev",Callback)
|
||||
e = mqcli.Subscribe("/dev", Callback)
|
||||
log.Print(e)
|
||||
mqcli.Public("/dev","shit")
|
||||
for true{
|
||||
mqcli.Public("/dev", "shit")
|
||||
for true {
|
||||
time.Sleep(100)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,40 +2,37 @@ package mq
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"go-tcpFramework-rudy/util"
|
||||
"gopkg.in/redis.v3"
|
||||
"io"
|
||||
"tcpteamplate/util"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"gopkg.in/redis.v3"
|
||||
"qiniupkg.com/x/log.v7"
|
||||
|
||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
type CallBack func(string,string)
|
||||
type CallBack func(string, string)
|
||||
|
||||
//通用mq库,支持发布,订阅
|
||||
type MqClient interface {
|
||||
Subscribe(topic string,callback *CallBack) error
|
||||
Public(topic string,data string) error
|
||||
Subscribe(topic string, callback *CallBack) error
|
||||
Public(topic string, data string) error
|
||||
}
|
||||
|
||||
|
||||
type RedisMqClient struct {
|
||||
client *redis.Client
|
||||
Address string
|
||||
client *redis.Client
|
||||
Address string
|
||||
Password string
|
||||
}
|
||||
type MqttClient struct {
|
||||
client MQTT.Client
|
||||
Address string
|
||||
type MqttClient struct {
|
||||
client MQTT.Client
|
||||
Address string
|
||||
ClientId string
|
||||
Options *MQTT.ClientOptions
|
||||
Options *MQTT.ClientOptions
|
||||
}
|
||||
|
||||
func (this *MqttClient) Connect(ip string,port int64,id string) error {
|
||||
func (this *MqttClient) Connect(ip string, port int64, id string) error {
|
||||
this.Address = ip
|
||||
|
||||
opts := MQTT.NewClientOptions().AddBroker(ip)
|
||||
|
@ -49,7 +46,7 @@ func (this *MqttClient) Connect(ip string,port int64,id string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (this *RedisMqClient) Connect(ip string,port int64,passwd string) error {
|
||||
func (this *RedisMqClient) Connect(ip string, port int64, passwd string) error {
|
||||
option := &redis.Options{
|
||||
Addr: ip,
|
||||
Password: passwd,
|
||||
|
@ -70,11 +67,11 @@ func (this *RedisMqClient) Connect(ip string,port int64,passwd string) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
func (this *MqttClient) Subscribe(topic string,callback CallBack) error {
|
||||
func (this *MqttClient) Subscribe(topic string, callback CallBack) error {
|
||||
handler := func(c MQTT.Client, msg MQTT.Message) {
|
||||
msg.Payload()
|
||||
msg.Topic()
|
||||
callback(msg.Topic(),string(msg.Payload()))
|
||||
callback(msg.Topic(), string(msg.Payload()))
|
||||
}
|
||||
opts := MQTT.NewClientOptions().AddBroker(this.Address)
|
||||
opts.SetClientID(this.ClientId)
|
||||
|
@ -86,33 +83,33 @@ func (this *MqttClient) Subscribe(topic string,callback CallBack) error {
|
|||
if token := this.client.Connect(); token.Wait() && token.Error() != nil {
|
||||
return token.Error()
|
||||
}
|
||||
this.client.Subscribe(topic,0,handler)
|
||||
this.client.Subscribe(topic, 0, handler)
|
||||
return nil
|
||||
}
|
||||
func (this *RedisMqClient) Subscribe(topic string,callback CallBack) error {
|
||||
if nil == this{
|
||||
func (this *RedisMqClient) Subscribe(topic string, callback CallBack) error {
|
||||
if nil == this {
|
||||
return errors.New(util.ERR_NULL_POINTER)
|
||||
}
|
||||
pubsub, err := this.client.Subscribe(topic)
|
||||
if nil != err{
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
x := make(chan bool,1)
|
||||
x := make(chan bool, 1)
|
||||
go func() {
|
||||
x <- true
|
||||
defer pubsub.Close()
|
||||
for true{
|
||||
for true {
|
||||
msg, err := pubsub.ReceiveMessage()
|
||||
if nil != err{
|
||||
if nil != err {
|
||||
log.Print(err)
|
||||
if err == io.EOF{
|
||||
if err == io.EOF {
|
||||
return
|
||||
}
|
||||
}
|
||||
callback(topic,msg.Payload)
|
||||
callback(topic, msg.Payload)
|
||||
}
|
||||
}()
|
||||
<- x
|
||||
<-x
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -133,45 +130,45 @@ func creatRedisClient(option *redis.Options) (*redis.Client, error) {
|
|||
return client, nil
|
||||
}
|
||||
|
||||
func (this *RedisMqClient) Public(topic string,data string) error {
|
||||
if nil == this{
|
||||
func (this *RedisMqClient) Public(topic string, data string) error {
|
||||
if nil == this {
|
||||
return errors.New(util.ERR_NULL_POINTER)
|
||||
}
|
||||
// 检测client有效性
|
||||
if nil != this.client {
|
||||
_, err := this.client.Ping().Result()
|
||||
_, err := this.client.Ping().Result()
|
||||
if nil != err {
|
||||
this.client.Close()
|
||||
// 尝试3次重连
|
||||
for i := 0; i < 3; i++ {
|
||||
this.client, err = creatRedisClient(&redis.Options{
|
||||
Addr:this.Address,
|
||||
Password:this.Password,
|
||||
Addr: this.Address,
|
||||
Password: this.Password,
|
||||
})
|
||||
if this.client != nil {
|
||||
if this.client != nil {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_, err := this.client.Publish(topic, data).Result()
|
||||
if nil != err{
|
||||
if nil != err {
|
||||
log.Print(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *MqttClient) Public(topic string,data string) error {
|
||||
if nil == this{
|
||||
func (this *MqttClient) Public(topic string, data string) error {
|
||||
if nil == this {
|
||||
return errors.New(util.ERR_NULL_POINTER)
|
||||
}
|
||||
// 检测client有效性
|
||||
if true != this.client.IsConnected() {
|
||||
|
||||
}
|
||||
token := this.client.Publish(topic,0, false,data)
|
||||
token := this.client.Publish(topic, 0, false, data)
|
||||
token.Wait()
|
||||
if nil != token.Error(){
|
||||
if nil != token.Error() {
|
||||
log.Print(token.Error())
|
||||
return token.Error()
|
||||
}
|
||||
|
|
|
@ -2,12 +2,13 @@ package zk
|
|||
|
||||
import (
|
||||
_ "bytes"
|
||||
zk "github.com/samuel/go-zookeeper/zk"
|
||||
"go-tcpFramework-rudy/config"
|
||||
"log"
|
||||
"os"
|
||||
_ "strings"
|
||||
"tcpteamplate/config"
|
||||
"time"
|
||||
|
||||
zk "github.com/samuel/go-zookeeper/zk"
|
||||
)
|
||||
|
||||
var g_zkCnn *zk.Conn
|
||||
|
|
Loading…
Reference in New Issue