summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Sustrik <sustrik@250bpm.com>2014-11-24 22:20:55 +0100
committerMartin Sustrik <sustrik@250bpm.com>2014-11-24 22:20:55 +0100
commit70753b60dffe2db998f9d0054f9cefa6b189650a (patch)
tree8c72101108603e7250ed0411409296974a8a3ffc
parenta0caf6f0c1e56a6b46222ae1b177c528cbbbe424 (diff)
downloadnanomsg-70753b60dffe2db998f9d0054f9cefa6b189650a.tar.gz
streamhdr removed from ws transport
Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
-rw-r--r--src/transports/ws/masker.c43
-rw-r--r--src/transports/ws/masker.h37
-rw-r--r--src/transports/ws/sws.c110
-rw-r--r--src/transports/ws/sws.h5
4 files changed, 102 insertions, 93 deletions
diff --git a/src/transports/ws/masker.c b/src/transports/ws/masker.c
new file mode 100644
index 0000000..08fd467
--- /dev/null
+++ b/src/transports/ws/masker.c
@@ -0,0 +1,43 @@
+/*
+ Copyright (c) 2014 Wirebird Labs LLC. All rights reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom
+ the Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ IN THE SOFTWARE.
+*/
+
+#include "masker.h"
+
+#include <stddef.h>
+#include <string.h>
+
+void nn_masker_init (struct nn_masker *self, void *mask)
+{
+ memcpy (self->mask, mask, 4);
+ self->offset = 0;
+}
+
+void nn_masker_mask (struct nn_masker *self, void *buf, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i != len; ++i) {
+ ((unsigned char*) buf) [i] ^= self->mask [self->offset % 4];
+ ++self->offset;
+ }
+}
+
diff --git a/src/transports/ws/masker.h b/src/transports/ws/masker.h
new file mode 100644
index 0000000..829dd37
--- /dev/null
+++ b/src/transports/ws/masker.h
@@ -0,0 +1,37 @@
+/*
+ Copyright (c) 2014 Wirebird Labs LLC. All rights reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom
+ the Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ IN THE SOFTWARE.
+*/
+
+#ifndef NN_MASKER_INCLUDED
+#define NN_MASKER_INCLUDED
+
+#include <stddef.h>
+
+struct nn_masker {
+ unsigned char mask [4];
+ unsigned char offset;
+};
+
+void nn_masker_init (struct nn_masker *self, void *mask);
+void nn_masker_mask (struct nn_masker *self, void *buf, size_t len);
+
+#endif
+
diff --git a/src/transports/ws/sws.c b/src/transports/ws/sws.c
index 1ca18ac..5d6004e 100644
--- a/src/transports/ws/sws.c
+++ b/src/transports/ws/sws.c
@@ -33,12 +33,9 @@
/* States of the object as a whole. */
#define NN_SWS_STATE_IDLE 1
-#define NN_SWS_STATE_PROTOHDR 2
-#define NN_SWS_STATE_STOPPING_STREAMHDR 3
-#define NN_SWS_STATE_ACTIVE 4
-#define NN_SWS_STATE_SHUTTING_DOWN 5
-#define NN_SWS_STATE_DONE 6
-#define NN_SWS_STATE_STOPPING 7
+#define NN_SWS_STATE_ACTIVE 2
+#define NN_SWS_STATE_SHUTTING_DOWN 3
+#define NN_SWS_STATE_DONE 4
/* Possible states of the inbound part of the object. */
#define NN_SWS_INSTATE_HDR 1
@@ -52,7 +49,6 @@
/* Subordinate srcptr objects. */
#define NN_SWS_SRC_USOCK 1
-#define NN_SWS_SRC_STREAMHDR 2
/* Constants to compose first byte of WebSocket message header from. */
#define NN_SWS_FIN 0x80
@@ -88,7 +84,6 @@ void nn_sws_init (struct nn_sws *self, int src,
nn_fsm_init (&self->fsm, nn_sws_handler, nn_sws_shutdown,
src, self, owner);
self->state = NN_SWS_STATE_IDLE;
- nn_streamhdr_init (&self->streamhdr, NN_SWS_SRC_STREAMHDR, &self->fsm);
self->usock = NULL;
self->usock_owner.src = -1;
self->usock_owner.fsm = NULL;
@@ -108,7 +103,6 @@ void nn_sws_term (struct nn_sws *self)
nn_msg_term (&self->outmsg);
nn_msg_term (&self->inmsg);
nn_pipebase_term (&self->pipebase);
- nn_streamhdr_term (&self->streamhdr);
nn_fsm_term (&self->fsm);
}
@@ -240,19 +234,12 @@ static void nn_sws_shutdown (struct nn_fsm *self, int src, int type,
if (nn_slow (src == NN_FSM_ACTION && type == NN_FSM_STOP)) {
nn_pipebase_stop (&sws->pipebase);
- nn_streamhdr_stop (&sws->streamhdr);
- sws->state = NN_SWS_STATE_STOPPING;
- }
- if (nn_slow (sws->state == NN_SWS_STATE_STOPPING)) {
- if (nn_streamhdr_isidle (&sws->streamhdr)) {
- nn_usock_swap_owner (sws->usock, &sws->usock_owner);
- sws->usock = NULL;
- sws->usock_owner.src = -1;
- sws->usock_owner.fsm = NULL;
- sws->state = NN_SWS_STATE_IDLE;
- nn_fsm_stopped (&sws->fsm, NN_SWS_STOPPED);
- return;
- }
+ nn_usock_swap_owner (sws->usock, &sws->usock_owner);
+ sws->usock = NULL;
+ sws->usock_owner.src = -1;
+ sws->usock_owner.fsm = NULL;
+ sws->state = NN_SWS_STATE_IDLE;
+ nn_fsm_stopped (&sws->fsm, NN_SWS_STOPPED);
return;
}
@@ -279,40 +266,23 @@ static void nn_sws_handler (struct nn_fsm *self, int src, int type,
case NN_FSM_ACTION:
switch (type) {
case NN_FSM_START:
- nn_streamhdr_start (&sws->streamhdr, sws->usock,
- &sws->pipebase);
- sws->state = NN_SWS_STATE_PROTOHDR;
- return;
- default:
- nn_fsm_bad_action (sws->state, src, type);
- }
-
- default:
- nn_fsm_bad_source (sws->state, src, type);
- }
-/******************************************************************************/
-/* PROTOHDR state. */
-/******************************************************************************/
- case NN_SWS_STATE_PROTOHDR:
- switch (src) {
-
- case NN_SWS_SRC_STREAMHDR:
- switch (type) {
- case NN_STREAMHDR_OK:
+ /* Start the pipe. */
+ rc = nn_pipebase_start (&sws->pipebase);
+ if (nn_slow (rc < 0)) {
+ sws->state = NN_SWS_STATE_DONE;
+ nn_fsm_raise (&sws->fsm, &sws->done, NN_SWS_ERROR);
+ return;
+ }
- /* Before moving to the active state stop the streamhdr
- state machine. */
- nn_streamhdr_stop (&sws->streamhdr);
- sws->state = NN_SWS_STATE_STOPPING_STREAMHDR;
- return;
+ /* Start receiving a message in asynchronous manner. */
+ sws->instate = NN_SWS_INSTATE_HDR;
+ nn_usock_recv (sws->usock, &sws->inhdr, 2, NULL);
- case NN_STREAMHDR_ERROR:
+ /* Mark the pipe as available for sending. */
+ sws->outstate = NN_SWS_OUTSTATE_IDLE;
- /* Raise the error and move directly to the DONE state.
- streamhdr object will be stopped later on. */
- sws->state = NN_SWS_STATE_DONE;
- nn_fsm_raise (&sws->fsm, &sws->done, NN_SWS_ERROR);
+ sws->state = NN_SWS_STATE_ACTIVE;
return;
default:
@@ -324,42 +294,6 @@ static void nn_sws_handler (struct nn_fsm *self, int src, int type,
}
/******************************************************************************/
-/* STOPPING_STREAMHDR state. */
-/******************************************************************************/
- case NN_SWS_STATE_STOPPING_STREAMHDR:
- switch (src) {
-
- case NN_SWS_SRC_STREAMHDR:
- switch (type) {
- case NN_STREAMHDR_STOPPED:
-
- /* Start the pipe. */
- rc = nn_pipebase_start (&sws->pipebase);
- if (nn_slow (rc < 0)) {
- sws->state = NN_SWS_STATE_DONE;
- nn_fsm_raise (&sws->fsm, &sws->done, NN_SWS_ERROR);
- return;
- }
-
- /* Start receiving a message in asynchronous manner. */
- sws->instate = NN_SWS_INSTATE_HDR;
- nn_usock_recv (sws->usock, &sws->inhdr, 2, NULL);
-
- /* Mark the pipe as available for sending. */
- sws->outstate = NN_SWS_OUTSTATE_IDLE;
-
- sws->state = NN_SWS_STATE_ACTIVE;
- return;
-
- default:
- nn_fsm_bad_action (sws->state, src, type);
- }
-
- default:
- nn_fsm_bad_source (sws->state, src, type);
- }
-
-/******************************************************************************/
/* ACTIVE state. */
/******************************************************************************/
case NN_SWS_STATE_ACTIVE:
diff --git a/src/transports/ws/sws.h b/src/transports/ws/sws.h
index ad3140c..b4b5cad 100644
--- a/src/transports/ws/sws.h
+++ b/src/transports/ws/sws.h
@@ -29,8 +29,6 @@
#include "../../aio/fsm.h"
#include "../../aio/usock.h"
-#include "../utils/streamhdr.h"
-
#include "../../utils/msg.h"
#include "masker.h"
@@ -56,9 +54,6 @@ struct nn_sws {
/* The underlying socket. */
struct nn_usock *usock;
- /* Child state machine to do protocol header exchange. */
- struct nn_streamhdr streamhdr;
-
/* The original owner of the underlying socket. */
struct nn_fsm_owner usock_owner;