Переработано общение с клиентами SFU (бекенд серверами)
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
.env
|
.env
|
||||||
|
.vscode
|
||||||
@@ -2,7 +2,6 @@ package sfu
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
connection "g365sfu/socket/struct"
|
connection "g365sfu/socket/struct"
|
||||||
"g365sfu/utils"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/pion/webrtc/v4"
|
"github.com/pion/webrtc/v4"
|
||||||
@@ -33,8 +32,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// CreateRoom создает комнату
|
// CreateRoom создает комнату
|
||||||
func CreateRoom(server *connection.Connection) (*Room, error) {
|
func CreateRoom(server *connection.Connection, roomID string) (*Room, error) {
|
||||||
roomID := "room_" + utils.RandomString(64)
|
|
||||||
roomsMu.Lock()
|
roomsMu.Lock()
|
||||||
defer roomsMu.Unlock()
|
defer roomsMu.Unlock()
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package socket
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"g365sfu/bytebuffer"
|
||||||
"g365sfu/logger"
|
"g365sfu/logger"
|
||||||
"g365sfu/sfu"
|
"g365sfu/sfu"
|
||||||
connection "g365sfu/socket/struct"
|
connection "g365sfu/socket/struct"
|
||||||
@@ -65,15 +66,23 @@ func randomSocketIdentifier() string {
|
|||||||
func processData(data <-chan []byte, connection *connection.Connection) {
|
func processData(data <-chan []byte, connection *connection.Connection) {
|
||||||
for bytes := range data {
|
for bytes := range data {
|
||||||
// Логика обработки байтов
|
// Логика обработки байтов
|
||||||
if bytes[0] == 0x01 {
|
buffer := bytebuffer.Wrap(bytes)
|
||||||
|
packetId, _ := buffer.Get()
|
||||||
|
if packetId == 0x01 {
|
||||||
// Это рукопожатие, дальше сравниваем секретные ключи
|
// Это рукопожатие, дальше сравниваем секретные ключи
|
||||||
// Секретный ключ идет сразу после первого байта и до конца сообщения
|
secretLength, _ := buffer.GetUint32()
|
||||||
receivedSecret := string(bytes[1:])
|
|
||||||
if receivedSecret == getSecret() {
|
receivedSecret, _ := buffer.GetBytes((int(secretLength)))
|
||||||
|
if string(receivedSecret) == getSecret() {
|
||||||
logger.LogSuccessMessage("success handshake from " + connection.Socket.RemoteAddr().String())
|
logger.LogSuccessMessage("success handshake from " + connection.Socket.RemoteAddr().String())
|
||||||
AddSocket(connection)
|
AddSocket(connection)
|
||||||
connection.Socket.WriteMessage(websocket.BinaryMessage, []byte{0x01})
|
// Подготовка ответа для клиента о успешном рукопожатии
|
||||||
return
|
response := bytebuffer.Allocate(1)
|
||||||
|
response.Put(0x01)
|
||||||
|
response.Flip()
|
||||||
|
// Отправляем ответ клиенту
|
||||||
|
connection.Socket.WriteMessage(websocket.BinaryMessage, response.Bytes())
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
connection.Socket.WriteMessage(websocket.BinaryMessage, []byte{0xFF})
|
connection.Socket.WriteMessage(websocket.BinaryMessage, []byte{0xFF})
|
||||||
logger.LogWarnMessage("failed handshake from " + connection.Socket.RemoteAddr().String() + " because of invalid secret key")
|
logger.LogWarnMessage("failed handshake from " + connection.Socket.RemoteAddr().String() + " because of invalid secret key")
|
||||||
@@ -89,34 +98,51 @@ func processData(data <-chan []byte, connection *connection.Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Создание комнаты
|
// Создание комнаты
|
||||||
if bytes[0] == 0x02 {
|
if packetId == 0x02 {
|
||||||
room, _ := sfu.CreateRoom(connection)
|
roomLength, _ := buffer.GetUint32()
|
||||||
|
roomIDBytes, _ := buffer.GetBytes(int(roomLength))
|
||||||
|
roomID := string(roomIDBytes)
|
||||||
|
room, _ := sfu.CreateRoom(connection, roomID)
|
||||||
logger.LogSuccessMessage("room initializated " + room.RoomID)
|
logger.LogSuccessMessage("room initializated " + room.RoomID)
|
||||||
bytes = append([]byte{0x02}, []byte(room.RoomID)...)
|
// Подготовка ответа для клиента с ID комнаты
|
||||||
connection.Socket.WriteMessage(websocket.BinaryMessage, bytes)
|
response := bytebuffer.Allocate(1 + 4 + len([]byte(room.RoomID)))
|
||||||
return
|
response.Put(0x02)
|
||||||
|
response.PutUint32(uint32(len([]byte(room.RoomID))))
|
||||||
|
response.PutBytes([]byte(room.RoomID))
|
||||||
|
response.Flip()
|
||||||
|
// Отправляем ответ клиенту
|
||||||
|
connection.Socket.WriteMessage(websocket.BinaryMessage, response.Bytes())
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
//SDP OFFER для подключения к комнате
|
//SDP OFFER для подключения к комнате
|
||||||
if bytes[0] == 0x03 {
|
if packetId == 0x03 {
|
||||||
roomIdLen := int(bytes[1])
|
roomIdLen, _ := buffer.GetUint32()
|
||||||
roomID := string(bytes[2 : 2+roomIdLen])
|
roomIDBytes, _ := buffer.GetBytes(int(roomIdLen))
|
||||||
|
roomID := string(roomIDBytes)
|
||||||
_, exists := sfu.GetRoom(roomID)
|
_, exists := sfu.GetRoom(roomID)
|
||||||
if !exists {
|
if !exists {
|
||||||
logger.LogWarnMessage("peer " + connection.Socket.RemoteAddr().String() + " tried to join non existing room " + roomID)
|
logger.LogWarnMessage("peer " + connection.Socket.RemoteAddr().String() + " tried to join non existing room " + roomID)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
peerIdLen := int(bytes[2+roomIdLen])
|
peerIdLen, _ := buffer.GetUint32()
|
||||||
peerID := string(bytes[3+roomIdLen : 3+roomIdLen+peerIdLen])
|
peerIDBytes, _ := buffer.GetBytes(int(peerIdLen))
|
||||||
logger.LogSuccessMessage("peer " + connection.Socket.RemoteAddr().String() + " joined to room " + roomID)
|
peerID := string(peerIDBytes)
|
||||||
|
logger.LogSuccessMessage("peer " + peerID + " joined to room " + roomID)
|
||||||
|
offerLength, _ := buffer.GetUint32()
|
||||||
|
offerBytes, _ := buffer.GetBytes(int(offerLength))
|
||||||
var offer webrtc.SessionDescription
|
var offer webrtc.SessionDescription
|
||||||
err := json.Unmarshal(bytes[3+roomIdLen+peerIdLen:], &offer)
|
err := json.Unmarshal(offerBytes, &offer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LogWarnMessage("failed to unmarshal offer from peer " + connection.Socket.RemoteAddr().String() + ": " + err.Error())
|
logger.LogWarnMessage("failed to unmarshal offer from peer " + peerID + " in room " + roomID + ": " + err.Error())
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
sfu.JoinWithOffer(roomID, peerID, offer)
|
_, err = sfu.JoinWithOffer(roomID, peerID, offer)
|
||||||
return
|
if err != nil {
|
||||||
|
logger.LogWarnMessage("failed to join peer " + peerID + " to room " + roomID + ": " + err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user