Skip to content

Commit

Permalink
implement a prototype for the server side
Browse files Browse the repository at this point in the history
  • Loading branch information
TTimo committed Apr 5, 2014
1 parent 59c3e32 commit 8830bb3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/nn.h
Expand Up @@ -166,6 +166,9 @@ extern "C" {
#define EISCONN (NN_HAUSNUMERO + 27)
#define NN_EISCONN_DEFINED
#endif
#ifndef ESOCKTNOSUPPORT
#define ESOCKTNOSUPPORT (NN_HAUSNUMERO + 28)
#endif

/* Native nanomsg error codes. */
#ifndef ETERM
Expand Down
64 changes: 63 additions & 1 deletion src/transports/ipc/bipc.c
Expand Up @@ -282,7 +282,69 @@ static void nn_bipc_handler (struct nn_fsm *self, int src, int type,

static void nn_bipc_start_listening (struct nn_bipc *self)
{
#if !defined NN_HAVE_WINDOWS
#if defined NN_HAVE_WINDOWS
const char *addr;
const char *win_name;

// TODO: transform the name
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa365783(v=vs.85).aspx
addr = nn_epbase_getaddr (&self->epbase);
win_name = "\\\\.\\pipe\\test.ipc";

// nn_usock_start replacement:

{
HANDLE instance;
HANDLE cp;
struct nn_worker *worker;

// TODO: expose custom nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes
// NOTE: FILE_FLAG_OVERLAPPED + PIPE_WAIT: http://blogs.msdn.com/b/oldnewthing/archive/2011/01/14/10115610.aspx?Redirected=true
instance = CreateNamedPipeA ( win_name, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, NULL );
// TODO: error reporting
nn_assert (instance != INVALID_HANDLE_VALUE);
// FIXME: abuse. Since SOCKETs are HANDLEs, should we change the type to a HANDLE instead?
self->usock.s = (SOCKET)instance;

/* Associate the socket with a worker thread/completion port. */
worker = nn_fsm_choose_worker (&self->usock.fsm);
cp = CreateIoCompletionPort ((HANDLE) self->usock.s,
nn_worker_getcp (worker), (ULONG_PTR) NULL, 0);
nn_assert (cp);

// TODO: should something else be setup here?
self->usock.domain = -1;
self->usock.type = -1;
self->usock.protocol = -1;

/* Start the state machine. */
nn_fsm_start (&self->usock.fsm);
}

// nn_usock_bind: no equivalent

// nn_usock_listen replacement:
{
OVERLAPPED olpd;
BOOL connect_ret;
DWORD err;

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa365146(v=vs.85).aspx
// NOTE: not setting up a 'manual reset' event
memset (&olpd, 0, sizeof(OVERLAPPED));
connect_ret = ConnectNamedPipe ((HANDLE) self->usock.s, &olpd);
nn_assert (connect_ret == FALSE); // Asynchronous: always returns 0
err = GetLastError();
// NOTE: ERROR_PIPE_CONNECTED is a rare edge case situation
nn_assert (err == ERROR_IO_PENDING || err == ERROR_PIPE_CONNECTED); // Success

/* Notify the state machine. */
#define NN_USOCK_ACTION_LISTEN 4
nn_fsm_action (&self->usock.fsm, NN_USOCK_ACTION_LISTEN);

}

#else
int rc;
struct sockaddr_storage ss;
struct sockaddr_un *un;
Expand Down
4 changes: 3 additions & 1 deletion src/transports/ipc/cipc.c
Expand Up @@ -381,7 +381,9 @@ static void nn_cipc_handler (struct nn_fsm *self, int src, int type,

static void nn_cipc_start_connecting (struct nn_cipc *self)
{
#if !defined NN_HAVE_WINDOWS
#if defined NN_HAVE_WINDOWS
nn_assert( TRUE );
#else
int rc;
struct sockaddr_storage ss;
struct sockaddr_un *un;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/err.c
Expand Up @@ -160,6 +160,8 @@ int nn_err_wsa_to_posix (int wsaerr)
return ECONNABORTED;
case WSAECONNRESET:
return ECONNRESET;
case WSAESOCKTNOSUPPORT:
return ESOCKTNOSUPPORT;
default:
nn_assert (0);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ipc.c
Expand Up @@ -38,7 +38,7 @@ int main ()
int i;
int s1, s2;

/* Try closing a IPC socket while it not connected. */
/* Try closing a IPC socket while it is not connected. */
sc = test_socket (AF_SP, NN_PAIR);
test_connect (sc, SOCKET_ADDRESS);
test_close (sc);
Expand Down

0 comments on commit 8830bb3

Please sign in to comment.