Esempio n. 1
0
            public object read(CodeContext /*!*/ context, int len, ByteArray buffer = null)
            {
                EnsureSslStream(true);

                try {
                    byte[]       buf    = new byte[2048];
                    MemoryStream result = new MemoryStream(len);
                    while (true)
                    {
                        int readLength = (len < buf.Length) ? len : buf.Length;
                        int bytes      = _sslStream.Read(buf, 0, readLength);
                        if (bytes > 0)
                        {
                            result.Write(buf, 0, bytes);
                            len -= bytes;
                        }

                        if (bytes == 0 || len == 0 || bytes < readLength)
                        {
                            var res = result.ToArray();
                            if (buffer == null)
                            {
                                return(Bytes.Make(res));
                            }

                            // TODO: get rid of the MemoryStream and write directly to the buffer
                            buffer[new Slice(0, res.Length)] = res;
                            return(res.Length);
                        }
                    }
                } catch (Exception e) {
                    throw PythonSocket.MakeException(context, e);
                }
            }
Esempio n. 2
0
            public int write(CodeContext /*!*/ context, Bytes data)
            {
                EnsureSslStream(true);

                byte[] buffer = data.UnsafeByteArray;
                try {
                    _sslStream.Write(buffer);
                    return(buffer.Length);
                } catch (Exception e) {
                    throw PythonSocket.MakeException(context, e);
                }
            }