+15
-6
server.go
+15
-6
server.go
···
211
211
delete(s.clients, client)
212
212
close(client.dataChan)
213
213
s.clientsMu.Unlock()
214
+
s.handlePub(client)
214
215
215
216
s.idmapsMu.Lock()
217
+
delete(s.clientToID, client)
216
218
for _, id := range client.myIDs { // remove myself from the idToClient map
217
219
delete(s.idToClient, id)
218
220
}
···
306
308
case *lrcpb.Event_Init:
307
309
s.handleInit(msg, client)
308
310
case *lrcpb.Event_Pub:
309
-
s.handlePub(msg, client)
311
+
s.handlePub(client)
310
312
case *lrcpb.Event_Insert:
311
313
s.handleInsert(msg, client)
312
314
case *lrcpb.Event_Delete:
···
328
330
}
329
331
330
332
func (s *Server) handleInit(msg *lrcpb.Event_Init, client *client) {
333
+
s.idmapsMu.Lock()
331
334
curID := s.clientToID[client]
332
335
if curID != nil {
336
+
s.idmapsMu.Unlock()
333
337
return
334
338
}
335
339
newID := s.lastID + 1
336
340
s.lastID = newID
337
-
s.idmapsMu.Lock()
338
341
s.clientToID[client] = &newID
339
342
s.idToClient[newID] = client
340
343
s.idmapsMu.Unlock()
···
391
394
}
392
395
}
393
396
394
-
func (s *Server) handlePub(msg *lrcpb.Event_Pub, client *client) {
397
+
func (s *Server) handlePub(client *client) {
398
+
s.idmapsMu.Lock()
395
399
curID := s.clientToID[client]
396
400
if curID == nil {
401
+
s.idmapsMu.Unlock()
397
402
return
398
403
}
399
-
s.idmapsMu.Lock()
400
404
s.clientToID[client] = nil
401
405
s.idmapsMu.Unlock()
402
-
msg.Pub.Id = curID
403
-
event := &lrcpb.Event{Msg: msg}
406
+
event := &lrcpb.Event{Msg: &lrcpb.Event_Pub{Pub: &lrcpb.Pub{Id: curID}}}
404
407
if s.pubChan != nil {
405
408
select {
406
409
case s.pubChan <- PubEvent{ID: *curID, Body: *client.post}:
···
415
418
}
416
419
417
420
func (s *Server) handleInsert(msg *lrcpb.Event_Insert, client *client) {
421
+
s.idmapsMu.Lock()
418
422
curID := s.clientToID[client]
423
+
s.idmapsMu.Unlock()
419
424
if curID == nil {
420
425
return
421
426
}
···
447
452
}
448
453
449
454
func (s *Server) handleDelete(msg *lrcpb.Event_Delete, client *client) {
455
+
s.idmapsMu.Lock()
450
456
curID := s.clientToID[client]
457
+
s.idmapsMu.Unlock()
451
458
if curID == nil {
452
459
return
453
460
}
···
495
502
}
496
503
497
504
func (s *Server) handleEditBatch(msg *lrcpb.Event_Editbatch, client *client) {
505
+
s.idmapsMu.Lock()
498
506
curID := s.clientToID[client]
507
+
s.idmapsMu.Unlock()
499
508
if curID == nil {
500
509
return
501
510
}