forked from recotana/ArdOSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSCServer.cpp
97 lines (56 loc) · 1.78 KB
/
OSCServer.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
/*
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 <string.h>
#include "OSCcommon/OSCServer.h"
#include "OSCcommon/OSCcommon.h"
#include "OSCCommon/OSCMessage.h"
#include <utility/socket.h>
#include <utility/w5100.h>
OSCServer::OSCServer(void){
_sock = MAX_SOCK_NUM;
}
OSCServer::~OSCServer(void){
stop();
}
int16_t OSCServer::begin(uint16_t _recievePort){
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;
_port=_recievePort;
if( socket( _sock , SnMR::UDP , _port, 0 ) ) return -1;
rcvFlush();
return 1;
}
void OSCServer::stop(void){
if (_sock == MAX_SOCK_NUM) return;
close(_sock);
_sock = MAX_SOCK_NUM;
}
int16_t OSCServer::aviableCheck(void){
if( !( W5100.readSnIR(_sock) && SnIR::RECV ) ) return -1;
if( W5100.getRXReceivedSize(_sock) == 0 ) return -1;
OSCMessage rcvMes;
if ( recvfrom( _sock , _rcvData , 1 , rcvMes._ip , &rcvMes._port ) > kMaxRecieveData ) return -1;
if( _decoder.decode( &rcvMes ,_rcvData ) < 0 ) return -1;
_adrMatch.paternComp(&rcvMes);
return 1;
}
void OSCServer::rcvFlush(void){
uint8_t byte;
while ( W5100.getRXReceivedSize(_sock) ) recv( _sock , &byte , 1);
}
void OSCServer::addCallback(char *_adr , Pattern::AdrFunc _func ){
_adrMatch.addOscAddress( _adr , _func );
}