|
74 | 74 | worker3: r++r++r++w++w++w rrrww+w
|
75 | 75 | server: ++++++
|
76 | 76 |
|
| 77 | +----------------------------------------------------------- |
| 78 | +
|
| 79 | +
|
| 80 | +Blocking i/o: |
| 81 | +------------- |
| 82 | +
|
| 83 | +Server: |
| 84 | +
|
| 85 | + results = [] |
| 86 | + stream_socket_server |
| 87 | + while ( unfinished jobs ) |
| 88 | + stream_socket_accept |
| 89 | + while ( got data ) |
| 90 | + stream_socket_recvfrom |
| 91 | + results[] = data |
| 92 | + stream_socket_sendto |
| 93 | + fclose |
| 94 | +
|
| 95 | +Worker: |
| 96 | +
|
| 97 | + stream_socket_client |
| 98 | + stream_socket_sendto [register worker with server] |
| 99 | + stream_socket_recvfrom [get first job from server] |
| 100 | + while ( got job ) |
| 101 | + stream_socket_sendto [send result] |
| 102 | + stream_socket_recvfrom [get another job] |
| 103 | +
|
| 104 | +
|
| 105 | +Non-blocking i/o: |
| 106 | +----------------- |
| 107 | +
|
| 108 | +Server: |
| 109 | +
|
| 110 | + clients = [] |
| 111 | + writeBufferPerClient = [] |
| 112 | + readBufferPerClient = [] |
| 113 | + stream_socket_server |
| 114 | + stream_set_blocking( server socket not blocking ) |
| 115 | + while ( unfinished jobs ) |
| 116 | + stream_select( readable: clients + server, writable: clients ) |
| 117 | + if ( server is readable ) |
| 118 | + clients[] = stream_socket_accept |
| 119 | + stream_set_blocking( client socket not blocking ) |
| 120 | + foreach ( clients[] ) |
| 121 | + if ( is readable ) |
| 122 | + while ( got data, which means client socket is open ) |
| 123 | + stream_socket_recvfrom |
| 124 | + readBufferPerClient[] .= data |
| 125 | + if ( no received data ) |
| 126 | + results[] = Request(readBuffer)->getBody/Headers() |
| 127 | + if ( is writable ) |
| 128 | + while ( didn't write all bytes of writeBufferPerClient[current] yet ) |
| 129 | + stream_socket_sendto |
| 130 | + if ( writeBufferPerClient[current] is empty ) |
| 131 | + fclose |
| 132 | + unset( clients[current] ) |
| 133 | +
|
| 134 | +Worker: |
| 135 | +
|
| 136 | + initialized = false |
| 137 | + jobQueue = [] |
| 138 | + resultsQueue = [] |
| 139 | + readBuffer = '' |
| 140 | + writeBuffer = '' |
| 141 | + stream_socket_client |
| 142 | + stream_set_blocking( client socket not blocking ) |
| 143 | + while ( true ) |
| 144 | + stream_select( readable: server, writable: server ) |
| 145 | + if ( server is writable ) |
| 146 | + if ( !initialized ) |
| 147 | + writeBuffer = I'm a new worker, register me |
| 148 | + else |
| 149 | + writeBuffer = here's a job result resultsQueue.pop(), gimme another job |
| 150 | + while ( didn't write all bytes of writeBuffer yet ) |
| 151 | + stream_socket_sendto |
| 152 | + if ( writeBuffer is empty ) |
| 153 | + fclose |
| 154 | + if ( server is readable ) |
| 155 | + while ( got data, which means client socket is open ) |
| 156 | + stream_socket_recvfrom |
| 157 | + readBuffer .= data |
| 158 | + if ( no received data ) |
| 159 | + jobQueue[] = Request(readBuffer)->getBody/Headers() |
| 160 | +
|
77 | 161 |
|
78 | 162 | */
|
79 | 163 |
|
|
0 commit comments