Esempio n. 1
0
        /// <summary>
        /// Shake hands with the connecting socket
        /// </summary>
        /// <param name="socket">The socket to send the handshake to</param>
        /// <param name="callback">a callback function that is called when the send has completed</param>
        public void Shake(Socket socket, Action <ClientHandshake> callback)
        {
            _onSuccess = callback;
            try
            {
                var buffer = new byte[_bufferSize];

                socket.AsyncReceive(buffer, (size) =>
                {
                    if (size > 0)
                    {
                        var validShake = IsValidHandshake(buffer, size);
                        if (validShake)
                        {
                            // generate a response for the client
                            var serverShake = GenerateResponseHandshake();
                            // send the handshake to the client
                            BeginSendServerHandshake(serverShake, socket);
                        }
                        else
                        {
                            // the client shake isn't valid
                            Log.Debug("invalid handshake received from " + socket.LocalEndPoint);
                            socket.Close();
                        }
                    }
                });
            }
            catch (Exception e)
            {
                Log.Error("Exception thrown from method Receive:\n" + e.Message);
            }
        }
Esempio n. 2
0
        public void ReceiveAsync(Action <string> callback, DataFrame frame = null)
        {
            var buffer = new byte[256];

            if (frame == null)
            {
                frame = new DataFrame();
            }

            Socket.AsyncReceive(buffer, frame, (sizeOfReceivedData, df) =>
            {
                var dataframe = (DataFrame)df;

                if (sizeOfReceivedData > 0)
                {
                    dataframe.Append(buffer);

                    if (dataframe.IsComplete)
                    {
                        var data = dataframe.ToString();

                        callback(data);
                    }
                    else // end is not is this buffer
                    {
                        ReceiveAsync(callback, dataframe); // continue to read
                    }
                }
            });
        }
Esempio n. 3
0
        public void Receive(DataFrame frame = null)
        {
            if (frame == null)
            {
                frame = new DataFrame();
            }

            var buffer = new byte[BufferSize];

            if (Socket == null || !Socket.Connected)
            {
                WebSocket.Disconnected();
            }

            Socket.AsyncReceive(buffer, frame, (sizeOfReceivedData, df) =>
            {
                var dataframe = (DataFrame)df;

                if (sizeOfReceivedData > 0)
                {
                    dataframe.Append(buffer);

                    if (dataframe.IsComplete)
                    {
                        var data = dataframe.ToString();

                        var model   = CreateModel(data);
                        var isValid = ModelIsValid(model);

                        // if the model was created it must be valid,
                        if (isValid && Factory != null || model == null && Factory == null)
                        {
                            if (model == null && Factory == null) // if the factory is null, use the raw string
                            {
                                model = (object)data;
                            }

                            WebSocket.Incoming(model);
                        }

                        Receive();
                    }
                    else // end is not is this buffer
                    {
                        Receive(dataframe); // continue to read
                    }
                }
                else // no data - the socket must be closed
                {
                    WebSocket.Disconnected();
                    Socket.Close();
                }
            });
        }