-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (66 loc) · 1.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"io"
"log"
"net"
"os"
)
func accept_any(l net.Listener) (net.Conn, error) {
return l.Accept()
}
func (server *Server) session_user(user User) {
buff := make([]byte, 1024)
for {
n, err := user.conn.Read(buff)
if err != nil {
if err != io.EOF {
log.Printf("%s err: %s", user.name, err)
}
continue
}
log.Printf("%s sent %s", user.name, buff[:n])
message := Message{
content: fmt.Sprintf("\n[%s]: %s", user.name, buff[:n]),
user: user,
}
server.broadcast(message)
}
}
func start_server(server *Server) {
log.Printf("Starting %s", server.name)
for {
conn, _ := server.listener.Accept()
username := server.get_random_username()
log.Printf("%s connected to %s", username, server.name)
user := User{
name: username,
conn: conn,
}
server.register_user(user)
go server.session_user(user)
}
}
func main() {
args := os.Args
server_name := "Olympus"
if len(args) > 1 {
server_name = args[1]
}
const (
IP = "127.0.0.1"
PORT = "1337"
)
server := NewServer(server_name, IP, PORT)
_ = server.open()
_ = make(chan struct{})
go start_server(&server)
for {
var console_buff string
fmt.Scanln(&console_buff)
if console_buff == "FIN" {
server.teardown()
break
}
}
}