|
|
@ -1,19 +1,17 @@
|
|
|
|
package main
|
|
|
|
package cm
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var manager = newConnectionManager()
|
|
|
|
type Manager struct {
|
|
|
|
|
|
|
|
|
|
|
|
type connectionManager struct {
|
|
|
|
|
|
|
|
active []net.Conn
|
|
|
|
active []net.Conn
|
|
|
|
connect chan net.Conn
|
|
|
|
connect chan net.Conn
|
|
|
|
disconnect chan net.Conn
|
|
|
|
disconnect chan net.Conn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func newConnectionManager() *connectionManager {
|
|
|
|
func New() *Manager {
|
|
|
|
m := &connectionManager{
|
|
|
|
m := &Manager{
|
|
|
|
active: make([]net.Conn, 0, 10),
|
|
|
|
active: make([]net.Conn, 0, 10),
|
|
|
|
connect: make(chan net.Conn),
|
|
|
|
connect: make(chan net.Conn),
|
|
|
|
disconnect: make(chan net.Conn),
|
|
|
|
disconnect: make(chan net.Conn),
|
|
|
@ -22,7 +20,7 @@ func newConnectionManager() *connectionManager {
|
|
|
|
return m
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *connectionManager) run() {
|
|
|
|
func (m *Manager) run() {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case conn := <-m.connect:
|
|
|
|
case conn := <-m.connect:
|
|
|
@ -33,15 +31,15 @@ func (m *connectionManager) run() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *connectionManager) Add(conn net.Conn) {
|
|
|
|
func (m *Manager) Add(conn net.Conn) {
|
|
|
|
m.connect <- conn
|
|
|
|
m.connect <- conn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *connectionManager) Remove(conn net.Conn) {
|
|
|
|
func (m *Manager) Remove(conn net.Conn) {
|
|
|
|
m.disconnect <- conn
|
|
|
|
m.disconnect <- conn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *connectionManager) removeConnection(conn net.Conn) {
|
|
|
|
func (m *Manager) removeConnection(conn net.Conn) {
|
|
|
|
for i, other := range m.active {
|
|
|
|
for i, other := range m.active {
|
|
|
|
if conn.RemoteAddr() == other.RemoteAddr() {
|
|
|
|
if conn.RemoteAddr() == other.RemoteAddr() {
|
|
|
|
m.active = append(m.active[:i], m.active[i+1:]...)
|
|
|
|
m.active = append(m.active[:i], m.active[i+1:]...)
|