Skip to content

Commit 211c65a

Browse files
authored
Chore: Minor TCP refactorings. (#1033)
* chore: unused params. * chore: redundant if-statement. * chore: redundant map assignment. * chore: removed else - break above. * chore: infof for consistency.
1 parent 20f9cc0 commit 211c65a

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

src/tcp/tcp.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,15 @@ func (s *server) run() (err error) {
115115
ip = tcpIP.IP
116116
}
117117
addr = net.JoinHostPort(ip.String(), s.port)
118-
if s.host != "" {
119-
if ip.To4() != nil {
120-
network = "tcp4"
121-
} else {
122-
network = "tcp6"
123-
}
118+
if ip.To4() != nil {
119+
network = "tcp4"
120+
} else {
121+
network = "tcp6"
124122
}
123+
125124
}
126125
addr = strings.Replace(addr, "127.0.0.1", "0.0.0.0", 1)
127-
log.Info("starting TCP server on " + addr)
126+
log.Infof("starting TCP server on %s", addr)
128127
server, err := net.Listen(network, addr)
129128
if err != nil {
130129
return fmt.Errorf("error listening on %s: %w", addr, err)
@@ -137,9 +136,9 @@ func (s *server) run() (err error) {
137136
return fmt.Errorf("problem accepting connection: %w", err)
138137
}
139138
log.Debugf("client %s connected", connection.RemoteAddr().String())
140-
go func(port string, connection net.Conn) {
139+
go func(connection net.Conn) {
141140
c := comm.New(connection)
142-
room, errCommunication := s.clientCommunication(port, c)
141+
room, errCommunication := s.clientCommunication(c)
143142
log.Debugf("room: %+v", room)
144143
log.Debugf("err: %+v", errCommunication)
145144
if errCommunication != nil {
@@ -157,23 +156,23 @@ func (s *server) run() (err error) {
157156
log.Debugf("checking connection of room %s for %+v", room, c)
158157
deleteIt := false
159158
s.rooms.Lock()
160-
if _, ok := s.rooms.rooms[room]; !ok {
159+
roomData, ok := s.rooms.rooms[room]
160+
if !ok {
161161
log.Debug("room is gone")
162162
s.rooms.Unlock()
163163
return
164164
}
165-
log.Debugf("room: %+v", s.rooms.rooms[room])
166-
if s.rooms.rooms[room].first != nil && s.rooms.rooms[room].second != nil {
165+
log.Debugf("room: %+v", roomData)
166+
if roomData.first != nil && roomData.second != nil {
167167
log.Debug("rooms ready")
168168
s.rooms.Unlock()
169169
break
170-
} else {
171-
if s.rooms.rooms[room].first != nil {
172-
errSend := s.rooms.rooms[room].first.Send([]byte{1})
173-
if errSend != nil {
174-
log.Debug(errSend)
175-
deleteIt = true
176-
}
170+
}
171+
if s.rooms.rooms[room].first != nil {
172+
errSend := s.rooms.rooms[room].first.Send([]byte{1})
173+
if errSend != nil {
174+
log.Debug(errSend)
175+
deleteIt = true
177176
}
178177
}
179178
s.rooms.Unlock()
@@ -183,7 +182,7 @@ func (s *server) run() (err error) {
183182
}
184183
time.Sleep(1 * time.Second)
185184
}
186-
}(s.port, connection)
185+
}(connection)
187186
}
188187
}
189188

@@ -222,7 +221,7 @@ func (s *server) stopRoomDeletion() {
222221

223222
var weakKey = []byte{1, 2, 3}
224223

225-
func (s *server) clientCommunication(port string, c *comm.Comm) (room string, err error) {
224+
func (s *server) clientCommunication(c *comm.Comm) (room string, err error) {
226225
// establish secure password with PAKE for communication with relay
227226
B, err := pake.InitCurve(weakKey, 1, "siec")
228227
if err != nil {
@@ -386,17 +385,17 @@ func (s *server) clientCommunication(port string, c *comm.Comm) (room string, er
386385
func (s *server) deleteRoom(room string) {
387386
s.rooms.Lock()
388387
defer s.rooms.Unlock()
389-
if _, ok := s.rooms.rooms[room]; !ok {
388+
roomData, ok := s.rooms.rooms[room]
389+
if !ok {
390390
return
391391
}
392392
log.Debugf("deleting room: %s", room)
393-
if s.rooms.rooms[room].first != nil {
394-
s.rooms.rooms[room].first.Close()
393+
if roomData.first != nil {
394+
roomData.first.Close()
395395
}
396-
if s.rooms.rooms[room].second != nil {
397-
s.rooms.rooms[room].second.Close()
396+
if roomData.second != nil {
397+
roomData.second.Close()
398398
}
399-
s.rooms.rooms[room] = roomInfo{first: nil, second: nil}
400399
delete(s.rooms.rooms, room)
401400
}
402401

0 commit comments

Comments
 (0)