Exemple #1
0
        /// <summary>
        /// Sends a message to the WebSocket server
        /// </summary>
        /// <param name="buffer">An ArraySegment of bytes that will be sent to the WebSocket server.</param>
        /// <param name="messageType">This is the message type to send Binary or Text</param>
        /// <returns>Ok or an error</returns>
        public PPError Send(ArraySegment <byte> buffer, WebSocketMessageType messageType)
        {
            ThrowIfNotConnected();

            if (messageType != WebSocketMessageType.Binary &&
                messageType != WebSocketMessageType.Text)
            {
                throw new ArgumentException($"Invalid Message Type {messageType} in method Send - Valid values are {WebSocketMessageType.Binary}, {WebSocketMessageType.Text}",
                                            "messageType");
            }

            ValidateArraySegment <byte>(buffer, "buffer");

            if (messageType == WebSocketMessageType.Text)
            {
                var varBuffer = new Var(Encoding.UTF8.GetString(buffer.Array));
                return((PPError)PPBWebSocket.SendMessage(this, varBuffer));
            }
            else
            {
                var size        = (uint)buffer.Count;
                var arrayBuffer = new VarArrayBuffer(size);

                var data = arrayBuffer.Map();
                for (int i = 0; i < size; ++i)
                {
                    data[i] = buffer.Array[i];
                }
                arrayBuffer.Flush();
                arrayBuffer.Unmap();

                return((PPError)PPBWebSocket.SendMessage(this, arrayBuffer));
            }
        }
Exemple #2
0
        private async Task <PPError> SendAsyncCore(ArraySegment <byte> buffer, WebSocketMessageType messageType, MessageLoop sendLoop)
        {
            var tcs = new TaskCompletionSource <PPError>();

            try
            {
                if (messageType == WebSocketMessageType.Text)
                {
                    if (MessageLoop == null)
                    {
                        tcs.TrySetResult(Send(buffer, messageType));
                    }
                    else
                    {
                        Action <PPError> action = new Action <PPError>((e) =>
                        {
                            var varBuffer = new Var(Encoding.UTF8.GetString(buffer.Array));
                            var result    = (PPError)PPBWebSocket.SendMessage(this, varBuffer);
                            tcs.TrySetResult(result);
                        }
                                                                       );
                        InvokeHelper(action, sendLoop);
                    }
                }
                else
                {
                    if (MessageLoop == null)
                    {
                        tcs.TrySetResult(Send(buffer, messageType));
                    }
                    else
                    {
                        Action <PPError> action = new Action <PPError>((e) =>
                        {
                            var size        = (uint)buffer.Count;
                            var arrayBuffer = new VarArrayBuffer(size);

                            var data = arrayBuffer.Map();
                            for (int i = 0; i < size; ++i)
                            {
                                data[i] = buffer.Array[i];
                            }
                            arrayBuffer.Flush();
                            arrayBuffer.Unmap();

                            var result = (PPError)PPBWebSocket.SendMessage(this, arrayBuffer);
                            tcs.TrySetResult(result);
                        });
                        InvokeHelper(action, sendLoop);
                    }
                }

                return(await tcs.Task);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
                tcs.SetException(exc);
                return(PPError.Aborted);
            }
        }