drivers/can_tcp_win32/can_tcp_win32_server.cpp
author Laurent Bessard
Wed, 24 Apr 2013 17:28:06 +0200
changeset 781 77fe158d6ccd
parent 774 91d708a2cb4e
permissions -rwxr-xr-x
Fixed Save As... function in Beremiz
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
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 407
diff changeset
    23
#include <stdio.h>
407
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    24
#include "Socket.h"
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    25
#include <iostream>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    26
#include <process.h>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    27
#include <string>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    28
#include <list>
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    29
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    30
using namespace std;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    31
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    32
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
    33
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    34
socket_list g_connections;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    35
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    36
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
    37
  Socket* s = (Socket*) a;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    38
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    39
  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
    40
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;
771
4c74be252095 Now can_tcp_win32 default to 127.0.0.1 and can_tcp_win32_server is verbose
Edouard Tisserant
parents: 633
diff changeset
    45
    cout << r  << endl;    
407
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"
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    72
		 "Use netcat to monitor/log trafic\n"
407
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"
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    74
		 "\n"
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    75
         "CAN message format:\n"
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    76
         "{COB_ID,RTR,data_len,{data}}\n"
407
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    77
		 "\n");
774
91d708a2cb4e Fixed DLL calling convention when build with mingw32 + some re-indenting in can_tcp_win32
Edouard Tisserant
parents: 771
diff changeset
    78
407
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    79
  fflush(stdout);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    80
  SocketServer in(11898,5);
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    81
  
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    82
  while (1) {
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    83
    Socket* s=in.Accept();
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
    unsigned ret;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    86
    _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
    87
  }
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    88
  
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    89
  return 0;
384c3cd7afa2 Fixed some win32 stdout related problems. May not worl wit visualstudio debug log anymore.
etisserant
parents: 355
diff changeset
    90
}