-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (90 loc) · 2.59 KB
/
main.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
// Copyright 2023 Kirill Scherba <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Dataserver TCP server example.
package main
import (
"bytes"
"fmt"
"io"
"log"
"time"
"github.com/teonet-go/dataserver"
"github.com/teonet-go/dataserver/server"
)
const (
appShort = "Simple TCP server" // Application short name.
appVersion = "0.0.1" // Application version.
localAddr = ":8089" // Local address of the dataserver.
)
// main is the entry point for the Dataserver TCP Server example. It initializes
// the dataserver, sets up read and write requests in a loop with callbacks,
// and handles errors.
func main() {
// Show application logo
fmt.Println(appShort + " ver " + appVersion)
// Set microseconds in log
log.SetFlags(log.Default().Flags() + log.Lmicroseconds)
// Create new Data Server
ds, err := server.NewDataServer(localAddr)
if err != nil {
fmt.Println("start data server error:", err)
}
for {
var buf = new(bytes.Buffer)
// Set read request
startPacket := dataserver.MakeStartPacket(dataserver.READ, "start", -1)
err := ds.SetRequest(startPacket,
func(startPacket *dataserver.StartPacket, reader io.Reader, err error) {
if err != nil {
fmt.Println("an error occurred while initializing the reader:", err)
return
}
fmt.Println("ready to read data")
p := make([]byte, server.ChankPacketLength)
for {
n, err := reader.Read(p)
if err != nil {
if err == io.EOF {
err = nil
}
break
}
buf.Write(p[:n])
fmt.Printf("got data, n, err: '%s' %d %v\n",
string(p[:n]), n, err)
}
log.Printf("done %x, err: %v\n\n", startPacket.Data, err)
// Set write request
startPacket = dataserver.MakeStartPacket(dataserver.WRITE, "start", -1)
ds.SetRequest(startPacket,
func(startPacket *dataserver.StartPacket, writer io.Writer, err error) {
if err != nil {
fmt.Println("an error occurred while initializing the writer:", err)
return
}
fmt.Println("ready to write data")
for {
n, err := buf.Read(p)
if err != nil {
break
}
writer.Write(p[:n])
fmt.Printf("send data, n, err: '%s' %d %v\n",
string(p[:n]), n, err)
}
log.Printf("done %x, err: %v\n\n", startPacket.Data, err)
},
)
},
)
if err != nil {
if err == server.ErrExistsStartPacket {
time.Sleep(250 * time.Millisecond)
continue
}
log.Println("set read request error:", err)
return
}
}
}