recreate a complete protobuf message from multiple chunks each new message is sent to handleData
(read_size int, size_to_read int, buffer *[]byte, total_data *[]byte)
| 51 | // recreate a complete protobuf message from multiple chunks |
| 52 | // each new message is sent to handleData |
| 53 | func (c *Command) processData(read_size int, size_to_read int, buffer *[]byte, total_data *[]byte) int { |
| 54 | |
| 55 | if read_size < size_to_read { |
| 56 | // not enough data |
| 57 | size_to_read -= read_size |
| 58 | data := make([]byte, read_size) |
| 59 | copy(data, (*buffer)[0:read_size]) |
| 60 | (*total_data) = append((*total_data), data...) |
| 61 | |
| 62 | } else { |
| 63 | |
| 64 | data := make([]byte, size_to_read) |
| 65 | copy(data, (*buffer)[0:size_to_read]) |
| 66 | |
| 67 | c.handleData(append((*total_data), data...)) |
| 68 | |
| 69 | if read_size > size_to_read { |
| 70 | // too much data |
| 71 | remaining := read_size - size_to_read - 1 |
| 72 | size_to_read = int(c.Buffer[size_to_read]) - remaining |
| 73 | |
| 74 | (*total_data) = make([]byte, remaining) |
| 75 | copy((*total_data), (*buffer)[size_to_read+1:read_size]) |
| 76 | |
| 77 | } else { |
| 78 | // exact amount of data |
| 79 | (*total_data) = make([]byte, 0) |
| 80 | size_to_read = 0 |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return size_to_read |
| 85 | } |
| 86 | |
| 87 | // reading loop to read data from the android tv device |
| 88 | func (c *Command) readLoop() { |