Skip to content

Commit

Permalink
dispatched for tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
zack-vii committed Apr 20, 2021
1 parent 7a07d70 commit 423265c
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 30 deletions.
3 changes: 2 additions & 1 deletion mdsshr/mdsthreadstatic.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#define THREADSTATIC_TDISHR 1
#define THREADSTATIC_TREESHR 2
#define THREADSTATIC_DCLSHR 3
#define THREADSTATIC_SIZE 4
#define THREADSTATIC_MDSIP 4
#define THREADSTATIC_SIZE 5

typedef struct
{
Expand Down
28 changes: 4 additions & 24 deletions mdstcpip/io_routines/ioroutinestcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,30 +430,10 @@ static int io_listen(int argc, char **argv)
client->addr = ((struct sockaddr_in *)&sin)->sin_addr.s_addr;
client->iphost = getHostInfo(sock, &client->host);
ClientList = client;
// add socket to active sockets //
FD_SET(sock, &fdactive);
dispatch_client(client);
}
}
// Process Clients in list searching for active sockets //
Client *c = ClientList;
while (c)
{
if (FD_ISSET(c->sock, &readfds))
{
// process active socket client //
MdsSetClientAddr(c->addr);
// DO MESSAGE ---> ProcessMessage() on client c //
DoMessage(c->id);
Client *c_chk;
for (c_chk = ClientList; c_chk && c_chk != c; c_chk = c_chk->next)
;
if (c_chk)
FD_CLR(c->sock, &readfds);
c = ClientList;
}
else
c = c->next;
}

}
else if (errno == EINTR)
{
Expand Down Expand Up @@ -486,10 +466,10 @@ static int io_listen(int argc, char **argv)
{
fprintf(stderr, "Removed disconnected client\n");
fflush(stderr);
pthread_cancel(c->thread);
pthread_detach(c->thread);
CloseConnection(c->id);
}
else
FD_SET(c->sock, &fdactive);
UnlockAsts();
}
}
Expand Down
41 changes: 41 additions & 0 deletions mdstcpip/io_routines/ioroutinesx.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sys/types.h>
#include <unistd.h>

//#define DEBUG
static ssize_t io_send(Connection *c, const void *buffer, size_t buflen,
int nowait);
static int io_disconnect(Connection *c);
Expand Down Expand Up @@ -34,6 +35,7 @@ typedef struct _client
{
struct _client *next;
SOCKET sock;
pthread_t thread;
int id;
char *username;
uint32_t addr;
Expand Down Expand Up @@ -570,3 +572,42 @@ static int run_server_mode(Options *options)
} while (STATUS_OK);
return C_ERROR;
}

#ifdef DEBUG
static void client_cleanup(void *args)
{
fputs((char*)args, stderr);
}
#endif

static void *client_thread(void *args)
{
Client *c = (Client *)args;
MdsSetClientAddr(c->addr);
#ifdef DEBUG
char closed[512];
sprintf(closed, "Client %d closed: %s@%s\n", c->id, c->username, c->iphost);
fprintf(stderr, "Client %d opened: %s@%s\n", c->id, c->username, c->iphost);
pthread_cleanup_push(client_cleanup, closed);
#endif
int status;
do
{
status = DoMessage(c->id);
} while (STATUS_OK);
#ifdef DEBUG
pthread_cleanup_pop(1);
#endif
return NULL;
}

static inline int dispatch_client(Client *c)
{
errno = pthread_create(&c->thread, NULL, client_thread, (void*)c);
if (errno)
{
perror("dispatch_client");
c->thread = 0;
}
return !!errno;
}
16 changes: 12 additions & 4 deletions mdstcpip/mdsipshr/GetSetSettings.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "../mdsip_connections.h"
#include "mdsipthreadstatic.h"
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#define DEFAULT_HOSTFILE "C:\\MDSIP.HOSTS"
#else
Expand Down Expand Up @@ -199,8 +200,15 @@ int SetMdsConnectTimeout(int sec)
////////////////////////////////////////////////////////////////////////////////
// CLIENT ADDRESS ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
static int ClientAddr = 0;
int MdsGetClientAddr() { return ClientAddr; }
int MdsGetClientAddr()
{
MDSIPTHREADSTATIC_INIT;
return MDSIP_CLIENTADDR;
}

/// Address of current client structure
void MdsSetClientAddr(int addr) { ClientAddr = addr; }
void MdsSetClientAddr(int addr)
{
MDSIPTHREADSTATIC_INIT;
MDSIP_CLIENTADDR = addr;
}
1 change: 0 additions & 1 deletion mdstcpip/mdsipshr/LoadIo.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <libroutines.h>

#include "../mdsshr/version.h"
#include "../mdsip_connections.h"

////////////////////////////////////////////////////////////////////////////////
Expand Down
51 changes: 51 additions & 0 deletions mdstcpip/mdsipshr/MdsIpThreadStatic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright (c) 2017, Massachusetts Institute of Technology All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <mdsplus/mdsconfig.h>

#include <stdlib.h>
#include <string.h>

#include <libroutines.h>
#include <mdsshr.h>
#include <status.h>
#include <strroutines.h>

#include "../mdsshr/version.h"
#include "mdsipthreadstatic.h"

static void buffer_free(MDSIPTHREADSTATIC_ARG)
{
free(MDSIPTHREADSTATIC_VAR);
}
static inline MDSIPTHREADSTATIC_TYPE *buffer_alloc()
{
MDSIPTHREADSTATIC_ARG =
(MDSIPTHREADSTATIC_TYPE *)calloc(1, sizeof(MDSIPTHREADSTATIC_TYPE));

return MDSIPTHREADSTATIC_VAR;
}

IMPLEMENT_GETTHREADSTATIC(MDSIPTHREADSTATIC_TYPE, MdsIpGetThreadStatic,
THREADSTATIC_MDSIP, buffer_alloc, buffer_free)
15 changes: 15 additions & 0 deletions mdstcpip/mdsipshr/mdsipthreadstatic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include "../mdsshr/mdsthreadstatic.h"

#define MDSIPTHREADSTATIC_VAR MdsIpThreadStatic_p
#define MDSIPTHREADSTATIC_TYPE MdsIpThreadStatic_t
#define MDSIPTHREADSTATIC_ARG MDSIPTHREADSTATIC_TYPE *MDSIPTHREADSTATIC_VAR
#define MDSIPTHREADSTATIC(MTS) MDSIPTHREADSTATIC_ARG = MdsIpGetThreadStatic(MTS)
#define MDSIPTHREADSTATIC_INIT MDSIPTHREADSTATIC(NULL)
typedef struct
{
int clientaddr;
} MDSIPTHREADSTATIC_TYPE;
#define MDSIP_CLIENTADDR MDSIPTHREADSTATIC_VAR->clientaddr

extern DEFINE_GETTHREADSTATIC(MDSIPTHREADSTATIC_TYPE, MdsIpGetThreadStatic);

0 comments on commit 423265c

Please sign in to comment.