forked from recotana/ArdOSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSCClient.cpp
102 lines (53 loc) · 1.59 KB
/
OSCClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
ArdOSC 2.1 - OSC Library for Arduino.
-------- Lisence -----------------------------------------------------------
ArdOSC
The MIT License
Copyright (c) 2009 - 2011 recotana( http://recotana.com ) All right reserved
*/
#include <stdlib.h>
#include "OSCCommon/OSCClient.h"
#include <utility/socket.h>
#include <utility/w5100.h>
OSCClient::OSCClient(void){
_sock = MAX_SOCK_NUM;
}
OSCClient::~OSCClient(void){
flushSendData();
}
int16_t OSCClient::sockOpen(void){
if ( _sock != MAX_SOCK_NUM ) return -1;
for ( int i = 0 ; i < MAX_SOCK_NUM ; i++ ) {
uint8_t s = W5100.readSnSR(i);
if ( s == SnSR::CLOSED || s == SnSR::FIN_WAIT ) {
_sock = i;
break;
}
}
if ( _sock == MAX_SOCK_NUM ) return -1;
socket( _sock, SnMR::UDP , kDummyPortNumber , 0 );
return 1;
}
void OSCClient::sockClose(void){
if ( _sock == MAX_SOCK_NUM ) return;
close(_sock);
_sock = MAX_SOCK_NUM;
}
int16_t OSCClient::send(OSCMessage *_message){
uint16_t result=0;
flushSendData();
_sendData = ( uint8_t*)calloc( _message->getMessageSize() ,1 );
if( encoder.encode( _message , _sendData ) < 0 ){
flushSendData();
return -1;
}
if( sockOpen()<0 ) return -1; //socket open check
result = sendto( _sock , _sendData , _message->getMessageSize() , _message->getIpAddress(), _message->getPortNumber() );
sockClose();
flushSendData();
return result;
}
void OSCClient::flushSendData(void){
free(_sendData);
_sendData=0;
}