-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code documentation and README.md updated
Version v0.0.3 released
- Loading branch information
1 parent
0f7c3af
commit 4b99be9
Showing
5 changed files
with
209 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
// 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. | ||
|
||
// Client Package provides a TCP client for communicating with the dataserver. | ||
package client | ||
|
||
import ( | ||
"net" | ||
"time" | ||
|
||
"github.com/teonet-go/dataserver" | ||
) | ||
|
||
const ( | ||
startPacketLength = dataserver.StartPacketLength | ||
// startPacketLength is the length in bytes of the start packet | ||
// sent by the client to initialize the connection. | ||
// reserved for the future constant: | ||
// startPacketLength = dataserver.StartPacketLength | ||
|
||
// ChankPacketLength is the max length in bytes of each data | ||
// packet sent over the connection. | ||
ChankPacketLength = 21 | ||
timeout = 5 * time.Second | ||
|
||
// timeout is the read/write timeout for network operations. | ||
// reserved for the future constant: | ||
// timeout = 5 * time.Second | ||
) | ||
|
||
// DataClient is TCP Data Client data structure and methods receiver. | ||
// DataClient is the client data structure containing the remote address, | ||
// start packet, and network connection. | ||
type DataClient struct { | ||
remoteAddr string | ||
startPacket *dataserver.StartPacket | ||
net.Conn | ||
} | ||
|
||
// NewDataClient creates new DataClient, connects to DataServer and sends | ||
// read or write request depending on the type of start packet. | ||
// NewDataClient creates a new DataClient instance, connects to the DataServer | ||
// specified by remoteAddr, and sends the provided startPacket to initialize | ||
// the connection. It returns the new DataClient and any error. | ||
func NewDataClient(remoteAddr string, startPacket *dataserver.StartPacket) ( | ||
dc *DataClient, err error) { | ||
|
||
|
@@ -51,9 +66,10 @@ func (dc *DataClient) Write(b []byte) (n int, err error) { | |
return dc.Conn.Write(b) | ||
} | ||
|
||
// Read reads data from the connection. | ||
// Read reads data from the connection into the provided byte slice b. | ||
// It returns the number of bytes read and any error encountered. | ||
// Read can be made to time out and return an error after a fixed | ||
// time limit; see SetDeadline and SetReadDeadline. | ||
// time limit by setting deadlines on the connection. | ||
func (dc *DataClient) Read(b []byte) (n int, err error) { | ||
return dc.Conn.Read(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
// TCP client example | ||
// 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 client example. | ||
package main | ||
|
||
import ( | ||
|
@@ -13,11 +16,14 @@ import ( | |
) | ||
|
||
const ( | ||
appShort = "Simple TCP client" | ||
appVersion = "0.0.1" | ||
remoteAddr = "localhost:8089" | ||
appShort = "Simple TCP client" // Application short name. | ||
appVersion = "0.0.1" // Application version. | ||
remoteAddr = "localhost:8089" // Remote address of the dataserver. | ||
) | ||
|
||
// main is the entry point for the Dataserver TCP Client example. It connects | ||
// to a dataserver, writes some data, then reads back data. It demonstrates | ||
// using the dataserver client API. | ||
func main() { | ||
|
||
// Show application logo | ||
|
@@ -54,27 +60,6 @@ func main() { | |
// Execute write data by case | ||
switch j { | ||
|
||
// // Write all data buffer | ||
// case 1: | ||
// title(dc, "write all data buffer") | ||
// err = dc.WriteAll() | ||
// if err != nil { | ||
// log.Println("write all, error:", err) | ||
// return | ||
// } | ||
|
||
// // Write all data as one string | ||
// case 2: | ||
// title(dc, "write all data as one string") | ||
// io.WriteString(dc, buf.String()) | ||
// dc.Close() | ||
|
||
// // Write using io.Copy | ||
// case 3: | ||
// title(dc, "write using io.Copy") | ||
// io.Copy(dc.Conn, buf) | ||
// dc.Close() | ||
|
||
// Write using read write by chanks | ||
case 4: | ||
title(dc, "write using read write by chanks") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
// TCP server example | ||
// 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 ( | ||
|
@@ -13,11 +17,14 @@ import ( | |
) | ||
|
||
const ( | ||
appShort = "Simple TCP server" | ||
appVersion = "0.0.1" | ||
localAddr = ":8089" | ||
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 | ||
|
@@ -97,21 +104,4 @@ func main() { | |
return | ||
} | ||
} | ||
|
||
// for { | ||
// // Set Request for read file and get result string in callback | ||
// request := dataserver.StartPacket{ | ||
// Type: dataserver.READ, | ||
// Data: []byte("start"), | ||
// } | ||
// ds.SetReadRequest(request, func(buf *bytes.Buffer, err error) { | ||
// if err != nil { | ||
// fmt.Println("error:", err) | ||
// return | ||
// } | ||
// fmt.Printf("\ngot result:\n%s\n", buf.String()) | ||
// }) | ||
// } | ||
|
||
// select {} | ||
} |
Oops, something went wrong.