在go例程中追加切片
我试着教自己去。我已经编写了一个简单的客户端/服务器应用程序,它有一些加密和非常简单的数据包结构 我有一个go例程,用于侦听数据,然后将数据发送到每个连接的客户端。在我向每个客户机发送数据的函数中,我将一条消息附加到一个头中,但它做了一些奇怪的行为在go例程中追加切片,go,slice,channel,Go,Slice,Channel,我试着教自己去。我已经编写了一个简单的客户端/服务器应用程序,它有一些加密和非常简单的数据包结构 我有一个go例程,用于侦听数据,然后将数据发送到每个连接的客户端。在我向每个客户机发送数据的函数中,我将一条消息附加到一个头中,但它做了一些奇怪的行为 func ClientSender(client *Client) { for { input := <-client.Outgoing //TODO add cleanup qu
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 END7”的行不等于“INPUT”值
最后一个字节始终等于“加密”输出中的第一个字节
下面是将切片发送到通道的代码:
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."))
}
}
func客户端阅读器(客户端*客户端){
//主读循环
为了{
字节读取,缓冲区:=client.Read()
如果字节读取 //客户端。传出片temp
和input
共享同一个备份数组。通过一个片的修改通过另一个片可见。包含“input END7”的行与包含“input END1”的行不同,因为片的备份数组在此行上被修改:
asdf := append(header, e...)
您可以使用以下代码行复制备份阵列:
temp := append([]byte(nil), input...)
切片temp
和input
共享相同的备份数组。通过一个切片进行的修改通过另一个切片可见。包含“input END7”的行与包含“input END1”的行不同,因为切片的备份数组在此行上进行了修改:
asdf := append(header, e...)
您可以使用以下代码行复制备份阵列:
temp := append([]byte(nil), input...)
频道的类型是什么?频道的类型是什么?啊,我没意识到。谢谢!那把它修好了。啊,我没意识到。谢谢!那把它修好了。