Skip to content

Commit

Permalink
add const modifier (swoole#3326)
Browse files Browse the repository at this point in the history
* add const modifier

* fix tests
  • Loading branch information
matyhtf authored May 18, 2020
1 parent 404ac0d commit acc0055
Show file tree
Hide file tree
Showing 32 changed files with 77 additions and 78 deletions.
8 changes: 4 additions & 4 deletions include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ typedef struct _swClient

void (*onConnect)(struct _swClient *cli);
void (*onError)(struct _swClient *cli);
void (*onReceive)(struct _swClient *cli, char *data, uint32_t length);
void (*onReceive)(struct _swClient *cli, const char *data, uint32_t length);
void (*onClose)(struct _swClient *cli);
void (*onBufferFull)(struct _swClient *cli);
void (*onBufferEmpty)(struct _swClient *cli);
Expand Down Expand Up @@ -166,12 +166,12 @@ typedef struct _swStream
swString *buffer;
uint8_t cancel;
void *private_data;
void (*response)(struct _swStream *stream, char *data, uint32_t length);
void (*response)(struct _swStream *stream, const char *data, uint32_t length);
swClient client;
} swStream;

swStream* swStream_new(char *dst_host, int dst_port, enum swSocket_type type);
int swStream_send(swStream *stream, char *data, size_t length);
swStream* swStream_new(const char *dst_host, int dst_port, enum swSocket_type type);
int swStream_send(swStream *stream, const char *data, size_t length);
void swStream_set_protocol(swProtocol *protocol);
void swStream_set_max_length(swStream *stream, uint32_t max_length);
int swStream_recv_blocking(swSocket *sock, void *__buf, size_t __len);
Expand Down
4 changes: 2 additions & 2 deletions include/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ int swHttpRequest_has_expect_header(swHttpRequest *request);
#endif

#ifdef SW_USE_HTTP2
ssize_t swHttpMix_get_package_length(swProtocol *protocol, swSocket *conn, char *data, uint32_t length);
ssize_t swHttpMix_get_package_length(swProtocol *protocol, swSocket *conn, const char *data, uint32_t length);
uint8_t swHttpMix_get_package_length_size(swSocket *conn);
int swHttpMix_dispatch_frame(swProtocol *protocol, swSocket *conn, char *data, uint32_t length);
int swHttpMix_dispatch_frame(swProtocol *protocol, swSocket *conn, const char *data, uint32_t length);
#endif

SW_EXTERN_C_END
Expand Down
2 changes: 1 addition & 1 deletion include/http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static sw_inline ssize_t swHttp2_get_length(const char *buf)
return (((uint8_t) buf[0]) << 16) + (((uint8_t) buf[1]) << 8) + (uint8_t) buf[2];
}

ssize_t swHttp2_get_frame_length(swProtocol *protocol, swSocket *conn, char *buf, uint32_t length);
ssize_t swHttp2_get_frame_length(swProtocol *protocol, swSocket *conn, const char *buf, uint32_t length);
int swHttp2_send_setting_frame(swProtocol *protocol, swSocket *conn);
const char* swHttp2_get_type(int type);
int swHttp2_get_type_color(int type);
Expand Down
2 changes: 1 addition & 1 deletion include/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ typedef struct
#define SETQOS(HDR, Q) (HDR | ((Q) << 1))
#define SETDUP(HDR, D) (HDR | ((D) << 3))

ssize_t swMqtt_get_package_length(swProtocol *protocol, swSocket *conn, char *data, uint32_t size);
ssize_t swMqtt_get_package_length(swProtocol *protocol, swSocket *conn, const char *data, uint32_t size);

SW_EXTERN_C_END

Expand Down
4 changes: 2 additions & 2 deletions include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -1061,8 +1061,8 @@ void swReactorThread_set_protocol(swServer *serv, swReactor *reactor);
void swReactorThread_join(swServer *serv);
void swReactorThread_free(swServer *serv);
int swReactorThread_close(swReactor *reactor, swSocket *_socket);
int swReactorThread_dispatch(swProtocol *proto, swSocket *_socket, char *data, uint32_t length);
int swReactorThread_send2worker(swServer *serv, swWorker *worker, void *data, size_t len);
int swReactorThread_dispatch(swProtocol *proto, swSocket *_socket, const char *data, uint32_t length);
int swReactorThread_send2worker(swServer *serv, swWorker *worker, const void *data, size_t len);

