Skip to content

Commit

Permalink
Make some parameters const.
Browse files Browse the repository at this point in the history
  • Loading branch information
gatzka committed Oct 9, 2016
1 parent 7aa2a6a commit 234485e
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/peer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct peer {
struct list_head fetch_list;
void *routing_table;
char *name;
int (*send_message)(struct peer *p, char *rendered, size_t len);
int (*send_message)(const struct peer *p, char *rendered, size_t len);
void (*close)(struct peer *p);
bool is_local_connection;
};
Expand Down
6 changes: 3 additions & 3 deletions src/socket_peer.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ static enum bs_read_callback_return read_msg_length(void *context, uint8_t *buf,
return BS_OK;
}

static int send_message(struct peer *p, char *rendered, size_t len)
static int send_message(const struct peer *p, char *rendered, size_t len)
{
uint32_t message_length = htonl(len);
struct buffered_socket_io_vector iov[2];
iov[0].iov_base = &message_length;
iov[0].iov_len = sizeof(message_length);
iov[1].iov_base = rendered;
iov[1].iov_len = len;
struct socket_peer *s_peer = container_of(p, struct socket_peer, peer);
const struct socket_peer *s_peer = const_container_of(p, struct socket_peer, peer);

struct buffered_reader *br = &s_peer->br;
const struct buffered_reader *br = &s_peer->br;
return br->writev(br->this_ptr, iov, ARRAY_SIZE(iov));
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/combined_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ static struct state_or_method *get_state(const char *path)
return (struct state_or_method *)state_table_get(path);
}

int send_message(struct peer *p, char *rendered, size_t len)
int send_message(const struct peer *p, char *rendered, size_t len)
{
(void)len;
if (p == fetch_peer_1) {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/fetch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ static void check_invalid_params(const cJSON *error)
}
}

int send_message(struct peer *p, char *rendered, size_t len)
int send_message(const struct peer *p, char *rendered, size_t len)
{
(void)len;
if (p == fetch_peer_1) {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
static char readback_buffer[10000];

extern "C" {
int send_message(struct peer *p, char *rendered, size_t len)
int send_message(const struct peer *p, char *rendered, size_t len)
{
(void)p;
char *ptr = readback_buffer;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/method_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static const char *method_no_args_path = "/method_no_args/";

static const int INVALID_PARAMS_ERROR = -32602;

int send_message(struct peer *p, char *rendered, size_t len)
int send_message(const struct peer *p, char *rendered, size_t len)
{
(void)p;
(void)rendered;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/parse_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static cJSON *parse_send_buffer(const char *json)
return root;
}

int send_message(struct peer *p, char *rendered, size_t len)
int send_message(const struct peer *p, char *rendered, size_t len)
{
(void)len;
(void)p;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/state_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

static char send_buffer[100000];

int send_message(struct peer *p, char *rendered, size_t len)
int send_message(const struct peer *p, char *rendered, size_t len)
{
(void)p;
memcpy(send_buffer, rendered, len);
Expand Down
4 changes: 4 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@
#define container_of(ptr, type, member) ( \
(void *)((char *)ptr - offsetof(type,member) ))

#define const_container_of(ptr, type, member) ( \
(const void *)((const char *)ptr - offsetof(type,member) ))


#endif
12 changes: 6 additions & 6 deletions src/websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ int websocket_upgrade_on_headers_complete(http_parser *parser)
}
}

static int send_frame(struct websocket *s, uint8_t *payload, size_t length, unsigned int type)
static int send_frame(const struct websocket *s, uint8_t *payload, size_t length, unsigned int type)
{
char ws_header[14];
uint8_t first_len;
Expand Down Expand Up @@ -571,27 +571,27 @@ static int send_frame(struct websocket *s, uint8_t *payload, size_t length, unsi
return br->writev(br->this_ptr, iov, ARRAY_SIZE(iov));
}

int websocket_send_binary_frame(struct websocket *s, uint8_t *payload, size_t length)
int websocket_send_binary_frame(const struct websocket *s, uint8_t *payload, size_t length)
{
return send_frame(s, payload, length, WS_BINARY_FRAME);
}

int websocket_send_text_frame(struct websocket *s, char *payload, size_t length)
int websocket_send_text_frame(const struct websocket *s, char *payload, size_t length)
{
return send_frame(s, (uint8_t *)payload, length, WS_TEXT_FRAME);
}

int websocket_send_ping_frame(struct websocket *s, uint8_t *payload, size_t length)
int websocket_send_ping_frame(const struct websocket *s, uint8_t *payload, size_t length)
{
return send_frame(s, payload, length, WS_PING_FRAME);
}

int websocket_send_pong_frame(struct websocket *s, uint8_t *payload, size_t length)
int websocket_send_pong_frame(const struct websocket *s, uint8_t *payload, size_t length)
{
return send_frame(s, payload, length, WS_PONG_FRAME);
}

int websocket_send_close_frame(struct websocket *s, enum ws_status_code status_code, const char *payload, size_t length)
int websocket_send_close_frame(const struct websocket *s, enum ws_status_code status_code, const char *payload, size_t length)
{
uint16_t code = status_code;
uint8_t buffer[length + sizeof(code)];
Expand Down
10 changes: 5 additions & 5 deletions src/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ enum bs_read_callback_return ws_get_header(void *context, uint8_t *buf, size_t l
int websocket_upgrade_on_header_field(http_parser *p, const char *at, size_t length);
int websocket_upgrade_on_headers_complete(http_parser *parser);
int websocket_upgrade_on_header_value(http_parser *p, const char *at, size_t length);
int websocket_send_text_frame(struct websocket *s, char *payload, size_t length);
int websocket_send_binary_frame(struct websocket *s, uint8_t *payload, size_t length);
int websocket_send_close_frame(struct websocket *s, enum ws_status_code status_code, const char *payload, size_t length);
int websocket_send_ping_frame(struct websocket *s, uint8_t *payload, size_t length);
int websocket_send_pong_frame(struct websocket *s, uint8_t *payload, size_t length);
int websocket_send_text_frame(const struct websocket *s, char *payload, size_t length);
int websocket_send_binary_frame(const struct websocket *s, uint8_t *payload, size_t length);
int websocket_send_close_frame(const struct websocket *s, enum ws_status_code status_code, const char *payload, size_t length);
int websocket_send_ping_frame(const struct websocket *s, uint8_t *payload, size_t length);
int websocket_send_pong_frame(const struct websocket *s, uint8_t *payload, size_t length);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions src/websocket_peer.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
#define WS_PING_FRAME 0x9
#define WS_PONG_FRAME 0x0a

static int ws_send_message(struct peer *p, char *rendered, size_t len)
static int ws_send_message(const struct peer *p, char *rendered, size_t len)
{
struct websocket_peer *ws_peer = container_of(p, struct websocket_peer, peer);
const struct websocket_peer *ws_peer = const_container_of(p, struct websocket_peer, peer);
return websocket_send_text_frame(&ws_peer->websocket, rendered, len);
}

Expand Down

0 comments on commit 234485e

Please sign in to comment.