drivers/can_tcp_win32/Socket.h
author Edouard Tisserant
Thu, 23 Jun 2011 18:12:34 +0200
changeset 662 aad111ad0018
parent 355 12adbd08e10c
permissions -rwxr-xr-x
fixed bug with cross wmingw build
355
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     1
/* 
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     2
   Socket.h
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     3
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     4
   Copyright (C) 2002-2004 René Nyffenegger
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     5
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     6
   This source code is provided 'as-is', without any express or implied
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     7
   warranty. In no event will the author be held liable for any damages
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     8
   arising from the use of this software.
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
     9
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    10
   Permission is granted to anyone to use this software for any purpose,
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    11
   including commercial applications, and to alter it and redistribute it
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    12
   freely, subject to the following restrictions:
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    13
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    14
   1. The origin of this source code must not be misrepresented; you must not
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    15
      claim that you wrote the original source code. If you use this source code
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    16
      in a product, an acknowledgment in the product documentation would be
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    17
      appreciated but is not required.
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    18
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    19
   2. Altered source versions must be plainly marked as such, and must not be
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    20
      misrepresented as being the original source code.
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    21
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    22
   3. This notice may not be removed or altered from any source distribution.
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    23
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    24
   René Nyffenegger rene.nyffenegger@adp-gmbh.ch
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    25
*/
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    26
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    27
#ifndef SOCKET_H
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    28
#define SOCKET_H
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    29
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    30
#include <winsock2.h>
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    31
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    32
#include <string>
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    33
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    34
enum TypeSocket {BlockingSocket, NonBlockingSocket};
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    35
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    36
class Socket {
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    37
public:
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    38
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    39
  virtual ~Socket();
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    40
  Socket(const Socket&);
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    41
  Socket& operator=(Socket&);
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    42
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    43
  std::string ReceiveLine();
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    44
  std::string ReceiveBytes();
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    45
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    46
  void   Close();
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    47
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    48
  // The parameter of SendLine is not a const reference
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    49
  // because SendLine modifes the std::string passed.
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    50
  void   SendLine (std::string);
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    51
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    52
  // The parameter of SendBytes is a const reference
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    53
  // because SendBytes does not modify the std::string passed 
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    54
  // (in contrast to SendLine).
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    55
  void   SendBytes(const std::string&);
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    56
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    57
protected:
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    58
  friend class SocketServer;
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    59
  friend class SocketSelect;
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    60
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    61
  Socket(SOCKET s);
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    62
  Socket();
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    63
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    64
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    65
  SOCKET s_;
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    66
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    67
  int* refCounter_;
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    68
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    69
private:
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    70
  static void Start();
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    71
  static void End();
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    72
  static int  nofSockets_;
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    73
};
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    74
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    75
class SocketClient : public Socket {
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    76
public:
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    77
  SocketClient(const std::string& host, int port);
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    78
};
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    79
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    80
class SocketServer : public Socket {
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    81
public:
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    82
  SocketServer(int port, int connections, TypeSocket type=BlockingSocket);
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    83
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    84
  Socket* Accept();
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    85
};
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    86
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    87
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/wsapiref_2tiq.asp
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    88
class SocketSelect {
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    89
  public:
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    90
    SocketSelect(Socket const * const s1, Socket const * const s2=NULL, TypeSocket type=BlockingSocket);
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    91
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    92
    bool Readable(Socket const * const s);
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    93
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    94
  private:
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    95
    fd_set fds_;
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    96
}; 
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    97
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    98
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
    99
12adbd08e10c TCP broadcasting chat server to exchange CAN messages between CanFestival nodes on windows. This let user simulate a CANopen network on windows.
etisserant
parents:
diff changeset
   100
#endif