forked from james-barrow/golang-ipc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
221 lines (147 loc) · 3.96 KB
/
example.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"log"
"time"
ipc "github.com/james-barrow/golang-ipc"
)
func main() {
log.Println("starting")
go server()
client()
}
func server() {
//&ipc.ServerConfig{Encryption: false}
sc, err := ipc.StartServer("testtest", nil)
if err != nil {
log.Println(err)
return
}
connectionChan := make(chan *ipc.Connection, 1)
go func() {
for {
m, err := sc.Read()
if err == nil {
if m.Status == "Connected" {
connectionChan <- m.Connection
} else if m.MsgType > 0 {
log.Println("Server recieved: "+string(m.Data)+" - Message type: ", m.MsgType)
}
} else {
log.Println("Server error")
log.Println(err)
break
}
}
}()
connection := <-connectionChan
go serverSend(connection)
go serverSend1(connection)
serverSend2(connection)
}
func serverSend(connection *ipc.Connection) {
for {
err := connection.Write(3, []byte("Hello Client 4"))
err = connection.Write(23, []byte("Hello Client 5"))
err = connection.Write(65, []byte("Hello Client 6"))
if err != nil {
//fmt.Println(err)
}
time.Sleep(time.Second / 30)
}
}
func serverSend1(connection *ipc.Connection) {
for {
connection.Write(5, []byte("Hello Client 1"))
connection.Write(7, []byte("Hello Client 2"))
connection.Write(9, []byte("Hello Client 3"))
time.Sleep(time.Second / 30)
}
}
func serverSend2(connection *ipc.Connection) {
for {
err := connection.Write(88, []byte("Hello Client 7"))
err = connection.Write(99, []byte("Hello Client 8"))
err = connection.Write(22, []byte("Hello Client 9"))
if err != nil {
//fmt.Println(err)
}
time.Sleep(time.Second / 30)
}
}
func client() {
//config := &ipc.ClientConfig{Encryption: false}
cc, err := ipc.StartClient("testtest", nil)
if err != nil {
log.Println(err)
return
}
go func() {
for {
m, err := cc.Read()
if err != nil {
// An error is only returned if the recieved channel has been closed,
//so you know the connection has either been intentionally closed or has timmed out waiting to connect/re-connect.
break
}
if m.MsgType == -1 { // message type -1 is status change
//log.Println("Status: " + m.Status)
}
if m.MsgType == -2 { // message type -2 is an error, these won't automatically cause the recieve channel to close.
log.Println("Error: " + err.Error())
}
if m.MsgType > 0 { // all message types above 0 have been recieved over the connection
log.Println(" Message type: ", m.MsgType)
log.Println("Client recieved: " + string(m.Data))
}
//}
}
}()
go clientSend(cc)
go clientSend(cc)
clientSend2(cc)
}
func clientSend(cc *ipc.Client) {
for {
_ = cc.Write(14, []byte("hello server 4"))
_ = cc.Write(44, []byte("hello server 5"))
_ = cc.Write(88, []byte("hello server 6"))
time.Sleep(time.Second / 20)
}
}
func clientSend1(cc *ipc.Client) {
for {
_ = cc.Write(1, []byte("hello server 1"))
_ = cc.Write(9, []byte("hello server 2"))
_ = cc.Write(34, []byte("hello server 3"))
time.Sleep(time.Second / 20)
}
}
func clientSend2(cc *ipc.Client) {
for {
_ = cc.Write(444, []byte("hello server 7"))
_ = cc.Write(234, []byte("hello server 8"))
_ = cc.Write(111, []byte("hello server 9"))
time.Sleep(time.Second / 20)
}
}
func clientRecv(c *ipc.Client) {
for {
m, err := c.Read()
if err != nil {
// An error is only returned if the recieved channel has been closed,
//so you know the connection has either been intentionally closed or has timmed out waiting to connect/re-connect.
break
}
if m.MsgType == -1 { // message type -1 is status change
//log.Println("Status: " + m.Status)
}
if m.MsgType == -2 { // message type -2 is an error, these won't automatically cause the recieve channel to close.
log.Println("Error: " + err.Error())
}
if m.MsgType > 0 { // all message types above 0 have been recieved over the connection
log.Println(" Message type: ", m.MsgType)
log.Println("Client recieved: " + string(m.Data))
}
//}
}
}