123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package network
- import (
- "fmt"
- "net"
- "time"
- )
- type connectionReadOptions func(conn net.Conn) error
- func withReadDeadline(duration time.Duration) connectionReadOptions {
- return func(conn net.Conn) error {
- if duration == 0 {
- return nil
- }
- err := conn.SetReadDeadline(time.Now().Add(duration))
- if err != nil {
- return err
- }
- return nil
- }
- }
- type connectionWriteOptions func(conn net.Conn) error
- func withWriteDeadline(duration time.Duration) connectionWriteOptions {
- return func(conn net.Conn) error {
- if duration == 0 {
- return nil
- }
- err := conn.SetWriteDeadline(time.Now().Add(duration))
- if err != nil {
- return err
- }
- return nil
- }
- }
- func readUDP(conn *net.UDPConn, bufferSize int, opts ...connectionReadOptions) ([]byte, *net.UDPAddr, error) {
- buffer := make([]byte, bufferSize)
- for _, opt := range opts {
- err := opt(conn)
- if err != nil {
- return nil, nil, err
- }
- }
- n, rAddr, err := conn.ReadFromUDP(buffer)
- if err != nil {
- return nil, nil, err
- }
- return buffer[:n], rAddr, nil
- }
- func writeUDPWithRemoteAddr(conn *net.UDPConn, rAddr *net.UDPAddr, data []byte, opts ...connectionWriteOptions) error {
- for _, opt := range opts {
- err := opt(conn)
- if err != nil {
- return err
- }
- }
- _, err := conn.WriteToUDP(data, rAddr)
- if err != nil {
- return err
- }
- return nil
- }
- func writeUDP(conn *net.UDPConn, data []byte, opts ...connectionWriteOptions) error {
- for _, opt := range opts {
- err := opt(conn)
- if err != nil {
- return err
- }
- }
- _, err := conn.Write(data)
- if err != nil {
- return err
- }
- return nil
- }
- func readTCP(conn net.Conn, bufferSize int, readCallback func(data []byte) (bool, error), opts ...connectionReadOptions) error {
- for {
- buffer := make([]byte, bufferSize)
- for _, opt := range opts {
- err := opt(conn)
- if err != nil {
- return err
- }
- }
- n, err := conn.Read(buffer)
- if err != nil {
- return err
- }
- if n != 0 {
- readOver, err := readCallback(buffer[:n])
- if err != nil {
- return err
- }
- if readOver {
- return nil
- }
- }
- }
- }
- func writeTCP(conn net.Conn, data []byte, opts ...connectionWriteOptions) error {
- writeBytesCount := 0
- for {
- for _, opt := range opts {
- err := opt(conn)
- if err != nil {
- return err
- }
- }
- n, err := conn.Write(data[writeBytesCount:])
- if err != nil {
- return err
- }
- writeBytesCount += n
- if writeBytesCount < len(data) {
- continue
- }
- return nil
- }
- }
- func closeConnection(conn net.Conn) {
- err := conn.Close()
- if err != nil {
- fmt.Println("Close Connection Error:", err.Error())
- return
- }
- conn = nil
- }
|