source: flair-src/trunk/lib/FlairCore/src/UdtSocket.cpp@ 2

Last change on this file since 2 was 2, checked in by Sanahuja Guillaume, 7 years ago

flaircore

File size: 4.4 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2015/04/21
6// filename: UdtSocket.cpp
7//
8// author: Gildas Bayard
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: Class defining a UDT socket
14//
15//
16/*********************************************************************/
17#include "UdtSocket.h"
18#include <udt.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <string.h>
23
24using std::string;
25
26namespace flair
27{
28namespace core
29{
30
31UdtSocket::UdtSocket(const Object* parent,const std::string name,bool _blockOnSend,bool _blockOnReceive):ConnectedSocket(parent,name) {
32 UDT::startup();
33 blockOnSend=_blockOnSend;
34 blockOnReceive=_blockOnReceive;
35}
36
37UdtSocket::~UdtSocket(){
38}
39
40void UdtSocket::Listen(const unsigned int port,const std::string localAddress) {
41 socket = UDT::socket(AF_INET, SOCK_DGRAM, 0);
42
43 UDT::setsockopt(socket, 0, UDT_SNDSYN, &blockOnSend, sizeof(bool));
44 UDT::setsockopt(socket, 0, UDT_RCVSYN, &blockOnReceive, sizeof(bool));
45 bool reuse = true;
46 UDT::setsockopt(socket, 0, UDT_REUSEADDR, &reuse, sizeof(bool));
47
48 sockaddr_in my_addr;
49 my_addr.sin_family = AF_INET;
50 my_addr.sin_port = htons(port);
51 if (localAddress=="ANY") {
52 my_addr.sin_addr.s_addr=INADDR_ANY;
53 } else {
54 inet_aton(localAddress.c_str(),&(my_addr.sin_addr));
55 }
56 memset(&(my_addr.sin_zero), '\0', 8);
57
58 if (UDT::ERROR == UDT::bind(socket, (sockaddr*)&my_addr, sizeof(my_addr))) {
59 Err("bind, %s\n",UDT::getlasterror().getErrorMessage());
60 }
61
62 UDT::listen(socket, 1);
63}
64
65UdtSocket *UdtSocket::Accept(Time timeout) {
66 // TIMEOUT UNSUPPORTED!!
67 UdtSocket *acceptedSocket=new UdtSocket(this->Parent(),this->ObjectName());
68 acceptedSocket->blockOnSend=this->blockOnSend;
69 acceptedSocket->blockOnReceive=this->blockOnReceive;
70
71 sockaddr_in their_addr;
72 int namelen = sizeof(their_addr);
73
74 if ((acceptedSocket->socket = UDT::accept(socket, (sockaddr*)&their_addr, &namelen))==UDT::INVALID_SOCK) {
75 Err("accept: %s, code %i\n",UDT::getlasterror().getErrorMessage(),UDT::getlasterror().getErrorCode());
76 }
77
78 return acceptedSocket;
79}
80
81bool UdtSocket::Connect(const unsigned int port,const std::string distantAddress,Time timeout) {
82 bool success=true;
83 socket=UDT::socket(AF_INET, SOCK_DGRAM, 0);
84
85 UDT::setsockopt(socket, 0, UDT_SNDSYN, &blockOnSend, sizeof(bool));
86 UDT::setsockopt(socket, 0, UDT_RCVSYN, &blockOnReceive, sizeof(bool));
87 bool reuse = true;
88 UDT::setsockopt(socket, 0, UDT_REUSEADDR, &reuse, sizeof(bool));
89
90 sockaddr_in serv_addr;
91 serv_addr.sin_family = AF_INET;
92 serv_addr.sin_port = htons(short(port));
93 if (inet_pton(AF_INET, distantAddress.c_str(), &serv_addr.sin_addr) <= 0) {
94 printf("incorrect network address.");
95 success=false;
96 }
97 memset(&(serv_addr.sin_zero), '\0', 8);
98
99 if (UDT::ERROR == UDT::connect(socket, (sockaddr*)&serv_addr, sizeof(serv_addr))) {
100 success=false;
101 }
102 if (!success) {
103 UDT::close(socket);
104 return false;
105 } else return true;
106}
107
108ssize_t UdtSocket::SendMessage(const char* buffer,size_t bufferSize,Time timeout){
109 int udtTimeout=timeout;
110 if (blockOnSend) {
111 if(UDT::setsockopt(socket, 0, UDT_SNDTIMEO, &udtTimeout, sizeof(udtTimeout))!=0) Err("error UDT_SNDTIMEO %s\n", UDT::getlasterror().getErrorMessage());
112 }
113 ssize_t bytesSent=UDT::sendmsg(socket, buffer, bufferSize, -1,true);
114
115 return bytesSent;
116}
117
118ssize_t UdtSocket::RecvMessage(char* buffer,size_t bufferSize,Time timeout){
119 int udtTimeout=timeout;
120 if (blockOnReceive) {
121 if(UDT::setsockopt(socket, 0, UDT_RCVTIMEO, &udtTimeout, sizeof(udtTimeout))!=0) Err("error UDT_RCVTIMEO %s\n", UDT::getlasterror().getErrorMessage());
122 }
123 ssize_t bytesRead= UDT::recvmsg(socket,buffer,bufferSize);
124
125/*
126 if(bytesRead<0) {
127 if(UDT::getlasterror().getErrorCode()==CUDTException::ECONNLOST) {
128 connection_lost=true;
129 }
130 }
131*/
132 return bytesRead;
133}
134
135uint16_t UdtSocket::NetworkToHost16(uint16_t data) {
136 return ntohs(data);
137}
138
139uint16_t UdtSocket::HostToNetwork16(uint16_t data) {
140 return htons(data);
141}
142
143uint32_t UdtSocket::NetworkToHost32(uint32_t data) {
144 return ntohl(data);
145}
146
147uint32_t UdtSocket::HostToNetwork32(uint32_t data) {
148 return htonl(data);
149}
150
151} // end namespace core
152} // end namespace flair
Note: See TracBrowser for help on using the repository browser.