2023-12-22 15:26:31 +01:00
|
|
|
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2023-12-22 01:44:50 +01:00
|
|
|
package logranger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2023-12-23 20:29:38 +01:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2023-12-22 01:44:50 +01:00
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Connection represents a connection to a network resource.
|
|
|
|
type Connection struct {
|
|
|
|
conn net.Conn
|
|
|
|
id string
|
|
|
|
rb *bufio.Reader
|
|
|
|
wb *bufio.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConnection creates a new Connection object with the provided net.Conn.
|
|
|
|
// The Connection object holds a reference to the provided net.Conn, along with an ID string,
|
|
|
|
// bufio.Reader, and bufio.Writer. It returns a pointer to the created Connection object.
|
|
|
|
func NewConnection(nc net.Conn) *Connection {
|
|
|
|
c := &Connection{
|
|
|
|
conn: nc,
|
2023-12-23 20:29:38 +01:00
|
|
|
id: NewConnectionID(),
|
2023-12-22 01:44:50 +01:00
|
|
|
rb: bufio.NewReader(nc),
|
|
|
|
wb: bufio.NewWriter(nc),
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2023-12-23 20:29:38 +01:00
|
|
|
|
|
|
|
// NewConnectionID generates a new unique message ID using a random number generator
|
|
|
|
// and returns it as a hexadecimal string.
|
|
|
|
func NewConnectionID() string {
|
|
|
|
return fmt.Sprintf("%x", rand.Int63())
|
|
|
|
}
|