<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # C++開發者如何使用Swoole [TOC] PHP編寫的Server程序在某些情況下表現會較差 * 內存占用敏感的場景,PHP底層使用內存結構`zval`來管理所有變量,會額外占用內存,如一個int32的整數可能需要占用16(PHP7)或24字節(PHP5)的內存,而C/C++只需要4字節。如果系統需要存儲大量整數,占用的內存會非常大。 * PHP是動態解釋執行的,計算性能較差,純運算的代碼可能會比C/C++程序差幾十甚至上百倍。此類場景下不適合使用PHP C/C++的支持彌補了這些不足,在上述場景下可以使用`c-swoole`或者`cpp-swoole`來編寫Server程序。 `cpp-swoole`是對`c-swoole`的面向對象封裝,支持了絕大部分swoole\_server的特性包括task功能,另外還支持高精度定時器特性。 `cpp-swoole`依賴`libswoole.so`,需要先編譯`c-swoole`生成`libswoole.so` ## 編譯libswoole. so ~~~ git clone https://github.com/swoole/swoole-src.git phpize ./configure cmake . #cmake -DCMAKE_INSTALL_PREFIX=/opt/swoole . sudo make install ~~~ 編譯安裝好`libswoole.so`后就可以下載`cpp-swoole`源碼,編譯`libswoole_cpp.so` ## 編譯libswoole\_cpp.so ~~~ git clone https://github.com/swoole/cpp-swoole.git cmake . make sudo make install ~~~ ## 編寫程序 頭文件: ~~~ #include <swoole/Server.hpp> #include <swoole/Timer.hpp> ~~~ 服務器程序只需要繼承`swoole::Server`,并實現響應的回調函數即可。 ~~~ #include <swoole/Server.hpp> #include <swoole/Timer.hpp> #include <iostream> using namespace std; using namespace swoole; class MyServer : public Server { public: MyServer(string _host, int _port, int _mode = SW_MODE_PROCESS, int _type = SW_SOCK_TCP) : Server(_host, _port, _mode, _type) { serv.worker_num = 4; SwooleG.task_worker_num = 2; } virtual void onStart(); virtual void onShutdown() {}; virtual void onWorkerStart(int worker_id) {} virtual void onWorkerStop(int worker_id) {} virtual void onPipeMessage(int src_worker_id, const DataBuffer &) {} virtual void onReceive(int fd, const DataBuffer &data); virtual void onConnect(int fd); virtual void onClose(int fd); virtual void onPacket(const DataBuffer &data, ClientInfo &clientInfo) {}; virtual void onTask(int task_id, int src_worker_id, const DataBuffer &data); virtual void onFinish(int task_id, const DataBuffer &data); }; void MyServer::onReceive(int fd, const DataBuffer &data) { swConnection *conn = swWorker_get_connection(&this->serv, fd); printf("onReceive: fd=%d, ip=%s|port=%d Data=%s|Len=%ld\n", fd, swConnection_get_ip(conn), swConnection_get_port(conn), (char *) data.buffer, data.length); int ret; char resp_data[SW_BUFFER_SIZE]; int n = snprintf(resp_data, SW_BUFFER_SIZE, (char *) "Server: %*s\n", (int) data.length, (char *) data.buffer); ret = this->send(fd, resp_data, (uint32_t) n); if (ret < 0) { printf("send to client fail. errno=%d\n", errno); } else { printf("send %d bytes to client success. data=%s\n", n, resp_data); } DataBuffer task_data("hello world\n"); this->task(task_data); // this->close(fd); } void MyServer::onConnect(int fd) { printf("PID=%d\tConnect fd=%d\n", getpid(), fd); } void MyServer::onClose(int fd) { printf("PID=%d\tClose fd=%d\n", getpid(), fd); } void MyServer::onTask(int task_id, int src_worker_id, const DataBuffer &data) { printf("PID=%d\tTaskID=%d\n", getpid(), task_id); } void MyServer::onFinish(int task_id, const DataBuffer &data) { printf("PID=%d\tClose fd=%d\n", getpid(), task_id); } void MyServer::onStart() { printf("server start\n"); } class MyTimer : Timer { public: MyTimer(long ms, bool interval) : Timer(ms, interval) { } MyTimer(long ms) : Timer(ms) { } protected: virtual void callback(void); int count = 0; }; void MyTimer::callback() { printf("#%d\thello world\n", count); if (count > 9) { this->clear(); } count++; } int main(int argc, char **argv) { MyServer server("127.0.0.1", 9501, SW_MODE_SINGLE); server.listen("127.0.0.1", 9502, SW_SOCK_UDP); server.listen("::1", 9503, SW_SOCK_TCP6); server.listen("::1", 9504, SW_SOCK_UDP6); server.setEvents(EVENT_onStart | EVENT_onReceive | EVENT_onClose | EVENT_onTask | EVENT_onFinish); server.start(); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看