-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_chat_server.go
More file actions
148 lines (128 loc) · 3.55 KB
/
Copy pathsimple_chat_server.go
File metadata and controls
148 lines (128 loc) · 3.55 KB
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
package main
import(
"fmt"
"net"
"container/list"
"bytes"
)
type Client struct{
Name string
Incoming chan string
Outgoing chan string
Conn net.Conn
Quit chan bool
ClientList *list.List
}
func (c *Client) Read(buffer []byte) bool{
bytesRead, err := c.Conn.Read(buffer)
if err != nil{ //Network error problems
c.Close()
Log(err)
return false
}
Log("Read, ", bytesRead, " bytes")
return true
}
func (c *Client) Close(){
c.Quit <- true
c.Conn.Close()
c.RemoveMe()
}
func (c *Client) Equal(other *Client) bool{
if bytes.Equal([]byte(c.Name), []byte(other.Name)){
if c.Conn == other.Conn{
return true
}
}
return false
}
func (c *Client) RemoveMe(){ //Returns void?
for entry := c.ClientList.Front(); entry != nil; entry = entry.Next() {
client := entry.Value.(Client)
if c.Equal(&client){
Log("Remove me: ", c.Name)
c.ClientList.Remove(entry)
}
}
}
func Log(v ...interface{}){
fmt.Println(v...)
}
func IOHandler(Incoming <- chan string, clientList *list.List){
for{
Log("IOHandler: Waiting for input")
input := <-Incoming
Log("IOHandling: ", input);
for e := clientList.Front(); e != nil; e = e.Next(){
client := e.Value.(Client)
client.Incoming <-input
}
}
}
func ClientReader(client *Client){
buffer := make([]byte, 2048)
for client.Read(buffer){
if bytes.Equal(buffer, []byte("/quit")){
client.Close()
break
}
Log("ClientReader received ", client.Name, "> ", string(buffer))
send := client.Name + "> " + string(buffer)
client.Outgoing <- send
for i := 0; i < 2048; i++ {
buffer[i] = 0x00
}
}
client.Outgoing <- client.Name + " has left chat"
Log("ClientReader stopped for ", client.Name)
}
func ClientSender(client *Client){
for{
select{
case buffer := <-client.Incoming:
Log("Case buffer is sending: ", string(buffer), " to ", client.Name)
count := 0
for i := 0; i < len(buffer); i++{
if buffer[i] == 0x00{
break
}
count++
}
Log("Send size: ", count)
client.Conn.Write([]byte(buffer)[0: count])
case <-client.Quit:
Log("Quitting client: ", client.Name)
client.Conn.Close()
break
}
}
}
func ClientHandler(conn net.Conn, ch chan string, clientList *list.List){
buffer := make([]byte, 1024)
bytesRead, _ := conn.Read(buffer)
name := string(buffer[0: bytesRead])
newClient := &Client{name, make(chan string), ch, conn, make(chan bool), clientList}
go ClientSender(newClient)
go ClientReader(newClient)
clientList.PushBack(*newClient)
ch <-string(name + " has just joined the chat")
}
func main(){
Log("hello, server!")
clientList := list.New()
in := make(chan string)
go IOHandler(in, clientList)
port := ":9988"
tcpAddr, _ := net.ResolveTCPAddr("tcp", port)
netListen, _ := net.Listen(tcpAddr.Network(), tcpAddr.String())
defer netListen.Close()
for{
Log("Waiting for Client")
connection, err := netListen.Accept()
if err != nil{
Log("Client error: ", err)
} else{
go ClientHandler(connection, in, clientList)
}
}
}