[PD1] Structured client.
Still need to create crypto object and use it to encrypt messages Need to create TLS still
This commit is contained in:
parent
278b8e1a73
commit
cdaae8fb7e
9 changed files with 252 additions and 110 deletions
16
Projs/PD1/internal/utils/networking/client.go
Normal file
16
Projs/PD1/internal/utils/networking/client.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package networking
|
||||
|
||||
import "net"
|
||||
|
||||
type Client[T any] struct {
|
||||
Connection Connection[T]
|
||||
}
|
||||
|
||||
func NewClient[T any]() Client[T] {
|
||||
dialConn, err := net.Dial("tcp", "localhost:8080")
|
||||
if err != nil {
|
||||
panic("Could not open connection to server")
|
||||
}
|
||||
conn := NewConnection[T](dialConn)
|
||||
return Client[T]{Connection: conn}
|
||||
}
|
35
Projs/PD1/internal/utils/networking/connection.go
Normal file
35
Projs/PD1/internal/utils/networking/connection.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package networking
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
)
|
||||
|
||||
type Connection[T any] struct {
|
||||
Conn net.Conn
|
||||
encoder *json.Encoder
|
||||
decoder *json.Decoder
|
||||
}
|
||||
|
||||
|
||||
func NewConnection[T any](netConn net.Conn) Connection[T] {
|
||||
return Connection[T]{
|
||||
Conn: netConn,
|
||||
encoder: json.NewEncoder(netConn),
|
||||
decoder: json.NewDecoder(netConn),
|
||||
}
|
||||
}
|
||||
|
||||
func (jc Connection[T]) Send(obj T) {
|
||||
if err := jc.encoder.Encode(&obj); err != nil {
|
||||
panic("Failed encoding data or sending it to connection")
|
||||
}
|
||||
}
|
||||
|
||||
func (jc Connection[T]) Receive() T {
|
||||
var obj T
|
||||
if err := jc.decoder.Decode(&obj); err != nil {
|
||||
panic("Failed decoding data or reading it from connection")
|
||||
}
|
||||
return obj
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
package networking
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
type Server[T any] struct {
|
||||
listener net.Listener
|
||||
C chan Connection[T]
|
||||
}
|
||||
|
||||
type Client[T any] struct {
|
||||
conn net.Conn
|
||||
}
|
||||
|
||||
type Connection[T any] struct {
|
||||
Conn net.Conn
|
||||
encoder *json.Encoder
|
||||
decoder *json.Decoder
|
||||
}
|
||||
|
||||
func NewServer[T any](port int) Server[T]{
|
||||
|
||||
listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
|
||||
if err != nil {
|
||||
panic("Server could not bind to address")
|
||||
}
|
||||
return Server[T]{
|
||||
listener:listener,
|
||||
C: make(chan Connection[T]),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server[T]) ListenLoop() {
|
||||
|
||||
for {
|
||||
listenerConn, err := s.listener.Accept()
|
||||
if err != nil {
|
||||
panic("Server could not accept connection")
|
||||
}
|
||||
conn := NewConnection[T](listenerConn)
|
||||
s.C <- conn
|
||||
}
|
||||
}
|
||||
|
||||
func (c Client[T]) Connect() Connection[T] {
|
||||
dialConn, err := net.Dial("tcp", "localhost:8080")
|
||||
if err != nil {
|
||||
panic("Could not open connection to server")
|
||||
}
|
||||
return NewConnection[T](dialConn)
|
||||
}
|
||||
|
||||
func NewConnection[T any](netConn net.Conn) Connection[T] {
|
||||
return Connection[T]{
|
||||
Conn: netConn,
|
||||
encoder: json.NewEncoder(netConn),
|
||||
decoder: json.NewDecoder(netConn),
|
||||
}
|
||||
}
|
||||
|
||||
func (jc Connection[T]) Send(obj T) {
|
||||
if err := jc.encoder.Encode(&obj); err != nil {
|
||||
panic("Failed encoding data or sending it to connection")
|
||||
}
|
||||
}
|
||||
|
||||
func (jc Connection[T]) Receive() T {
|
||||
var obj T
|
||||
if err := jc.decoder.Decode(&obj); err != nil {
|
||||
panic("Failed decoding data or reading it from connection")
|
||||
}
|
||||
return obj
|
||||
}
|
35
Projs/PD1/internal/utils/networking/server.go
Normal file
35
Projs/PD1/internal/utils/networking/server.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package networking
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
type Server[T any] struct {
|
||||
listener net.Listener
|
||||
C chan Connection[T]
|
||||
}
|
||||
|
||||
func NewServer[T any](port int) Server[T]{
|
||||
|
||||
listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
|
||||
if err != nil {
|
||||
panic("Server could not bind to address")
|
||||
}
|
||||
return Server[T]{
|
||||
listener:listener,
|
||||
C: make(chan Connection[T]),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server[T]) ListenLoop() {
|
||||
|
||||
for {
|
||||
listenerConn, err := s.listener.Accept()
|
||||
if err != nil {
|
||||
panic("Server could not accept connection")
|
||||
}
|
||||
conn := NewConnection[T](listenerConn)
|
||||
s.C <- conn
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue