From f264bdf67f27ab26276589bd27faae67f74064ef Mon Sep 17 00:00:00 2001 From: set Date: Sat, 14 Mar 2026 18:43:57 +0200 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=BD=D0=BE=20=D0=BE=D0=B1=D1=89=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=81=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D0=B0=D0=BC=D0=B8=20SFU=20(=D0=B1=D0=B5=D0=BA=D0=B5=D0=BD?= =?UTF-8?q?=D0=B4=20=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80=D0=B0=D0=BC=D0=B8?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- sfu/rooms.go | 4 +-- socket/socket.go | 72 ++++++++++++++++++++++++++++++++---------------- 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 2eea525..c2111fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.env \ No newline at end of file +.env +.vscode \ No newline at end of file diff --git a/sfu/rooms.go b/sfu/rooms.go index 3c59889..9c0f0c2 100644 --- a/sfu/rooms.go +++ b/sfu/rooms.go @@ -2,7 +2,6 @@ package sfu import ( connection "g365sfu/socket/struct" - "g365sfu/utils" "sync" "github.com/pion/webrtc/v4" @@ -33,8 +32,7 @@ var ( ) // CreateRoom создает комнату -func CreateRoom(server *connection.Connection) (*Room, error) { - roomID := "room_" + utils.RandomString(64) +func CreateRoom(server *connection.Connection, roomID string) (*Room, error) { roomsMu.Lock() defer roomsMu.Unlock() diff --git a/socket/socket.go b/socket/socket.go index cffa1af..dc4f362 100644 --- a/socket/socket.go +++ b/socket/socket.go @@ -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 } } }