浏览代码

feat(backend): add adf implementation

v0lp3 4 年之前
父节点
当前提交
e0407249ca
共有 2 个文件被更改,包括 36 次插入10 次删除
  1. 32 8
      src/backend.go
  2. 4 2
      src/main.go

+ 32 - 8
src/backend.go

@@ -11,7 +11,7 @@ import (
 	"time"
 )
 
-func Scan(brotherIP string, brotherPort int, resolution int, color string, adf bool) ([]byte, int, int) {
+func Scan(brotherIP string, brotherPort int, resolution int, color string, adf bool) ([][]byte, int, int) {
 	log.Println("Valid IP address, opening socket...")
 
 	socket, err := net.Dial("tcp", fmt.Sprintf("%s:%d", brotherIP, brotherPort))
@@ -146,18 +146,42 @@ func SaveImage(data []byte, width int, height int, name string, color string) {
 	}
 }
 
-func removeHeaders(scan []byte) []byte {
+func removeHeaders(data []byte) [][]byte {
 	log.Println("Removing headers from bytes...")
 
 	const headerLen int = 12
 
-	payloadLen := binary.LittleEndian.Uint16(scan[headerLen-2 : headerLen])
-	chunkSize := int(payloadLen) + headerLen
-	scanOutput := make([]byte, 0)
+	pages := make([][]byte, 0)
+	page := make([]byte, 0)
 
-	for i := 0; i < len(scan)-chunkSize; i += chunkSize {
-		scanOutput = append(scanOutput, scan[i+headerLen:i+chunkSize]...)
+	currentPage := 1
+	i := 0
+
+headersLoop:
+	for {
+		if data[i] == 0x82 {
+			log.Println("Parsed page", currentPage)
+			pages = append(pages, page)
+
+			if len(data) > i+10 && data[i+10] == 0x80 {
+				break headersLoop
+			}
+
+			page = make([]byte, 0)
+
+			currentPage++
+
+			i += headerLen - 2
+			continue headersLoop
+		}
+
+		payloadLen := binary.LittleEndian.Uint16(data[i+headerLen-2 : i+headerLen])
+		chunkSize := int(payloadLen) + headerLen
+
+		page = append(page, data[i+headerLen:i+chunkSize]...)
+
+		i += chunkSize
 	}
 
-	return scanOutput
+	return pages
 }

+ 4 - 2
src/main.go

@@ -21,7 +21,9 @@ func main() {
 		HandleError(fmt.Errorf("invalid IP address: %s", *brotherIP))
 	}
 
-	rawImage, width, heigth := Scan(*brotherIP, brotherPort, *resolution, *color, *adf)
+	rawImages, width, heigth := Scan(*brotherIP, brotherPort, *resolution, *color, *adf)
 
-	SaveImage(rawImage, width, heigth, *name, *color)
+	for i, rawImage := range rawImages {
+		SaveImage(rawImage, width, heigth, fmt.Sprintf("%s(%d)", *name, i), *color)
+	}
 }