-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBismuthNative.go
More file actions
135 lines (119 loc) · 3.46 KB
/
BismuthNative.go
File metadata and controls
135 lines (119 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package BismuthNative
import (
"encoding/json"
"fmt"
"io"
"log"
"net"
"reflect"
"strconv"
"strings"
"time"
)
type bismuthNative struct {
tcpAddr string
server string
port int
socketErr error
socketAPI net.Conn
}
func (this *bismuthNative) cnxCheck() {
if this.socketAPI == nil {
this.socketAPI, this.socketErr = net.Dial("tcp", this.tcpAddr)
if this.socketErr != nil {
log.Fatalln(this.socketErr)
}
this.socketErr = this.socketAPI.(*net.TCPConn).SetKeepAlive(true)
if this.socketErr != nil {
log.Fatalln(this.socketErr)
}
this.socketErr = this.socketAPI.(*net.TCPConn).SetKeepAlivePeriod(30 * time.Second)
if this.socketErr != nil {
log.Fatalln(this.socketErr)
}
}
}
func (this *bismuthNative) cnxIsClosed() {
this.socketAPI.SetReadDeadline(time.Now())
var one []byte
if _, err := this.socketAPI.Read(one); err == io.EOF {
log.Printf("Client disconnect: %s", this.socketAPI.RemoteAddr().String())
this.socketAPI.Close()
this.socketAPI = nil
} else {
var zero time.Time
this.socketAPI.SetReadDeadline(zero)
}
}
func (this *bismuthNative) send(message string) {
this.cnxIsClosed()
this.cnxCheck()
_, this.socketErr = this.socketAPI.Write([]byte(message))
if this.socketErr != nil {
log.Printf("ERROR soccket send() [%s] message: %s", this.socketErr.Error(), message)
}
}
func New(nodeServer string, nodePort int) bismuthNative {
tcpAddr := strings.Join([]string{nodeServer, strconv.Itoa(nodePort)}, ":")
bn := bismuthNative{tcpAddr, nodeServer, nodePort, nil, nil}
bn.cnxCheck()
return bn
}
func (this *bismuthNative) Close() {
this.socketAPI.Close()
}
func (this *bismuthNative) Command(cmds ...interface{}) string {
var socketMessage string
var socketReceiveMessage, bJson []byte
var socketLenReceiveMessage int
for _, cmd := range cmds {
rt := reflect.TypeOf(cmd)
switch rt.Kind() {
case reflect.Int:
var cmdApi int = cmd.(int)
socketMessage = fmt.Sprintf("%010v%d", len(strconv.Itoa(cmdApi)), cmdApi)
this.send(socketMessage)
case reflect.Bool:
var cmdApi bool = cmd.(bool)
if cmdApi == true {
this.send("0000000004true")
} else {
this.send("0000000005false")
}
case reflect.String:
var cmdApi string = cmd.(string)
bJson, this.socketErr = json.Marshal(cmdApi)
if this.socketErr != nil {
log.Printf("ERROR JSON: %s", this.socketErr.Error())
} else {
socketMessage = string(bJson)
socketMessage = fmt.Sprintf("%010v%s", strconv.Itoa(len(socketMessage)), socketMessage)
this.send(socketMessage)
}
case reflect.Slice:
itemsPtr := reflect.ValueOf(cmd)
sliceItems, err := itemsPtr.Interface().([]string)
if !err {
log.Println("ERROR slice []string")
}
bJson, this.socketErr = json.Marshal(sliceItems)
if this.socketErr != nil {
log.Printf("ERROR JSON: %s", this.socketErr.Error())
} else {
socketMessage = string(bJson)
socketMessage = fmt.Sprintf("%010v%s", strconv.Itoa(len(socketMessage)), socketMessage)
this.send(socketMessage)
}
default:
println("")
log.Fatalln("Unknow paramater type: ", rt, "for BismuthAPI command")
}
}
var nByte int
var lengthSocketMessage []byte = make([]byte, 10)
nByte, this.socketErr = this.socketAPI.Read(lengthSocketMessage)
socketLenReceiveMessage, _ = strconv.Atoi(string(lengthSocketMessage[:nByte]))
socketReceiveMessage = make([]byte, socketLenReceiveMessage)
nByte, this.socketErr = this.socketAPI.Read(socketReceiveMessage)
return string(socketReceiveMessage[:nByte])
}