| 76 | } |
| 77 | |
| 78 | int Stream::Create(const StreamOptions &options, |
| 79 | const StreamSettings *remote_settings, |
| 80 | StreamId *id, bool parse_rpc_response) { |
| 81 | Stream* s = new Stream(); |
| 82 | s->_host_socket = NULL; |
| 83 | s->_fake_socket_weak_ref = NULL; |
| 84 | s->_connected = false; |
| 85 | s->_options = options; |
| 86 | s->_closed = false; |
| 87 | s->_error_code = 0; |
| 88 | s->_cur_buf_size = options.max_buf_size > 0 ? options.max_buf_size : 0; |
| 89 | if (options.max_buf_size > 0 && options.min_buf_size > options.max_buf_size) { |
| 90 | // set 0 if min_buf_size is invalid. |
| 91 | s->_options.min_buf_size = 0; |
| 92 | LOG(WARNING) << "options.min_buf_size is larger than options.max_buf_size, it will be set to 0."; |
| 93 | } |
| 94 | if (FLAGS_socket_max_streams_unconsumed_bytes > 0 && s->_options.min_buf_size > 0) { |
| 95 | s->_cur_buf_size = s->_options.min_buf_size; |
| 96 | } |
| 97 | |
| 98 | if (remote_settings != NULL) { |
| 99 | s->_remote_settings.MergeFrom(*remote_settings); |
| 100 | } |
| 101 | s->_parse_rpc_response = parse_rpc_response; |
| 102 | if (bthread_id_list_init(&s->_writable_wait_list, 8, 8/*FIXME*/)) { |
| 103 | delete s; |
| 104 | return -1; |
| 105 | } |
| 106 | bthread::ExecutionQueueOptions q_opt; |
| 107 | q_opt.bthread_attr |
| 108 | = FLAGS_usercode_in_pthread ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL; |
| 109 | if (bthread::execution_queue_start(&s->_consumer_queue, &q_opt, Consume, s) != 0) { |
| 110 | LOG(FATAL) << "Fail to create ExecutionQueue"; |
| 111 | delete s; |
| 112 | return -1; |
| 113 | } |
| 114 | SocketOptions sock_opt; |
| 115 | sock_opt.conn = s; |
| 116 | SocketId fake_sock_id; |
| 117 | if (Socket::Create(sock_opt, &fake_sock_id) != 0) { |
| 118 | s->BeforeRecycle(NULL); |
| 119 | return -1; |
| 120 | } |
| 121 | SocketUniquePtr ptr; |
| 122 | CHECK_EQ(0, Socket::Address(fake_sock_id, &ptr)); |
| 123 | s->_fake_socket_weak_ref = ptr.get(); |
| 124 | s->_id = fake_sock_id; |
| 125 | *id = s->id(); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | void Stream::BeforeRecycle(Socket *) { |
| 130 | // No one holds reference now, so we don't need lock here |
nothing calls this directly
no test coverage detected