在 go routine 中追加 slice
Append slice in go routine
我正在自学围棋。我写了一个简单的客户端/服务器应用程序,它有一些加密和非常简单的数据包结构。
我有一个 go 例程用于监听然后将数据发送到每个连接的客户端。在我向每个客户端发送数据的函数中,我将一条消息附加到 header 但它做了一些奇怪的行为。
func ClientSender(client *Client) {
for {
input := <-client.Outgoing //TODO add cleanup quit bool
for _, clientz := range RoomList[client.Room].Clients { //TODO rename to client connections = ClientList
temp := input
dbgMsg.Printf("RAW SENDER: % x", input)
dbgMsg.Printf("INPUT END1: % x", input)
dbgMsg.Printf("AES KEY % x\n", clientz.AES_Key)
dbgMsg.Printf("INPUT END2: % x", input)
dbgMsg.Printf("pre ecnryp: % x\n", input[30:])
dbgMsg.Printf("INPUT END3: % x", input)
encPayload := input[30:]
dbgMsg.Printf("INPUT END4: % x", input)
header := input[:30]
dbgMsg.Printf("INPUT END5: % x", input)
e,_ := barrenoid.Encrypt(clientz.AES_Key, encPayload)
dbgMsg.Printf("INPUT END6: % x", input)
dbgMsg.Printf("header: % x\n", input[:30])
dbgMsg.Printf("payload: % x\n", input[30:])
dbgMsg.Printf("encrypt: % x\n", e)
dbgMsg.Printf("TEMP: % x\n", temp)
asdf := append(header, e...)
dbgMsg.Printf("SENDING: % x", asdf)
//_, err := clientz.Conn.Write(payload)
//chkError(err)
input = temp
dbgMsg.Printf("INPUT END7: % x", input)
}
}
}
"input" 的值发生了变化,我不知道为什么。这是上面代码的输出:
INFO: 2016/02/22 10:47:38 RAW SENDER: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END1: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 AES KEY 06 89 c9 d7 ad ec 4a d0 33 bf fa ab 6e 05 cd 51 87 8b f0 ad 60 a8 36 47 ca 8f 7a f8 b8 6f 1c ce
INFO: 2016/02/22 10:47:38 INPUT END2: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 pre ecnryp: 0a
INFO: 2016/02/22 10:47:38 INPUT END3: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END4: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END5: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END6: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 header: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00
INFO: 2016/02/22 10:47:38 payload: 0a
INFO: 2016/02/22 10:47:38 encrypt: ***c8*** 7e ff f9 f5 c3 ce 1e 1d 44 91 b7 fb 09 5d e0 7e
INFO: 2016/02/22 10:47:38 TEMP: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 SENDING: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 c8 7e ff f9 f5 c3 ce 1e 1d 44 91 b7 fb 09 5d e0 7e
INFO: 2016/02/22 10:47:38 INPUT END7: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 ***c8***
我不明白为什么包含 "INPUT END7" 的行不等于 "input" 值。
最后一个字节总是等于"encrypted"输出中的第一个字节...
这是将切片发送到通道的代码:
func ClientReader(client *Client) {
//Main Read loop
for {
bytesRead, buffer := client.Read()
if bytesRead < HEADERSIZE {
//client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(""), []byte("Minimum header not recieved."))
client.Close()
break // Connection to host is broken
}
//dbgMsg.Printf("RAW RECIEVED % x", buffer)
cmdBit, encryptionByte, ts, payload := protocolParser(buffer)
dbgMsg.Printf("CMDBIT: % x, ENCBIT: % x, TS: % d, PAYLOAD: % x", cmdBit, encryptionByte, ts, payload)
if encryptionByte == 0xAE {
payload, _ = barrenoid.Decrypt(client.AES_Key, payload)
dbgMsg.Printf("Decrypted payload % x\n", payload)
} else if encryptionByte == 0x00 {
// no need to decrypt
} else {
//bad packet reject
}
if cmdBit == 0x0D{
//payload, _ = barrenoid.Encrypt(client.AES_Key, payload)
client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), payload)
} else if cmdBit == 0x1C {
client.Name = string(payload)
} else {
//bad packet reject
//client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), []byte("Unknown command bit."))
}
}
切片 temp
和 input
共享同一个支持数组。通过一个切片的修改通过另一个切片可见。包含 "INPUT END7" 的行与包含 "INPUT END1" 的行不同,因为在这一行修改了切片的支持数组:
asdf := append(header, e...)
您可以使用这行代码复制支持数组:
temp := append([]byte(nil), input...)
我正在自学围棋。我写了一个简单的客户端/服务器应用程序,它有一些加密和非常简单的数据包结构。
我有一个 go 例程用于监听然后将数据发送到每个连接的客户端。在我向每个客户端发送数据的函数中,我将一条消息附加到 header 但它做了一些奇怪的行为。
func ClientSender(client *Client) {
for {
input := <-client.Outgoing //TODO add cleanup quit bool
for _, clientz := range RoomList[client.Room].Clients { //TODO rename to client connections = ClientList
temp := input
dbgMsg.Printf("RAW SENDER: % x", input)
dbgMsg.Printf("INPUT END1: % x", input)
dbgMsg.Printf("AES KEY % x\n", clientz.AES_Key)
dbgMsg.Printf("INPUT END2: % x", input)
dbgMsg.Printf("pre ecnryp: % x\n", input[30:])
dbgMsg.Printf("INPUT END3: % x", input)
encPayload := input[30:]
dbgMsg.Printf("INPUT END4: % x", input)
header := input[:30]
dbgMsg.Printf("INPUT END5: % x", input)
e,_ := barrenoid.Encrypt(clientz.AES_Key, encPayload)
dbgMsg.Printf("INPUT END6: % x", input)
dbgMsg.Printf("header: % x\n", input[:30])
dbgMsg.Printf("payload: % x\n", input[30:])
dbgMsg.Printf("encrypt: % x\n", e)
dbgMsg.Printf("TEMP: % x\n", temp)
asdf := append(header, e...)
dbgMsg.Printf("SENDING: % x", asdf)
//_, err := clientz.Conn.Write(payload)
//chkError(err)
input = temp
dbgMsg.Printf("INPUT END7: % x", input)
}
}
}
"input" 的值发生了变化,我不知道为什么。这是上面代码的输出:
INFO: 2016/02/22 10:47:38 RAW SENDER: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END1: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 AES KEY 06 89 c9 d7 ad ec 4a d0 33 bf fa ab 6e 05 cd 51 87 8b f0 ad 60 a8 36 47 ca 8f 7a f8 b8 6f 1c ce
INFO: 2016/02/22 10:47:38 INPUT END2: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 pre ecnryp: 0a
INFO: 2016/02/22 10:47:38 INPUT END3: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END4: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END5: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END6: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 header: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00
INFO: 2016/02/22 10:47:38 payload: 0a
INFO: 2016/02/22 10:47:38 encrypt: ***c8*** 7e ff f9 f5 c3 ce 1e 1d 44 91 b7 fb 09 5d e0 7e
INFO: 2016/02/22 10:47:38 TEMP: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 SENDING: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 c8 7e ff f9 f5 c3 ce 1e 1d 44 91 b7 fb 09 5d e0 7e
INFO: 2016/02/22 10:47:38 INPUT END7: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 ***c8***
我不明白为什么包含 "INPUT END7" 的行不等于 "input" 值。
最后一个字节总是等于"encrypted"输出中的第一个字节...
这是将切片发送到通道的代码:
func ClientReader(client *Client) {
//Main Read loop
for {
bytesRead, buffer := client.Read()
if bytesRead < HEADERSIZE {
//client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(""), []byte("Minimum header not recieved."))
client.Close()
break // Connection to host is broken
}
//dbgMsg.Printf("RAW RECIEVED % x", buffer)
cmdBit, encryptionByte, ts, payload := protocolParser(buffer)
dbgMsg.Printf("CMDBIT: % x, ENCBIT: % x, TS: % d, PAYLOAD: % x", cmdBit, encryptionByte, ts, payload)
if encryptionByte == 0xAE {
payload, _ = barrenoid.Decrypt(client.AES_Key, payload)
dbgMsg.Printf("Decrypted payload % x\n", payload)
} else if encryptionByte == 0x00 {
// no need to decrypt
} else {
//bad packet reject
}
if cmdBit == 0x0D{
//payload, _ = barrenoid.Encrypt(client.AES_Key, payload)
client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), payload)
} else if cmdBit == 0x1C {
client.Name = string(payload)
} else {
//bad packet reject
//client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), []byte("Unknown command bit."))
}
}
切片 temp
和 input
共享同一个支持数组。通过一个切片的修改通过另一个切片可见。包含 "INPUT END7" 的行与包含 "INPUT END1" 的行不同,因为在这一行修改了切片的支持数组:
asdf := append(header, e...)
您可以使用这行代码复制支持数组:
temp := append([]byte(nil), input...)