mirror of
https://github.com/fhmq/hmq.git
synced 2026-05-04 07:08:32 +00:00
feat(hmq):add windows pipe socket.Use the open source project npipe, which nicely encapsulates the operations of windows pipe and returns the connection type net.Conn.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
encJson "encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/natefinch/npipe"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -165,6 +166,10 @@ func (b *Broker) Start() {
|
||||
if b.config.Port == "" && b.config.UnixFilePath != "" {
|
||||
go b.StartUnixSocketClientListening(b.config.UnixFilePath, true)
|
||||
}
|
||||
//listen client over windows pipe
|
||||
if b.config.Port == "" && b.config.UnixFilePath == "" && b.config.WindowsPipeName != "" {
|
||||
go b.StartPipeSocketListening(b.config.WindowsPipeName, true)
|
||||
}
|
||||
|
||||
//listen for cluster
|
||||
if b.config.Cluster.Port != "" {
|
||||
@@ -274,11 +279,11 @@ func (b *Broker) StartClientListening(Tls bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Broker) StartUnixSocketClientListening(socketPath string, UnixSocket bool) {
|
||||
func (b *Broker) StartUnixSocketClientListening(socketPath string, unixSocket bool) {
|
||||
var err error
|
||||
var l net.Listener
|
||||
for {
|
||||
if UnixSocket {
|
||||
if unixSocket {
|
||||
if FileExist(socketPath) {
|
||||
if err != nil {
|
||||
log.Error("Remove Unix socketPath ", zap.Error(err))
|
||||
@@ -328,6 +333,57 @@ func (b *Broker) StartUnixSocketClientListening(socketPath string, UnixSocket bo
|
||||
}
|
||||
}
|
||||
|
||||
// StartPipeSocketListening We use the open source npipe library to support pipe communication in windows
|
||||
func (b *Broker) StartPipeSocketListening(pipeName string, usePipe bool) {
|
||||
var err error
|
||||
var ln *npipe.PipeListener
|
||||
|
||||
for {
|
||||
if usePipe {
|
||||
fmt.Println(pipeName)
|
||||
ln, err = npipe.Listen(pipeName)
|
||||
log.Info("Start Listening client on ", zap.String("pipeName", pipeName))
|
||||
}
|
||||
if err == nil {
|
||||
break // successfully listening
|
||||
}
|
||||
log.Error("Error listening on ", zap.Error(err))
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
|
||||
tmpDelay := 10 * ACCEPT_MIN_SLEEP
|
||||
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
if ne, ok := err.(net.Error); ok && ne.Temporary() {
|
||||
log.Error(
|
||||
"Temporary Client Accept Error(%v), sleeping %dms",
|
||||
zap.Error(ne),
|
||||
zap.Duration("sleeping", tmpDelay/time.Millisecond),
|
||||
)
|
||||
|
||||
time.Sleep(tmpDelay)
|
||||
tmpDelay *= 2
|
||||
if tmpDelay > ACCEPT_MAX_SLEEP {
|
||||
tmpDelay = ACCEPT_MAX_SLEEP
|
||||
}
|
||||
} else {
|
||||
log.Error("Accept error", zap.Error(err))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
tmpDelay = ACCEPT_MIN_SLEEP
|
||||
go func() {
|
||||
err := b.handleConnection(CLIENT, conn)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Broker) StartClusterListening() {
|
||||
var hp string = b.config.Cluster.Host + ":" + b.config.Cluster.Port
|
||||
log.Info("Start Listening cluster on ", zap.String("hp", hp))
|
||||
|
||||
@@ -19,21 +19,22 @@ import (
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
type Config struct {
|
||||
Worker int `json:"workerNum"`
|
||||
HTTPPort string `json:"httpPort"`
|
||||
Host string `json:"host"`
|
||||
Port string `json:"port"`
|
||||
Cluster RouteInfo `json:"cluster"`
|
||||
Router string `json:"router"`
|
||||
TlsHost string `json:"tlsHost"`
|
||||
TlsPort string `json:"tlsPort"`
|
||||
WsPath string `json:"wsPath"`
|
||||
WsPort string `json:"wsPort"`
|
||||
WsTLS bool `json:"wsTLS"`
|
||||
TlsInfo TLSInfo `json:"tlsInfo"`
|
||||
Debug bool `json:"debug"`
|
||||
Plugin Plugins `json:"plugins"`
|
||||
UnixFilePath string `json:"unixFilePath"`
|
||||
Worker int `json:"workerNum"`
|
||||
HTTPPort string `json:"httpPort"`
|
||||
Host string `json:"host"`
|
||||
Port string `json:"port"`
|
||||
Cluster RouteInfo `json:"cluster"`
|
||||
Router string `json:"router"`
|
||||
TlsHost string `json:"tlsHost"`
|
||||
TlsPort string `json:"tlsPort"`
|
||||
WsPath string `json:"wsPath"`
|
||||
WsPort string `json:"wsPort"`
|
||||
WsTLS bool `json:"wsTLS"`
|
||||
TlsInfo TLSInfo `json:"tlsInfo"`
|
||||
Debug bool `json:"debug"`
|
||||
Plugin Plugins `json:"plugins"`
|
||||
UnixFilePath string `json:"unixFilePath"`
|
||||
WindowsPipeName string `json:"windowsPipeName"`
|
||||
}
|
||||
|
||||
type Plugins struct {
|
||||
|
||||
Reference in New Issue
Block a user