-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
389 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package handle | ||
|
||
import ( | ||
global "github.com/IUnlimit/perpetua/internal" | ||
collections "github.com/chenjiandongx/go-queue" | ||
) | ||
|
||
var echoMap *EchoMap | ||
|
||
// EchoMap 利用 echo 完成 NTQQ -> client 消息调度 (发送时标记echo) | ||
type EchoMap struct { | ||
Receive chan bool | ||
|
||
dataMap map[string]*collections.Queue | ||
} | ||
|
||
func NewEchoMap() *EchoMap { | ||
return &EchoMap{ | ||
dataMap: make(map[string]*collections.Queue), | ||
Receive: make(chan bool), | ||
} | ||
} | ||
|
||
// JustPut echo | ||
func (em *EchoMap) JustPut(id string, data *global.MsgData) { | ||
queue := echoMap.dataMap[id] | ||
if queue == nil { | ||
queue = collections.NewQueue() | ||
echoMap.dataMap[id] = queue | ||
} | ||
queue.Put(data) | ||
} | ||
|
||
func (em *EchoMap) JustGet(id string, consumer func(*global.MsgData)) { | ||
queue := echoMap.dataMap[id] | ||
if queue == nil { | ||
return | ||
} | ||
|
||
for { | ||
e, ok := queue.Get() | ||
if !ok { | ||
return | ||
} | ||
consumer(e.(*global.MsgData)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package handle | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
global "github.com/IUnlimit/perpetua/internal" | ||
collections "github.com/chenjiandongx/go-queue" | ||
"github.com/google/uuid" | ||
log "github.com/sirupsen/logrus" | ||
"testing" | ||
) | ||
|
||
func TestEcho(t *testing.T) { | ||
id := uuid.NewString() | ||
str := fmt.Sprintf("%s#%s#a", global.EchoPrefix, id) | ||
re := global.EchoRegx | ||
matches := re.FindStringSubmatch(str) | ||
|
||
if len(matches) == 4 { | ||
group1 := matches[1] | ||
group2 := matches[2] | ||
group3 := matches[3] | ||
|
||
fmt.Println("Group 1:", group1) | ||
fmt.Println("Group 2:", group2) | ||
fmt.Println("Group 3:", group3) | ||
} else { | ||
fmt.Println("No match found, len: ", len(uuid.NewString())) | ||
} | ||
} | ||
|
||
func TestUpdateEcho(t *testing.T) { | ||
message := `{ | ||
"action": "send_private_msg", | ||
"params": { | ||
"user_id": 765743073, | ||
"message": "hello" | ||
}, | ||
"echo": "" | ||
}` | ||
var msgData global.MsgData | ||
err := json.Unmarshal([]byte(message), &msgData) | ||
if err != nil { | ||
log.Errorf("[<-Client] Failed to unmarshal client message: %s", string(message)) | ||
return | ||
} | ||
|
||
// sign with echo field | ||
id := uuid.NewString() | ||
echo := msgData["echo"].(string) | ||
echo = fmt.Sprintf("%s#%s#%s", global.EchoPrefix, id, echo) | ||
msgData["echo"] = echo | ||
log.Printf("[<-Client] Update client(port-%d) message echo: %s", 666, echo) | ||
} | ||
|
||
func TestSend(t *testing.T) { | ||
message := `{ | ||
"action": "send_private_msg", | ||
"params": { | ||
"user_id": 765743073, | ||
"message": "hello" | ||
}, | ||
"echo": "" | ||
}` | ||
var msgData global.MsgData | ||
err := json.Unmarshal([]byte(message), &msgData) | ||
if err != nil { | ||
log.Errorf("[<-Client] Failed to unmarshal client message: %s", string(message)) | ||
return | ||
} | ||
|
||
bytes, _ := json.Marshal(msgData) | ||
fmt.Println(string(bytes)) | ||
} | ||
|
||
func TestQueue(t *testing.T) { | ||
queue := collections.NewQueue() | ||
queue.Put("666") | ||
for { | ||
e, ok := queue.Get() | ||
if !ok { | ||
return | ||
} | ||
fmt.Println(e) | ||
} | ||
} | ||
|
||
func TestSlice(t *testing.T) { | ||
receivers := make([]*Handler, 1) | ||
handler := NewHandler(context.Background()) | ||
receivers = append(receivers, handler) | ||
fmt.Println(len(receivers)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.