package main
import (
“net”
“sync”
“fmt”
“time”
“errors”
“context”
“os”
“os/signal”
“syscall”
“encoding/csv”
)
type TcpServer struct{
url string
listener net.Listener
connections map[uint64]*TcpConnection
connectionsLock *sync.RWMutex
running bool
runningLock *sync.Mutex
exit bool
Stop chan bool
remoteUrl string
}
func NewTcpServer() *TcpServer {
s := new(TcpServer)
s.connections = make(map[uint64]*TcpConnection)
s.connectionsLock = new(sync.RWMutex)
s.runningLock = new(sync.Mutex)
s.Stop = make(chan bool)
s.exit = false
return s
}
func (s *TcpServer) SetListenUrl(url string){
s.url = url
}
func (s *TcpServer) SetRemoteUrl(url string){
s.remoteUrl = url
}
func (s *TcpServer) Start() error {
s.runningLock.Lock()
alreadyRunning := s.running
if alreadyRunning {
s.runningLock.Unlock()
return errors.New(“netServer already started”)
}
ln, err := net.Listen(“tcp”, s.url)
//nln:= netutil.LimitListener(ln,10000)
fmt.Println(“netServer listen:”,s.url)
if err != nil {
return err
}
s.listener = ln
s.running = true
s.runningLock.Unlock()
go s.listen()
return nil
}
func (s TcpServer) listen() {
ticker := time.NewTicker(time.Second * 1)
stopTickGoroutine

最低0.47元/天 解锁文章
417

被折叠的 条评论
为什么被折叠?



