drivers/can_tcp_win32/can_tcp_win32_server.cpp
author lbessard
Mon, 20 Apr 2009 17:42:36 +0200
changeset 546 13701680330c
parent 407 384c3cd7afa2
child 633 2c43383a1d57
permissions -rwxr-xr-x
Bug avoided reset PDO mapping fixed
407
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     1
/*
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     3
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     4
Copyright (C): Edouard TISSERANT
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     5
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     6
See COPYING file for copyrights details.
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     7
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     8
This library is free software; you can redistribute it and/or
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    10
License as published by the Free Software Foundation; either
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    12
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    13
This library is distributed in the hope that it will be useful,
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    16
Lesser General Public License for more details.
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    17
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    19
License along with this library; if not, write to the Free Software
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    21
*/
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    22
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    23
#include "Socket.h"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    24
#include <iostream>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    25
#include <process.h>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    26
#include <string>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    27
#include <list>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    28
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    29
using namespace std;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    30
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    31
typedef std::list<Socket*> socket_list;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    32
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    33
socket_list g_connections;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    34
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    35
unsigned __stdcall Connection(void* a) {
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    36
  Socket* s = (Socket*) a;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    37
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    38
  g_connections.push_back(s);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    39
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    40
  //s->SendLine("Welcome to the Message Distributor");
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    41
  printf("Accepted new connection (0x%x).\n");
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    42
  while (1) {
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    43
    std::string r = s->ReceiveLine();
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    44
    if (r.empty()) break;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    45
    //cout << r  << endl;    
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    46
    for (socket_list::iterator os =g_connections.begin();
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    47
                               os!=g_connections.end(); 
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    48
                               os++) {
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    49
      if (*os != s) (*os)->SendLine(r);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    50
    }
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    51
  }
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    52
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    53
  g_connections.remove(s);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    54
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    55
  delete s;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    56
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    57
  printf("Connection closed (0x%x).\n",s);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    58
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    59
  return 0;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    60
}
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    61
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    62
int main() {
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    63
  printf("************************************************\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    64
		 "* TCP broadcasting chat server for CanFestival *\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    65
		 "*  for use with can_tcp_win32 CAN driver DLL   *\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    66
		 "************************************************\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    67
		 "\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    68
		 "Accepts connections on port 11898\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    69
		 "and repeat '\\n' terminated lines \n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    70
		 "to each connected client\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    71
		 "\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    72
		 "Use netcat to monitor trafic\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    73
		 " nc 127.0.0.1 11898\n"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    74
		 "\n");
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    75
  fflush(stdout);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    76
  SocketServer in(11898,5);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    77
  
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    78
  while (1) {
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    79
    Socket* s=in.Accept();
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    80
  
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    81
    unsigned ret;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    82
    _beginthreadex(0,0,Connection,(void*) s,0,&ret);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    83
  }
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    84
  
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    85
  return 0;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    86
}