2013-11-25 32 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

相关问题