int swReactorProcess_create(swServer *serv);
int swReactorProcess_start(swServer *serv);
Expand Down
39 changes: 19 additions & 20 deletions include/swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -844,12 +844,12 @@ typedef struct _swProtocol
uint16_t real_header_length;
uint16_t ext_flags;

int (*onPackage)(struct _swProtocol *, swSocket *, char *, uint32_t);
ssize_t (*get_package_length)(struct _swProtocol *, swSocket *, char *, uint32_t);
int (*onPackage)(struct _swProtocol *, swSocket *, const char *, uint32_t);
ssize_t (*get_package_length)(struct _swProtocol *, swSocket *, const char *, uint32_t);
uint8_t (*get_package_length_size)(swSocket *);
} swProtocol;

typedef ssize_t (*swProtocol_length_function)(struct _swProtocol *, swSocket *, char *, uint32_t);
typedef ssize_t (*swProtocol_length_function)(struct _swProtocol *, swSocket *, const char *, uint32_t);
//------------------------------String--------------------------------
#define swoole_tolower(c) (uchar) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
#define swoole_toupper(c) (uchar) ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)
Expand Down Expand Up @@ -885,10 +885,10 @@ swString *swString_dup(const char *src_str, size_t length);
swString *swString_dup2(swString *src);
int swString_repeat(swString *src, const char *data, size_t len, size_t n);
void swString_print(swString *str);
int swString_append(swString *str, swString *append_str);
int swString_append(swString *str, const swString *append_str);
int swString_append_ptr(swString *str, const char *append_str, size_t length);
int swString_write(swString *str, off_t offset, swString *write_str);
int swString_write_ptr(swString *str, off_t offset, char *write_str, size_t length);
int swString_write_ptr(swString *str, off_t offset, const char *write_str, size_t length);
int swString_extend(swString *str, size_t new_size);
char* swString_alloc(swString *str, size_t __size);

Expand Down Expand Up @@ -1025,7 +1025,7 @@ typedef struct _swDgramPacket
typedef struct _swSendData
{
swDataHead info;
char *data;
const char *data;
} swSendData;

typedef struct
Expand All @@ -1050,7 +1050,7 @@ typedef struct _swPipe
swSocket *worker_socket;

int (*read)(struct _swPipe *, void *recv, int length);
int (*write)(struct _swPipe *, void *send, int length);
int (*write)(struct _swPipe *, const void *send, int length);
swSocket* (*getSocket)(struct _swPipe *, int master);
int (*close)(struct _swPipe *);
} swPipe;
Expand Down Expand Up @@ -1223,7 +1223,7 @@ typedef struct _swShareMemory_mmap
void *mem;
} swShareMemory;

void *swShareMemory_mmap_create(swShareMemory *object, size_t size, char *mapfile);
void *swShareMemory_mmap_create(swShareMemory *object, size_t size, const char *mapfile);
void *swShareMemory_sysv_create(swShareMemory *object, size_t size, int key);
int swShareMemory_sysv_free(swShareMemory *object, int rm);
int swShareMemory_mmap_free(swShareMemory *object);
Expand Down Expand Up @@ -1379,12 +1379,12 @@ const char* swLog_get_file();
void swLog_set_date_with_microseconds(uchar enable);

//----------------------Tool Function---------------------
uint64_t swoole_hash_key(char *str, int str_len);
uint64_t swoole_hash_key(const char *str, int str_len);
uint32_t swoole_common_multiple(uint32_t u, uint32_t v);
uint32_t swoole_common_divisor(uint32_t u, uint32_t v);

extern void swoole_sha1(const char *str, int _len, unsigned char *digest);
extern void swoole_sha256(const char *str, int _len, unsigned char *digest);
extern void swoole_sha1(const char *str, int _len, uchar *digest);
extern void swoole_sha256(const char *str, int _len, uchar *digest);

