2013-11-25 38 views
0

爲什麼要殺死一個簡單的「ClientList.Remove(entry)」來自其他客戶端的所有連接?從列表中刪除客戶端殺死其他連接

我有一個非常簡單的Go TCP服務器,可以進行連接操作和登錄操作。 之後,如果創建一個客戶端並與TCP客戶端啓動一個GO例程。

newClient:= &客戶{ 「」, 「」,login.LoginToken,康涅狄格州} 去ClientReader(newClient) ClientList.PushBack(* newClient)

的圍棋程序讀取所有輸入數據。 並且當連接有超時或網絡更改(客戶端獲得新IP) 它從客戶端列表中刪除客戶端。

但是當它從列表中刪除客戶端....所有其他客戶端連接都已死亡? 在循環中找到合適的客戶端並將其刪除。

看看removeloop:

常規:

func ClientReader(client *Client) { 
    buffer := make([]byte, 2048) 
    for { 
     bytesRead, error := client.Conn.Read(buffer) 
    if error != nil { 
     Log(error) 
     break 
    } 

    var m Message 
    err := json.Unmarshal([]byte(buffer[0:bytesRead]), &m) 
    if err != nil { 
     Log(err) 
    } else { 

     switch m.Cmd { 
     case "Message": 

     case "Ping": 
      Log("Ping from: ", client.Name, " on ", client.Conn.RemoteAddr()) 
      client.Conn.SetDeadline(time.Now().Add(25 * time.Second)) 
      pong := []byte(`{"PONG":"..."}` + "\r\n") 
      client.Conn.Write(pong) 
      Log("PONG: " + time.Now().Format(time.RFC850)) 
      Log("User Online: " + strconv.Itoa(ClientList.Len())) 
      Log("Goroutines: " + strconv.Itoa(runtime.NumGoroutine())) 

     default: 
      Log("Not supported Command: ", m.Cmd) 
      clienterror := []byte(`{"Err":"Command not supported"}` + "\r\n") 
      client.Conn.Write(clienterror) 
     } 
     for i := 0; i < 2048; i++ { 
      buffer[i] = 0x00 
     } 
    } 

} 

RemoveLoop: 
    for entry := ClientList.Front(); entry != nil; entry = entry.Next() { 
     listclient := entry.Value.(Client) 
     if client.Conn.RemoteAddr() == listclient.Conn.RemoteAddr() { 
     ClientList.Remove(entry) 
     Log("## SEARCH: ", client.Name, client.Conn.RemoteAddr()) 
     Log("## FOUND: ", listclient.Name,listclient.Conn.RemoteAddr()) 
    Log("## REMOVED: ", entry.Value) 
    break RemoveLoop 
    } 
} 
Log("Exit Client Reader Routine ", client.Name, " on ", client.Conn.RemoteAddr()) 

}

回答

0

目前代碼:

  • 不是不是線程安全
  • 使用緩衝片相當比buffer object(其中我s更容易出錯)
  • 看起來像它使用(鏈接)list當(散列)map會更好。

請修復這些問題,並從那裏再次開始調試。

P.S.您可能還想將ClientReader更改爲客戶端的一種方法而不是函數?

在這個版本(http://play.golang.org/p/MDZlFSStiN)請看:

func ClientReader(client *Client) { 
buffer := new(bytes.Buffer) 
for { 
    _, err := buffer.ReadFrom(client.Conn) 
    if err != nil { 
     log.Println(err) 
     break 
    } 
    var m Message 
    if err = json.Unmarshal(buffer.Bytes(), &m); err != nil { 
     log.Println(err) 
    } else { 
     switch m.Cmd { 
     case "Message": 
     case "Ping": 
      log.Printf("Ping from: %s on %s\n", client.Name, client.Conn.RemoteAddr()) 
      client.Conn.SetDeadline(time.Now().Add(25 * time.Second)) 
      client.Conn.Write([]byte("{\"PONG\":\"...\"}\r\n")) 
      log.Printf("PONG: %s\n", time.Now().Format(time.RFC850)) 
      clientListLock.RLock() 
      log.Printf("User Online: %s\n", strconv.Itoa(len(clientList))) 
      clientListLock.RUnlock() 
      log.Printf("Goroutines: %s\n", strconv.Itoa(runtime.NumGoroutine())) 
     default: 
      log.Printf("Not supported Command: %s\n", m.Cmd) 
      client.Conn.Write([]byte("{\"Err\":\"Command not supported\"}\r\n")) 
     } 
     buffer.Truncate(0) 
    } 
} 
clientListLock.Lock() 
delete(clientList, client.Conn.RemoteAddr().String()) 
clientListLock.Unlock() 
log.Printf("Exit Client Reader Routine %s on %s\n", client.Name, client.Conn.RemoteAddr()) 
} 
+0

感謝您的快速回答:) – user3033143

0

感謝您的快速回答:) 我使你的建議(該線程客戶端列表),但我的問題仍然存在。

經過測試和重寫後,我在serverlog中看到一個非常瘋狂的東西。

ServerStart後它有4 Goroutines ....沒問題。 (Golang實習生?)

客戶端1已登錄...並且Goroutine 5已啓動....沒問題(第一個客戶端讀取器) 客戶端2已登錄...並且Goroutine 6已啓動.... OK(第二個客戶端閱讀器)

客戶端2是一個手機,我在手機和WLAN之間隨機切換。

服務器檢測到從電話的IP更改...是好的。 在ConnectionTimout ...正常後,使用新IP地址登錄的MobilePhone和服務器將終止舊Connection。

而不是從手機2-n坪後服務器增加Goroutines從6到7和手機失去了連接?

Goroutine不是客戶端讀取器(它記錄每個START和EXIT) 而客戶端讀取器是我的代碼中唯一的Goroutine。

GOLANG在做什麼,爲什麼會有Goroutine數字7?

+0

您需要分享更多的代碼,*可能*的問題點不在此代碼塊中。你共享的代碼不會產生任何goroutines,所以它很難幫助你。 – voidlogic

相關問題