private void Client_MessageReceived(object sender, DarkRift.Client.MessageReceivedEventArgs e)
    {
        try
        {
            using (Message message = e.GetMessage())
                using (DarkRiftReader reader = message.GetReader())
                {
                    OpCodes opCode = (OpCodes)message.Tag;
                    switch (opCode)
                    {
                    case OpCodes.ServerConnectionData:
                        string serverIP = reader.ReadString();

                        if (directConnectModule == null)
                        {
                            directConnectAddress = "";
                            return;
                        }

                        directConnectAddress = serverIP;
                        if (showDebugLogs)
                        {
                            Debug.Log("Received direct connect info from server.");
                        }
                        break;

                    case OpCodes.Authenticated:
                        isAuthenticated = true;
                        if (showDebugLogs)
                        {
                            Debug.Log("Authenticated with server.");
                        }
                        break;

                    case OpCodes.AuthenticationRequest:
                        using (DarkRiftWriter writer = DarkRiftWriter.Create())
                        {
                            writer.Write(relayPassword);
                            using (Message sendAuthenticationResponse = Message.Create((ushort)OpCodes.AuthenticationResponse, writer))
                                drClient.Client.SendMessage(sendAuthenticationResponse, SendMode.Reliable);
                        }
                        if (showDebugLogs)
                        {
                            Debug.Log("Server requested authentication key.");
                        }
                        break;

                    case OpCodes.GetData:
                        int    dataLength   = reader.ReadInt32();
                        byte[] receivedData = new byte[dataLength];
                        System.Buffer.BlockCopy(reader.ReadBytes(), 0, receivedData, 0, dataLength);

                        if (isServer)
                        {
                            OnServerDataReceived?.Invoke(connectedRelayClients.GetByFirst(reader.ReadUInt16()), new ArraySegment <byte>(receivedData), e.SendMode == SendMode.Unreliable ? 1 : 0);
                        }

                        if (isClient)
                        {
                            OnClientDataReceived?.Invoke(new ArraySegment <byte>(receivedData), e.SendMode == SendMode.Unreliable ? 1 : 0);
                        }

                        break;

                    case OpCodes.ServerLeft:

                        if (isClient)
                        {
                            isClient = false;
                            OnClientDisconnected?.Invoke();
                        }

                        break;

                    case OpCodes.PlayerDisconnected:

                        if (isServer)
                        {
                            ushort user = reader.ReadUInt16();
                            OnServerDisconnected?.Invoke(connectedRelayClients.GetByFirst(user));
                            connectedRelayClients.Remove(user);
                            if (showDebugLogs)
                            {
                                Debug.Log($"Client {user} left room.");
                            }
                        }

                        break;

                    case OpCodes.RoomCreated:
                        serverID    = reader.ReadUInt16();
                        isConnected = true;
                        if (showDebugLogs)
                        {
                            Debug.Log("Server Created on relay.");
                        }
                        break;

                    case OpCodes.ServerJoined:
                        ushort clientID = reader.ReadUInt16();

                        if (isClient)
                        {
                            isConnected = true;
                            OnClientConnected?.Invoke();
                            if (showDebugLogs)
                            {
                                Debug.Log("Successfully joined server.");
                            }
                        }

                        if (isServer)
                        {
                            connectedRelayClients.Add(clientID, currentMemberID);
                            OnServerConnected?.Invoke(currentMemberID);
                            currentMemberID++;
                            if (showDebugLogs)
                            {
                                Debug.Log($"Client {clientID} joined the server.");
                            }
                        }
                        break;

                    case OpCodes.ServerListResponse:
                        int serverListCount = reader.ReadInt32();
                        relayServerList.Clear();
                        for (int i = 0; i < serverListCount; i++)
                        {
                            relayServerList.Add(new RelayServerInfo()
                            {
                                serverName     = reader.ReadString(),
                                currentPlayers = reader.ReadInt32(),
                                maxPlayers     = reader.ReadInt32(),
                                serverID       = reader.ReadUInt16(),
                                serverData     = reader.ReadString()
                            });
                        }
                        serverListUpdated?.Invoke();
                        if (showDebugLogs)
                        {
                            Debug.Log("Received Server List.");
                        }
                        break;
                    }
                }
        }
        catch
        {
            // Server shouldnt send messed up data but we do have an unreliable channel, so eh.
        }
    }
Ejemplo n.º 2
0
    void MessageReceived(object sender, MessageReceivedEventArgs e)
    {
        using (Message message = e.GetMessage() as Message)
        {
            using (DarkRiftReader reader = message.GetReader())
            {
                switch (message.Tag)
                {
                //// Byte[] 압축 수신일 경우
                //case MessageTags.VIDEO_STREAMING:
                //    {
                //        uint checkflag = reader.ReadUInt32();
                //        short uid = reader.ReadInt16();

                //        MemoryStream input = new MemoryStream(reader.ReadBytes());
                //        MemoryStream output = new MemoryStream();
                //        using (DeflateStream dstream = new DeflateStream(input, CompressionMode.Decompress))
                //        {
                //            dstream.CopyTo(output);
                //        }
                //        SetCameraTexture(output.ToArray());
                //    }
                //    break;

                //// LOGIC  (DeflateStream Byte[]분할 수신일 경우)
                case MessageTags.VIDEO_STREAMING:
                {
                    uint  checkflag  = reader.ReadUInt32();
                    short uid        = reader.ReadInt16();
                    int   numOfDatas = reader.ReadInt32();
                    var   idx        = reader.ReadInt32();

                    if (idx == 0)
                    {
                        textureByteData.Clear();
                    }

                    textureByteData.AddRange(reader.ReadBytes());

                    if (idx == numOfDatas)
                    {
                        Debug.Log(textureByteData.Count);
                        MemoryStream input  = new MemoryStream(textureByteData.ToArray());
                        MemoryStream output = new MemoryStream();
                        using (DeflateStream dstream = new DeflateStream(input, CompressionMode.Decompress))
                        {
                            dstream.CopyTo(output);
                        }
                        SetCameraTexture(output.ToArray());
                    }
                }
                break;

                    //// LOGIC  (BitConverter Byte[]분할 수신일 경우)
                    //case MessageTags.VIDEO_STREAMING:
                    //    {
                    //        uint checkflag = reader.ReadUInt32();
                    //        short uid = reader.ReadInt16();
                    //        int numOfDatas = reader.ReadInt32();
                    //        var idx = reader.ReadInt32();

                    //        if (idx == 0)
                    //        {
                    //            textureByteData.Clear();
                    //        }

                    //        textureByteData.AddRange(reader.ReadBytes());

                    //        if (idx == numOfDatas)
                    //        {
                    //            MemoryStream ms = new MemoryStream();
                    //            var gzBuffer = textureByteData.ToArray();
                    //            int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                    //            ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

                    //            byte[] buffer = new byte[msgLength];

                    //            ms.Position = 0;
                    //            GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);
                    //            zip.Read(buffer, 0, buffer.Length);

                    //            SetCameraTexture(buffer);
                    //        }
                    //    }
                    //    break;
                }
            }
        }
    }