Ejemplo n.º 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));
            }
        }
Ejemplo n.º 2
0
        protected virtual void OnReceiveData(PPError error, ArraySegment <byte> buffer)
        {
            WebSocketReceiveResult receiveResult = null;
            var recVar = (Var)receiveVar;

            if (State == WebSocketState.Open)
            {
                if (recVar.IsArrayBuffer)
                {
                    var arrayBuffer = new VarArrayBuffer(receiveVar);
                    var size        = (uint)Math.Min(buffer.Count, arrayBuffer.ByteLength);

                    int offs = 0;
                    var data = arrayBuffer.Map();
                    for (offs = 0; offs < size; offs++)
                    {
                        buffer.Array[offs] = data[offs];
                    }
                    arrayBuffer.Unmap();
                    receiveResult = new WebSocketReceiveResult(size, WebSocketMessageType.Binary, true);
                }
                else
                {
                    var msg  = Encoding.UTF8.GetBytes(recVar.AsString());
                    var size = (uint)Math.Min(buffer.Count, msg.Length);

                    int offs = 0;
                    for (offs = 0; offs < size; offs++)
                    {
                        buffer.Array[offs] = msg[offs];
                    }
                    receiveResult = new WebSocketReceiveResult(size, WebSocketMessageType.Text, true);
                }
            }
            else
            {
                receiveResult = new WebSocketReceiveResult(0, WebSocketMessageType.Close, true, CloseStatus, CloseStatusDescription);
            }

            ReceiveData?.Invoke(this, receiveResult);
        }
Ejemplo n.º 3
0
        private async Task <WebSocketReceiveResult> ReceiveAsyncCore(ArraySegment <byte> buffer, MessageLoop receiveLoop = null)
        {
            var tcs = new TaskCompletionSource <WebSocketReceiveResult>();
            EventHandler <WebSocketReceiveResult> handler = (s, e) => { tcs.TrySetResult(e); };

            try
            {
                ReceiveData += handler;

                if (MessageLoop == null)
                {
                    //Console.WriteLine("Receive no message loop");
                    var receiveResult = Receive(buffer);
                    if (receiveResult != PPError.Ok && !tcs.Task.IsCompleted)
                    {
                        tcs.TrySetResult(new WebSocketReceiveResult(0, WebSocketMessageType.Close, true));
                    }
                }
                else
                {
                    Action <PPError> action = new Action <PPError>((e) =>
                    {
                        var rcvMsgResult = (PPError)PPBWebSocket.ReceiveMessage(this, out receiveVar, new BlockUntilComplete());
                        WebSocketReceiveResult receiveResult = null;
                        var recVar = (Var)receiveVar;
                        if (State == WebSocketState.Open)
                        {
                            if (recVar.IsArrayBuffer)
                            {
                                var arrayBuffer = new VarArrayBuffer(receiveVar);
                                var size        = (uint)Math.Min(buffer.Count, arrayBuffer.ByteLength);

                                int offs = 0;
                                var data = arrayBuffer.Map();
                                for (offs = 0; offs < size; offs++)
                                {
                                    buffer.Array[offs] = data[offs];
                                }
                                arrayBuffer.Unmap();
                                receiveResult = new WebSocketReceiveResult(size, WebSocketMessageType.Binary, true);
                            }
                            else
                            {
                                var msg  = Encoding.UTF8.GetBytes(recVar.AsString());
                                var size = (uint)Math.Min(buffer.Count, msg.Length);

                                int offs = 0;
                                for (offs = 0; offs < size; offs++)
                                {
                                    buffer.Array[offs] = msg[offs];
                                }
                                receiveResult = new WebSocketReceiveResult(size, WebSocketMessageType.Text, true);
                            }
                        }
                        else
                        {
                            receiveResult = new WebSocketReceiveResult(0, WebSocketMessageType.Close, true, CloseStatus, CloseStatusDescription);
                        }

                        tcs.TrySetResult(receiveResult);
                    }
                                                                   );
                    InvokeHelper(action, receiveLoop);
                }
                return(await tcs.Task);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
                tcs.SetException(exc);
                return(new WebSocketReceiveResult(0, WebSocketMessageType.Close, true));
            }
            finally
            {
                ReceiveData -= handler;
            }
        }
Ejemplo n.º 4
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);
            }
        }