Skip to content

Commit

Permalink
Remove useless function and exit on Ctrl+D = EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcPer committed Jun 19, 2021
1 parent 1242021 commit f13e3d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func readInput(sendCh chan []byte) {
msg := fmt.Sprintf("%v\n", s.Text())
sendCh <- []byte(msg)
}
if s.Err() == nil {
os.Exit(0)
}
}

func (c *Client) readIncoming(rcvCh chan packet, conn net.Conn) {
Expand Down
24 changes: 10 additions & 14 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (
type client struct {
id string
conn net.Conn
sndChannel chan []byte
rcvChannel chan packet
}

Expand All @@ -40,7 +39,7 @@ type packet struct {
func New(username string) Server {
cs := make([]*client, 0, 10)
ch := make(chan packet, 50)
prompt := fmt.Sprintf("%s%s%s>%s ", bold, lGreen, username, string(reset))
prompt := fmt.Sprintf("%s%s%s>%s ", bold, lGreen, username, reset)
promptDelete := strings.Repeat("\b", len(prompt))
return Server{username, cs, ch, prompt, promptDelete}
}
Expand All @@ -56,27 +55,19 @@ func (s *Server) Start(url string) {
go s.readInput()
// go s.pollConns()

fmt.Print(s.prompt)
for {
conn, err := ln.Accept()
if err != nil {
fmt.Printf("Failed to accept connection: %v", err)
} else {
sndCh := make(chan []byte, 50)
c := client{conn: conn, sndChannel: sndCh, rcvChannel: s.rcvChannel}
c := client{conn: conn, rcvChannel: s.rcvChannel}
s.clients = append(s.clients, &c)
go handleConn(&c)
go c.readIncoming()
}
}
}

func handleConn(c *client) {
go c.readIncoming()

for msg := range c.sndChannel {
c.conn.Write(msg)
}
}

func (s *Server) pollConns() {
for {
for _, c := range s.clients {
Expand Down Expand Up @@ -117,6 +108,9 @@ func (s *Server) readInput() {
s.rcvChannel <- packet{s.id, sca.Text()}
fmt.Print(s.prompt)
}
if sca.Err() == nil {
os.Exit(0)
}
}

func (c *client) readIncoming() {
Expand All @@ -135,7 +129,7 @@ func (s *Server) startBroadcast() {
for pk := range s.rcvChannel {
if pk.id != s.id {
fmt.Print(s.promptDelete)
msg := fmt.Sprintf("%s%s%s>%s %s", string(bold), string(lYellow), pk.id, string(reset), pk.msg)
msg := fmt.Sprintf("%s%s%s>%s %s", bold, lYellow, pk.id, reset, pk.msg)
fmt.Println(msg)
fmt.Print(s.prompt)
}
Expand Down Expand Up @@ -167,6 +161,8 @@ func processCmd(c *client, msg string) {
}
return
}
case "info":
return
default:
goto FAIL_CMD
}
Expand Down

0 comments on commit f13e3d2

Please sign in to comment.