mirror of
https://github.com/Mezeporta/Erupe.git
synced 2025-12-13 23:44:52 +01:00
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package channelserver
|
|
|
|
import (
|
|
"erupe-ce/common/byteframe"
|
|
"erupe-ce/network/mhfpacket"
|
|
|
|
"sync"
|
|
)
|
|
|
|
// Semaphore holds Semaphore-specific information
|
|
type Semaphore struct {
|
|
sync.RWMutex
|
|
|
|
// Semaphore ID string
|
|
name string
|
|
|
|
id uint32
|
|
|
|
// Map of session -> charID.
|
|
// These are clients that are registered to the Semaphore
|
|
clients map[*Session]uint32
|
|
|
|
// Max Players for Semaphore
|
|
maxPlayers uint16
|
|
}
|
|
|
|
// NewSemaphore creates a new Semaphore with intialized values
|
|
func NewSemaphore(s *Server, ID string, MaxPlayers uint16) *Semaphore {
|
|
sema := &Semaphore{
|
|
name: ID,
|
|
id: s.NextSemaphoreID(),
|
|
clients: make(map[*Session]uint32),
|
|
maxPlayers: MaxPlayers,
|
|
}
|
|
return sema
|
|
}
|
|
|
|
// BroadcastMHF queues a MHFPacket to be sent to all sessions in the Semaphore
|
|
func (s *Semaphore) BroadcastMHF(pkt mhfpacket.MHFPacket, ignoredSession *Session) {
|
|
// Broadcast the data.
|
|
for session := range s.clients {
|
|
if session == ignoredSession {
|
|
continue
|
|
}
|
|
|
|
// Make the header
|
|
bf := byteframe.NewByteFrame()
|
|
bf.WriteUint16(uint16(pkt.Opcode()))
|
|
|
|
// Build the packet onto the byteframe.
|
|
pkt.Build(bf, session.clientContext)
|
|
|
|
// Enqueue in a non-blocking way that drops the packet if the connections send buffer channel is full.
|
|
session.QueueSendNonBlocking(bf.Data())
|
|
}
|
|
}
|