Пример #1
0
func ListenToEvents() {
	clients := make([]*events.SomeSocket, 0, 1024)
	clientsCounter := 0
	for {
		select {
		case newVoteEvent := <-*events.GetNewVoteChan():
			for _, client := range clients {
				client.NewVoteEvent <- &newVoteEvent
			}
		case voteUpdateEvent := <-*events.GetVoteUpdateChan():
			for _, client := range clients {
				client.VoteUpdEvent <- &voteUpdateEvent
			}
		case newSocketEvent := <-*events.GetNewSocketsChan():
			clientsCounter++
			newSocketEvent.Socket.Id = strconv.Itoa(clientsCounter)
			clients = append(clients, newSocketEvent.Socket)
		case socketClosedEvent := <-*events.GetClosedSocketsChan():
			id := socketClosedEvent.Socket.Id
			index := 0
			for i, socket := range clients {
				if socket.Id == id {
					index = i
				}
			}

			clients = append(clients[:index], clients[index+1:]...)
		}
	}
}
Пример #2
0
func HandleNewConnection(c net.Conn) {
	s := TcpSocket{conection: &c}

	// closing in ListenToEvents()
	s.SomeSocket.NewVoteEvent = make(chan *events.NewVoteEvent)
	s.SomeSocket.VoteUpdEvent = make(chan *events.VoteUpdateEvent)
	s.SomeSocket.CloseEvent = make(chan *events.SocketClosedEvent)

	go s.ListenToEvents()

	*events.GetNewSocketsChan() <- events.NewSocketEvent{&s.SomeSocket}

	defer ConnectionClosed(&s)

	// read
	for {
		header := make([]byte, 6)
		headerLen, err := c.Read(header)
		if err != nil || headerLen != 6 {
			break
		}

		packet := InitPacketWithHeaderData(header)
		if packet.size > 0 {
			content := make([]byte, packet.size)
			contLen, err := c.Read(content)
			if err != nil || contLen != int(packet.size) {
				break
			}
			packet.content = content
		}

		s.ProccesPacket(&packet)
	}
}
Пример #3
0
func ListenAndServer(host string, ns *notification.Sender) {
	notificationSender = ns
	l, err := net.Listen("tcp", host)
	if err != nil {
		log.Println("TcpSocket Error listening:", err.Error())
		os.Exit(1)
	}

	println("Starting tcp server...")

	defer l.Close()

	defer close(*events.GetNewVoteChan())
	defer close(*events.GetVoteUpdateChan())
	defer close(*events.GetNewSocketsChan())
	defer close(*events.GetClosedSocketsChan())

	go ListenToEvents()

	for {
		conn, err := l.Accept()
		if err != nil {
			log.Println("TcpSocket Error accepting: ", err.Error())
			os.Exit(1)
		}

		go HandleNewConnection(conn)
	}
}
Пример #4
0
func ListenAndServer(host string, ns *notification.Sender, dbconn string) {
	notificationSender = ns
	dbConnectionAddress = dbconn

	l, err := net.Listen("tcp", host)
	if err != nil {
		log.Error.Printf("TcpSocket Error listening: %s\n", err.Error())
		os.Exit(1)
	}

	defer l.Close()

	defer close(*events.GetNewVoteChan())
	defer close(*events.GetVoteUpdateChan())
	defer close(*events.GetNewSocketsChan())
	defer close(*events.GetClosedSocketsChan())

	go ListenToEvents()

	for {
		conn, err := l.Accept()
		if err != nil {
			log.Error.Printf("TcpSocket Error accepting: %s\n", err.Error())
			os.Exit(1)
		}

		go handleNewConnection(conn)
	}
}
Пример #5
0
func handleNewConnection(c net.Conn) {
	funcPrefix := "Handling new connection"
	log.Debug.Printf("%s: start\n", funcPrefix)
	defer log.Debug.Printf("%s: end\n", funcPrefix)
	s := TcpSocket{connection: &c}

	// closing in ListenToEvents()
	s.SomeSocket.NewVoteEvent = make(chan *events.NewVoteEvent)
	s.SomeSocket.VoteUpdEvent = make(chan *events.VoteUpdateEvent)
	s.SomeSocket.CloseEvent = make(chan *events.SocketClosedEvent)

	go s.ListenToEvents()

	log.Debug.Printf("%s: sending new socket connection event...\n", funcPrefix)
	*events.GetNewSocketsChan() <- events.NewSocketEvent{&s.SomeSocket}

	defer connectionClosed(&s)
	defer log.Debug.Printf("%s: closing connection...\n", funcPrefix)

	// read
	log.Debug.Printf("%s: start reading packets from socket connection\n", funcPrefix)
	for {
		header := make([]byte, 6)
		headerLen, err := c.Read(header)
		if err != nil || headerLen != 6 {
			break
		}

		packet := InitPacketWithHeaderData(header)
		if packet.size > 0 {
			content := make([]byte, packet.size)
			contLen, err := c.Read(content)
			if err != nil || contLen != int(packet.size) {
				break
			}
			packet.content = content
		}

		s.ProcessPacket(&packet)
	}
	log.Debug.Printf("%s: finish reading packets from socket connection\n", funcPrefix)
}
func ListenToEvents() {
	funcPrefix := "Processing socket managing events"
	log.Debug.Printf("%s: start\n", funcPrefix)
	defer log.Debug.Printf("%s: end\n", funcPrefix)

	clients := make([]*events.SomeSocket, 0, 1024)
	clientsCounter := 0
	for {
		select {
		case newVoteEvent := <-*events.GetNewVoteChan():
			log.Debug.Printf("%s: handling vote create event\n", funcPrefix)
			for _, client := range clients {
				client.NewVoteEvent <- &newVoteEvent
			}
		case voteUpdateEvent := <-*events.GetVoteUpdateChan():
			log.Debug.Printf("%s: handling vote update event\n", funcPrefix)
			for _, client := range clients {
				client.VoteUpdEvent <- &voteUpdateEvent
			}
		case newSocketEvent := <-*events.GetNewSocketsChan():
			log.Debug.Printf("%s: handling socket connection create event\n", funcPrefix)
			clientsCounter++
			newSocketEvent.Socket.Id = strconv.Itoa(clientsCounter)
			clients = append(clients, newSocketEvent.Socket)
		case socketClosedEvent := <-*events.GetClosedSocketsChan():
			log.Debug.Printf("%s: handling socket connection close event\n", funcPrefix)
			id := socketClosedEvent.Socket.Id
			index := 0
			for i, socket := range clients {
				if socket.Id == id {
					index = i
				}
			}
			clients = append(clients[:index], clients[index+1:]...)
		}
	}
}