static sw_inline uint16_t swoole_swap_endian16(uint16_t x)
{
Expand Down Expand Up @@ -1631,7 +1631,6 @@ int swSocket_sendfile_sync(int sock, const char *filename, off_t offset, size_t
ssize_t swSocket_write_blocking(swSocket *sock, const void *__data, size_t __len);
ssize_t swSocket_recv_blocking(swSocket *sock, void *__data, size_t __len, int flags);


static sw_inline int swSocket_error(int err)
{
switch (err)
Expand Down Expand Up @@ -2015,7 +2014,7 @@ struct _swProcessPool
int (*onTask)(struct _swProcessPool *pool, swEventData *task);

void (*onWorkerStart)(struct _swProcessPool *pool, int worker_id);
void (*onMessage)(struct _swProcessPool *pool, char *data, uint32_t length);
void (*onMessage)(struct _swProcessPool *pool, const char *data, uint32_t length);
void (*onWorkerStop)(struct _swProcessPool *pool, int worker_id);

int (*main_loop)(struct _swProcessPool *pool, swWorker *worker);
Expand Down Expand Up @@ -2242,16 +2241,16 @@ int swReactorSelect_create(swReactor *reactor);

/*----------------------------Process Pool-------------------------------*/
int swProcessPool_create(swProcessPool *pool, uint32_t worker_num, key_t msgqueue_key, int ipc_mode);
int swProcessPool_create_unix_socket(swProcessPool *pool, char *socket_file, int blacklog);
int swProcessPool_create_tcp_socket(swProcessPool *pool, char *host, int port, int blacklog);
int swProcessPool_create_unix_socket(swProcessPool *pool, const char *socket_file, int blacklog);
int swProcessPool_create_tcp_socket(swProcessPool *pool, const char *host, int port, int blacklog);
int swProcessPool_set_protocol(swProcessPool *pool, int task_protocol, uint32_t max_packet_size);
void swProcessPool_set_max_request(swProcessPool *pool, uint32_t max_request, uint32_t max_request_grace);
int swProcessPool_wait(swProcessPool *pool);
int swProcessPool_start(swProcessPool *pool);
void swProcessPool_shutdown(swProcessPool *pool);
pid_t swProcessPool_spawn(swProcessPool *pool, swWorker *worker);
int swProcessPool_dispatch(swProcessPool *pool, swEventData *data, int *worker_id);
int swProcessPool_response(swProcessPool *pool, char *data, int length);
int swProcessPool_response(swProcessPool *pool, const char *data, int length);
int swProcessPool_dispatch_blocking(swProcessPool *pool, swEventData *data, int *dst_worker_id);
int swProcessPool_add_worker(swProcessPool *pool, swWorker *worker);
int swProcessPool_del_worker(swProcessPool *pool, swWorker *worker);
Expand Down Expand Up @@ -2317,9 +2316,9 @@ swChannel* swChannel_new(size_t size, size_t maxlen, int flag);
#define swChannel_empty(ch) (ch->num == 0)
#define swChannel_full(ch) ((ch->head == ch->tail && ch->tail_tag != ch->head_tag) || (ch->bytes + sizeof(int) * ch->num == ch->size))
int swChannel_pop(swChannel *object, void *out, int buffer_length);
int swChannel_push(swChannel *object, void *in, int data_length);
int swChannel_push(swChannel *object, const void *in, int data_length);
int swChannel_out(swChannel *object, void *out, int buffer_length);
int swChannel_in(swChannel *object, void *in, int data_length);
int swChannel_in(swChannel *object, const void *in, int data_length);
int swChannel_peek(swChannel *object, void *out, int buffer_length);
int swChannel_wait(swChannel *object);
int swChannel_notify(swChannel *object);
Expand Down Expand Up @@ -2370,13 +2369,13 @@ struct _swThread
swThreadPool *pool;
};

int swThreadPool_dispatch(swThreadPool *pool, void *task, int task_len);
int swThreadPool_dispatch(swThreadPool *pool, const void *task, int task_len);
int swThreadPool_create(swThreadPool *pool, int max_num);
int swThreadPool_run(swThreadPool *pool);
int swThreadPool_free(swThreadPool *pool);

//--------------------------------protocol------------------------------
ssize_t swProtocol_get_package_length(swProtocol *protocol, swSocket *conn, char *data, uint32_t size);
ssize_t swProtocol_get_package_length(swProtocol *protocol, swSocket *conn, const char *data, uint32_t size);
int swProtocol_recv_check_length(swProtocol *protocol, swSocket *conn, swString *buffer);
int swProtocol_recv_check_eof(swProtocol *protocol, swSocket *conn, swString *buffer);

Expand Down
4 changes: 2 additions & 2 deletions include/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ void swWebSocket_decode(swWebSocket_frame *frame, swString *data);
int swWebSocket_pack_close_frame(swString *buffer, int code, char* reason, size_t length, uint8_t flags);
void swWebSocket_print_frame(swWebSocket_frame *frame);

ssize_t swWebSocket_get_package_length(swProtocol *protocol, swSocket *conn, char *data, uint32_t length);
int swWebSocket_dispatch_frame(swProtocol *protocol, swSocket *conn, char *data, uint32_t length);
ssize_t swWebSocket_get_package_length(swProtocol *protocol, swSocket *conn, const char *data, uint32_t length);
int swWebSocket_dispatch_frame(swProtocol *protocol, swSocket *conn, const char *data, uint32_t length);

SW_EXTERN_C_END

Expand Down
4 changes: 2 additions & 2 deletions php_swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ php_socket *swoole_convert_to_socket(int sock);
void swoole_php_socket_free(zval *zsocket);
#endif

ssize_t php_swoole_length_func(swProtocol *protocol, swSocket *_socket, char *data, uint32_t length);
int php_swoole_client_onPackage(swConnection *conn, char *data, uint32_t length);
ssize_t php_swoole_length_func(swProtocol *protocol, swSocket *_socket, const char *data, uint32_t length);
int php_swoole_client_onPackage(swConnection *conn, const char *data, uint32_t length);
zend_bool php_swoole_signal_isset_handler(int signo);

ZEND_BEGIN_MODULE_GLOBALS(swoole)
Expand Down
2 changes: 1 addition & 1 deletion src/core/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pid_t swoole_fork(int flags)
return pid;
}

uint64_t swoole_hash_key(char *str, int str_len)
uint64_t swoole_hash_key(const char *str, int str_len)
{
uint64_t hash = 5381;
int c, i = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/core/channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ swChannel* swChannel_new(size_t size, size_t maxlen, int flags)
/**
* push data(no lock)
*/
int swChannel_in(swChannel *object, void *in, int data_length)
int swChannel_in(swChannel *object, const void *in, int data_length)
{
assert(data_length <= object->maxlen);
if (swChannel_full(object))
Expand Down Expand Up @@ -190,7 +190,7 @@ int swChannel_notify(swChannel *object)
/**
* push data (lock)
*/
int swChannel_push(swChannel *object, void *in, int data_length)
int swChannel_push(swChannel *object, const void *in, int data_length)
{
assert(object->flag & SW_CHAN_LOCK);
object->lock.lock(&object->lock);
Expand Down
4 changes: 2 additions & 2 deletions src/core/string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ swString *swString_dup(const char *src_str, size_t length)
return str;
}

int swString_append(swString *str, swString *append_str)
int swString_append(swString *str, const swString *append_str)
{
size_t new_size = str->length + append_str->length;
if (new_size > str->size)
Expand Down Expand Up @@ -147,7 +147,7 @@ int swString_write(swString *str, off_t offset, swString *write_str)
return SW_OK;
}

int swString_write_ptr(swString *str, off_t offset, char *write_str, size_t length)
int swString_write_ptr(swString *str, off_t offset, const char *write_str, size_t length)
{
size_t new_length = offset + length;
if (new_length > str->size)
Expand Down
2 changes: 1 addition & 1 deletion src/memory/shared_memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void* sw_shm_realloc(void *ptr, size_t new_size)
}
}

void *swShareMemory_mmap_create(swShareMemory *object, size_t size, char *mapfile)
void *swShareMemory_mmap_create(swShareMemory *object, size_t size, const char *mapfile)
{
void *mem;
int tmpfd = -1;
Expand Down
4 changes: 2 additions & 2 deletions src/network/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static int swClient_onWrite(swReactor *reactor, swEvent *event);
static int swClient_onError(swReactor *reactor, swEvent *event);
static void swClient_onTimeout(swTimer *timer, swTimer_node *tnode);
static void swClient_onResolveCompleted(swAio_event *event);
static int swClient_onPackage(swProtocol *proto, swSocket *conn, char *data, uint32_t length);
static int swClient_onPackage(swProtocol *proto, swSocket *conn, const char *data, uint32_t length);

static sw_inline void execute_onConnect(swClient *cli)
{
Expand Down Expand Up @@ -1068,7 +1068,7 @@ static int swClient_https_proxy_handshake(swClient *cli)
}
#endif

static int swClient_onPackage(swProtocol *proto, swSocket *conn, char *data, uint32_t length)
static int swClient_onPackage(swProtocol *proto, swSocket *conn, const char *data, uint32_t length)
{
swClient *cli = (swClient *) conn->object;
cli->onReceive(cli, data, length);
Expand Down
6 changes: 3 additions & 3 deletions src/network/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static void swStream_onError(swClient *cli)
swStream_free((swStream *) cli->object);
}

static void swStream_onReceive(swClient *cli, char *data, uint32_t length)
static void swStream_onReceive(swClient *cli, const char *data, uint32_t length)
{
swStream *stream = (swStream*) cli->object;
if (length == 4)
Expand Down Expand Up @@ -76,7 +76,7 @@ static void swStream_free(swStream *stream)
sw_free(stream);
}

swStream* swStream_new(char *dst_host, int dst_port, enum swSocket_type type)
swStream* swStream_new(const char *dst_host, int dst_port, enum swSocket_type type)
{
swStream *stream = (swStream*) sw_malloc(sizeof(swStream));
if (!stream)
Expand Down Expand Up @@ -129,7 +129,7 @@ void swStream_set_max_length(swStream *stream, uint32_t max_length)
stream->client.protocol.package_max_length = max_length;
}

int swStream_send(swStream *stream, char *data, size_t length)
int swStream_send(swStream *stream, const char *data, size_t length)
{
if (stream->buffer == NULL)
{
Expand Down
6 changes: 3 additions & 3 deletions src/os/process_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ int swProcessPool_create(swProcessPool *pool, uint32_t worker_num, key_t msgqueu
return SW_OK;
}

int swProcessPool_create_unix_socket(swProcessPool *pool, char *socket_file, int blacklog)
int swProcessPool_create_unix_socket(swProcessPool *pool, const char *socket_file, int blacklog)
{
if (pool->ipc_mode != SW_IPC_SOCKET)
{
Expand All @@ -175,7 +175,7 @@ int swProcessPool_create_unix_socket(swProcessPool *pool, char *socket_file, int
return SW_OK;
}

int swProcessPool_create_tcp_socket(swProcessPool *pool, char *host, int port, int blacklog)
int swProcessPool_create_tcp_socket(swProcessPool *pool, const char *host, int port, int blacklog)
{
if (pool->ipc_mode != SW_IPC_SOCKET)
{
Expand Down Expand Up @@ -247,7 +247,7 @@ static sw_inline int swProcessPool_schedule(swProcessPool *pool)
return target_worker_id;
}

int swProcessPool_response(swProcessPool *pool, char *data, int length)
int swProcessPool_response(swProcessPool *pool, const char *data, int length)
{
if (pool->stream == nullptr || pool->stream->last_connection == nullptr || pool->stream->response_buffer == nullptr)
{
Expand Down
4 changes: 2 additions & 2 deletions src/os/thread_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ int swThreadPool_create(swThreadPool *pool, int thread_num)
return SW_OK;
}

int swThreadPool_dispatch(swThreadPool *pool, void *task, int task_len)
int swThreadPool_dispatch(swThreadPool *pool, const void *task, int task_len)
{
int ret;

pool->cond.lock(&pool->cond);
#ifdef SW_THREADPOOL_USE_CHANNEL
ret = swChannel_in(pool->chan, task, task_len);
#else
ret = swRingQueue_push(&pool->queue, task);
ret = swRingQueue_push(&pool->queue, (char*) task);
#endif
pool->cond.unlock(&pool->cond);

Expand Down
4 changes: 2 additions & 2 deletions src/pipe/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "swoole.h"

static int swPipeBase_read(swPipe *p, void *data, int length);
static int swPipeBase_write(swPipe *p, void *data, int length);
static int swPipeBase_write(swPipe *p, const void *data, int length);
static int swPipeBase_close(swPipe *p);

typedef struct _swPipeBase
Expand Down Expand Up @@ -108,7 +108,7 @@ static int swPipeBase_read(swPipe *p, void *data, int length)
return read(object->pipes[0], data, length);
}

static int swPipeBase_write(swPipe *p, void *data, int length)
static int swPipeBase_write(swPipe *p, const void *data, int length)
{
swPipeBase *object = (swPipeBase *) p->object;
return write(object->pipes[1], data, length);
Expand Down
Loading

0 comments on commit acc0055

Please sign in to comment.