sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/server/server.go

86 lines
1.6 KiB
Go
Raw Normal View History

package server
import (
"crypto/tls"
2017-12-14 21:30:07 +01:00
"net"
2017-12-14 21:30:07 +01:00
"github.com/genofire/yaja/database"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/acme/autocert"
)
type Server struct {
TLSConfig *tls.Config
2017-12-14 21:30:07 +01:00
TLSManager *autocert.Manager
ClientAddr []string
ServerAddr []string
Database *database.State
}
func (srv *Server) Start() {
2017-12-14 21:30:07 +01:00
for _, addr := range srv.ServerAddr {
socket, err := net.Listen("tcp", addr)
if err != nil {
log.Warn("create server socket: ", err.Error())
break
}
go srv.listenServer(socket)
}
2017-12-14 21:30:07 +01:00
for _, addr := range srv.ClientAddr {
socket, err := net.Listen("tcp", addr)
if err != nil {
log.Warn("create client socket: ", err.Error())
break
}
go srv.listenClient(socket)
}
}
func (srv *Server) listenServer(s2s net.Listener) {
for {
conn, err := s2s.Accept()
if err != nil {
log.Warn("accepting server connection: ", err.Error())
break
}
go srv.handleServer(conn)
}
}
func (srv *Server) listenClient(c2s net.Listener) {
for {
conn, err := c2s.Accept()
if err != nil {
log.Warn("accepting client connection: ", err.Error())
break
}
go srv.handleClient(conn)
}
}
func (srv *Server) handleServer(conn net.Conn) {
log.Info("new server connection:", conn.RemoteAddr())
}
func (srv *Server) handleClient(conn net.Conn) {
log.Info("new client connection:", conn.RemoteAddr())
client := NewClient(conn, srv)
state := ConnectionStartup()
for {
state, client = state.Process(client)
if state == nil {
client.log.Info("disconnect")
client.Close()
//s.DisconnectBus <- Disconnect{Jid: client.jid}
return
}
// run next state
}
}
func (srv *Server) Close() {
}