コード例 #1
0
        public void Broadcast(int payloadType, byte[] payload)
        {
            if (payload.Length > OVRNetwork.MaxPayloadLength)
            {
                Debug.LogWarningFormat("[OVRNetworkTcpServer] drop payload because it's too long: {0} bytes", payload.Length);
            }

            FrameHeader header = new FrameHeader();

            header.protocolIdentifier = FrameHeaderMagicIdentifier;
            header.payloadType        = payloadType;
            header.payloadLength      = payload.Length;

            byte[] headerBuffer = header.ToBytes();

            byte[] dataBuffer = new byte[headerBuffer.Length + payload.Length];
            headerBuffer.CopyTo(dataBuffer, 0);
            payload.CopyTo(dataBuffer, headerBuffer.Length);

            lock (clientsLock)
            {
                foreach (TcpClient client in clients)
                {
                    if (client.Connected)
                    {
                        try
                        {
                            client.GetStream().BeginWrite(dataBuffer, 0, dataBuffer.Length, new AsyncCallback(DoWriteDataCallback), client.GetStream());
                        }
                        catch (SocketException e)
                        {
                            Debug.LogWarningFormat("[OVRNetworkTcpServer] close client because of socket error: {0}", e.Message);
                            client.GetStream().Close();
                            client.Close();
                        }
                    }
                }
            }
        }