server.go 834 Bytes
package net

import (
	"fmt"
	"net"
	"pro2d/conf"
	"pro2d/utils"
	"sync"
)

type Server struct {
	SConf      *conf.SConf
	Clients *sync.Map

}

func NewServer(sConf *conf.SConf) *Server {
	return &Server{
		SConf:      sConf,
		Clients:    new(sync.Map),
	}
}

func (s *Server) OnRecv(msg *MsgPkg) {
	utils.Sugar.Debugf("cmd: %d, data: %s", msg.Head.Cmd, msg.Body)
}

func (s *Server) OnClose(conn *Connection) {
	s.Clients.Delete(conn.Id)
}

func (s *Server)Start() error {
	port := fmt.Sprintf(":%d", s.SConf.Port)
	l, err := net.Listen("tcp", port)
	if err != nil {
		return err
	}

	utils.Sugar.Debugf("listen on %s\n", port)
	id := 0
	for {
		conn, err := l.Accept()
		if err != nil {
			return err
		}

		id++
		client := NewConn(id, conn, s)
		s.Clients.Store(id, client)
		go client.Start()
	}
}

func (s *Server)Stop()  {
}