forked from coolspeed/century
-
Notifications
You must be signed in to change notification settings - Fork 0
/
century.go
227 lines (192 loc) · 4.84 KB
/
century.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"fmt"
"bufio"
"net"
"os"
"time"
"sync"
"io"
)
type Session struct {
sid int
chatRoom *ChatRoom
connection net.Conn
incoming chan string
outgoing chan string
reader *bufio.Reader
writer *bufio.Writer
killRoomConnGoroutine chan bool
killSocketReaderGoroutine chan bool
killSocketWriterGoroutine chan bool
sessionMutex sync.Mutex
}
func NewSession(sid int, chatRoom *ChatRoom, connection net.Conn) *Session {
writer := bufio.NewWriter(connection)
reader := bufio.NewReader(connection)
session := &Session{
sid: sid,
chatRoom: chatRoom,
connection: connection,
incoming: make(chan string),
outgoing: make(chan string),
reader: reader,
writer: writer,
killRoomConnGoroutine: make(chan bool),
killSocketReaderGoroutine: make(chan bool),
killSocketWriterGoroutine: make(chan bool),
// sessionMutex:
}
fmt.Println("A new session created. sid=", sid)
return session
}
func (session *Session) Read() {
for {
select {
case <-session.killSocketReaderGoroutine:
return
default:
line, err := session.reader.ReadString('\n')
if err != nil {
// EOF? yes: disconnected
// Judge. if true: LeaveAndDelete -- pop session from chatRoom, and delete session
if err == io.EOF {
fmt.Println("Client disconnected. Destroy session, sid=", session.sid)
session.LeaveAndDelete()
}
// else:
fmt.Println("bufio.reader.ReadString failed.")
fmt.Println(err)
time.Sleep(100 * time.Millisecond)
continue
}
session.incoming <- line
}
}
}
func (session *Session) Write() {
for {
select {
case <-session.killSocketWriterGoroutine:
return
case data := <-session.outgoing:
session.writer.WriteString(data)
session.writer.Flush()
}
}
}
func (session *Session) Listen() {
go session.Read()
go session.Write()
}
func (session *Session) LeaveAndDelete() {
// leave
chatRoom := *session.chatRoom
sid := session.sid
chatRoom.roomMutex.Lock()
defer chatRoom.roomMutex.Unlock()
delete(chatRoom.sessions, sid)
// delete
session.sessionMutex.Lock()
defer session.sessionMutex.Unlock()
// release resources
// resouce: socket reader goroutine & socket writer goroutine
session.killSocketReaderGoroutine <- true
session.killSocketWriterGoroutine <- true
// resource: reader & writer
session.reader = nil
session.writer = nil
// resource: socket conection
session.connection.Close()
session.connection = nil
// resource: RoomConnGoroutine
session.killRoomConnGoroutine <- true
// resource: connection to chatRoom
// "many in, one out" 형태의 'chatRoom'의(!) channel 이기에 지워야할 채널(RoomConn)이 사실은 존재하지 않는다. (위에서) 해당 goroutine 지워줬으니 끝.
}
type ChatRoom struct {
sessions map[int]*Session
lastSid int
entrance chan net.Conn
incoming chan string
outgoing chan string
roomMutex sync.Mutex
}
func NewChatRoom() *ChatRoom {
chatRoom := &ChatRoom{
sessions: make(map[int]*Session),
lastSid: -1,
entrance: make(chan net.Conn),
incoming: make(chan string),
outgoing: make(chan string),
// roomMutex:
}
fmt.Println("A new ChatRoom created.")
return chatRoom
}
func (chatRoom *ChatRoom) Broadcast(data string) {
for _, session := range chatRoom.sessions {
session.outgoing <- data
}
}
func (chatRoom *ChatRoom) Join(connection net.Conn) {
chatRoom.roomMutex.Lock()
defer chatRoom.roomMutex.Unlock()
newSessionId := chatRoom.lastSid + 1
chatRoom.lastSid = newSessionId
session := NewSession(newSessionId, chatRoom, connection)
session.Listen()
fmt.Println("session started listening.")
_, keyExist := chatRoom.sessions[newSessionId]
if ! keyExist {
chatRoom.sessions[newSessionId] = session
}
go func() { // goroutine for roomConn writer
for {
select {
case <-session.killRoomConnGoroutine:
return
case data := <-session.incoming:
chatRoom.incoming <- data
}
}
}()
}
func (chatRoom *ChatRoom) Listen() {
go func() { // This goroutine runs forever, and does not need a channel to kill it.
for {
select {
case data := <-chatRoom.incoming:
//fmt.Println("RECEIVED: " + data)
chatRoom.Broadcast(data)
case conn := <-chatRoom.entrance:
chatRoom.Join(conn)
}
}
}()
}
func main() {
fmt.Println("Server started.")
port := "6666"
listener, err_listen := net.Listen("tcp", ":" + port)
if err_listen != nil {
fmt.Println("Server listening failed. Exit.")
os.Exit(1)
}
fmt.Println("Server started to listen on port " + port)
chatRoom := NewChatRoom()
// listen
chatRoom.Listen()
fmt.Println("chatRoom started to listen.")
for {
conn, err_ac := listener.Accept()
if err_ac != nil {
fmt.Println("Connection accepting failed.")
conn.Close()
time.Sleep(100 * time.Millisecond)
continue
}
fmt.Println("A new connection accepted.")
chatRoom.entrance <- conn // ChatRoom.entrance: channel of connection
}
}