package network

import (
	"net"
	"sync/atomic"
	"time"
)

type ConnSafe struct {
	conn atomic.Value
}

func (s *ConnSafe) Set(conn net.Conn) {
	s.conn.Store(conn)
}

func (s *ConnSafe) netConn() net.Conn {
	return s.conn.Load().(net.Conn)
}

func (s *ConnSafe) Read(b []byte) (n int, err error) {
	return s.netConn().Read(b)
}

func (s *ConnSafe) Write(b []byte) (n int, err error) {
	return s.netConn().Write(b)
}

func (s *ConnSafe) Close() error {
	return s.netConn().Close()
}

func (s *ConnSafe) LocalAddr() net.Addr {
	return s.netConn().LocalAddr()
}

func (s *ConnSafe) RemoteAddr() net.Addr {
	return s.netConn().RemoteAddr()
}

func (s *ConnSafe) SetDeadline(t time.Time) error {
	return s.netConn().SetDeadline(t)
}

func (s *ConnSafe) SetReadDeadline(t time.Time) error {
	return s.netConn().SetReadDeadline(t)
}

func (s *ConnSafe) SetWriteDeadline(t time.Time) error {
	return s.netConn().SetWriteDeadline(t)
}
