init repo.
Some checks failed
catsync-build / simple-build (amd64, drawin) (push) Failing after 36s
catsync-build / simple-build (amd64, linux) (push) Failing after 36s
catsync-build / simple-build (amd64, windows) (push) Failing after 36s
catsync-build / simple-build (arm64, drawin) (push) Failing after 37s
catsync-build / simple-build (arm64, linux) (push) Failing after 37s

This commit is contained in:
tqcq 2024-12-22 19:34:50 +08:00
parent 9384ff370f
commit cc86c06c10
7 changed files with 180 additions and 7 deletions

View File

@ -0,0 +1,51 @@
---
name: catsync-build
on:
push:
paths:
- '.gitea/workflows/build.yml'
- 'go.mod'
- 'go.sum'
- 'main.go'
- 'assets/**'
- 'pb/**'
pull_request:
paths:
- '.gitea/workflows/build.yml'
- 'go.mod'
- 'go.sum'
- 'main.go'
- 'assets/**'
- 'pb/**'
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
name: simple-build
runs-on: ubuntu-20.04
strategy:
matrix:
goos: [linux, windows, drawin]
goarch: [amd64, arm64]
exclude:
- goarch: arm64
goos: windows
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v3
with:
go-version: 1.23.1
- name: build
run: |
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -x .
# - name: GoReleaser
# uses: goreleaser/goreleaser-action@v5
# with:
# distribution: goreleaser
# version: latest
# args: release --clean ${{ env.flags }}
#

9
.gitignore vendored
View File

@ -1,7 +1,2 @@
out/ catsync
build/ pb/
cmake-kits.json
.cache/
.idea
compile_commands.json
*.a

View File

View File

@ -0,0 +1,20 @@
syntax="proto3";
package main;
option go_package = "/pb";
import public "google/protobuf/timestamp.proto";
message PeerMessage {
uint32 magic_number= 1;
uint32 version = 2;
PeerMessageType type = 3;
google.protobuf.Timestamp send_timestamp = 4;
string uid = 5;
}
enum PeerMessageType {
PEER_MESSAGE_TYPE_REQUEST = 0;
PEER_MESSAGE_TYPE_RESPONSE = 1;
PEER_MESSAGE_TYPE_ACK = 2;
}

7
go.mod Normal file
View File

@ -0,0 +1,7 @@
module code.uocat.com/tqcq/catsync
go 1.23.3
require (
google.golang.org/protobuf v1.36.0 // indirect
)

4
go.sum Normal file
View File

@ -0,0 +1,4 @@
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
google.golang.org/protobuf v1.36.0 h1:mjIs9gYtt56AzC4ZaffQuh88TZurBGhIJMBZGSxNerQ=
google.golang.org/protobuf v1.36.0/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=

96
main.go Normal file
View File

@ -0,0 +1,96 @@
package main
import (
"code.uocat.com/tqcq/catsync/pb"
"context"
"fmt"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"net"
"sync"
"time"
)
var (
PeerList = make(map[string]net.UDPAddr)
)
func LANServer(ctx context.Context, conn *net.UDPConn) {
var wait_group sync.WaitGroup
defer wait_group.Wait()
for {
buf := make([]byte, 1024)
n, remote_addr, err := conn.ReadFromUDP(buf)
if err != nil {
fmt.Printf("Recv failed. %v\n", err)
continue
}
if n != 0 {
// process request
wait_group.Add(1)
go func() {
defer wait_group.Done()
fmt.Printf("remote_addr: %s\n", remote_addr.AddrPort().String())
pm := &pb.PeerMessage{}
err := proto.Unmarshal(buf[:n], pm)
if err != nil {
fmt.Printf("Unmarshal failed. %v\n", err)
return
}
fmt.Printf("uid: %s, send_timestamp: %s\n", pm.Uid, pm.SendTimestamp.AsTime().Local())
}()
continue
}
}
}
func LANClient(ctx context.Context, conn *net.UDPConn) {
broadcast_addr, err := net.ResolveUDPAddr("udp4", "255.255.255.255:8829")
if err != nil {
fmt.Printf("err: %v", err)
return
}
for {
pm := &pb.PeerMessage{
MagicNumber: 13,
Version: 0,
SendTimestamp: timestamppb.Now(),
Type: pb.PeerMessageType_PEER_MESSAGE_TYPE_REQUEST,
Uid: "test uid",
}
buf, err := proto.Marshal(pm)
if err != nil {
fmt.Printf("err: %v\n", err)
} else {
_, _ = conn.WriteToUDP(buf, broadcast_addr)
}
time.Sleep(5 * time.Second)
}
}
func main() {
listenAddr, err := net.ResolveUDPAddr("udp4", ":8829")
if err != nil {
fmt.Printf("ResolveUDPAddr failed. %v\n", err)
return
}
broadcastServer, err := net.ListenUDP("udp4", listenAddr)
if err != nil {
fmt.Printf("ListenUDP Failed. %v\n", err)
return
}
defer broadcastServer.Close()
fmt.Printf("Server Started.\n")
ctx := context.Background()
go LANServer(ctx, broadcastServer)
go LANClient(ctx, broadcastServer)
select {
case <-ctx.Done():
fmt.Printf("ctx Done: %v", <-ctx.Done())
}
}