Переработано общение с клиентами SFU (бекенд серверами)
This commit is contained in:
@@ -2,6 +2,7 @@ package socket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"g365sfu/bytebuffer"
|
||||
"g365sfu/logger"
|
||||
"g365sfu/sfu"
|
||||
connection "g365sfu/socket/struct"
|
||||
@@ -65,15 +66,23 @@ func randomSocketIdentifier() string {
|
||||
func processData(data <-chan []byte, connection *connection.Connection) {
|
||||
for bytes := range data {
|
||||
// Логика обработки байтов
|
||||
if bytes[0] == 0x01 {
|
||||
buffer := bytebuffer.Wrap(bytes)
|
||||
packetId, _ := buffer.Get()
|
||||
if packetId == 0x01 {
|
||||
// Это рукопожатие, дальше сравниваем секретные ключи
|
||||
// Секретный ключ идет сразу после первого байта и до конца сообщения
|
||||
receivedSecret := string(bytes[1:])
|
||||
if receivedSecret == getSecret() {
|
||||
secretLength, _ := buffer.GetUint32()
|
||||
|
||||
receivedSecret, _ := buffer.GetBytes((int(secretLength)))
|
||||
if string(receivedSecret) == getSecret() {
|
||||
logger.LogSuccessMessage("success handshake from " + connection.Socket.RemoteAddr().String())
|
||||
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})
|
||||
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 {
|
||||
room, _ := sfu.CreateRoom(connection)
|
||||
if packetId == 0x02 {
|
||||
roomLength, _ := buffer.GetUint32()
|
||||
roomIDBytes, _ := buffer.GetBytes(int(roomLength))
|
||||
roomID := string(roomIDBytes)
|
||||
room, _ := sfu.CreateRoom(connection, roomID)
|
||||
logger.LogSuccessMessage("room initializated " + room.RoomID)
|
||||
bytes = append([]byte{0x02}, []byte(room.RoomID)...)
|
||||
connection.Socket.WriteMessage(websocket.BinaryMessage, bytes)
|
||||
return
|
||||
// Подготовка ответа для клиента с ID комнаты
|
||||
response := bytebuffer.Allocate(1 + 4 + len([]byte(room.RoomID)))
|
||||
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 для подключения к комнате
|
||||
if bytes[0] == 0x03 {
|
||||
roomIdLen := int(bytes[1])
|
||||
roomID := string(bytes[2 : 2+roomIdLen])
|
||||
if packetId == 0x03 {
|
||||
roomIdLen, _ := buffer.GetUint32()
|
||||
roomIDBytes, _ := buffer.GetBytes(int(roomIdLen))
|
||||
roomID := string(roomIDBytes)
|
||||
_, exists := sfu.GetRoom(roomID)
|
||||
if !exists {
|
||||
logger.LogWarnMessage("peer " + connection.Socket.RemoteAddr().String() + " tried to join non existing room " + roomID)
|
||||
return
|
||||
continue
|
||||
}
|
||||
peerIdLen := int(bytes[2+roomIdLen])
|
||||
peerID := string(bytes[3+roomIdLen : 3+roomIdLen+peerIdLen])
|
||||
logger.LogSuccessMessage("peer " + connection.Socket.RemoteAddr().String() + " joined to room " + roomID)
|
||||
peerIdLen, _ := buffer.GetUint32()
|
||||
peerIDBytes, _ := buffer.GetBytes(int(peerIdLen))
|
||||
peerID := string(peerIDBytes)
|
||||
logger.LogSuccessMessage("peer " + peerID + " joined to room " + roomID)
|
||||
offerLength, _ := buffer.GetUint32()
|
||||
offerBytes, _ := buffer.GetBytes(int(offerLength))
|
||||
var offer webrtc.SessionDescription
|
||||
err := json.Unmarshal(bytes[3+roomIdLen+peerIdLen:], &offer)
|
||||
err := json.Unmarshal(offerBytes, &offer)
|
||||
if err != nil {
|
||||
logger.LogWarnMessage("failed to unmarshal offer from peer " + connection.Socket.RemoteAddr().String() + ": " + err.Error())
|
||||
return
|
||||
logger.LogWarnMessage("failed to unmarshal offer from peer " + peerID + " in room " + roomID + ": " + err.Error())
|
||||
continue
|
||||
}
|
||||
sfu.JoinWithOffer(roomID, peerID, offer)
|
||||
return
|
||||
_, err = sfu.JoinWithOffer(roomID, peerID, offer)
|
||||
if err != nil {
|
||||
logger.LogWarnMessage("failed to join peer " + peerID + " to room " + roomID + ": " + err.Error())
|
||||
continue
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user