aboutsummaryrefslogtreecommitdiff
path: root/system/pdp_comm.c
blob: 80cbaaa9897d6be2b79ca87b000e44cb55ad79d9 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 *   Pure Data Packet system implementation.
 *   Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* this file contains misc communication methods */


#include "pdp.h"
#include "pdp_internals.h"
#include <stdio.h>

/* all symbols are C style */
#ifdef __cplusplus
extern "C"
{
#endif


/************** packet management and communication convenience functions ************/

/* send a packet to an outlet */
void outlet_pdp(t_outlet *out, int packetid)
{
    t_atom atom[2];
    t_symbol *s = gensym("pdp");
    t_symbol *rro = gensym("register_ro");
    t_symbol *rrw = gensym("register_rw");
    t_symbol *proc = gensym("process");


    SETFLOAT(atom+1, (float)packetid);

    SETSYMBOL(atom+0, rro);
    outlet_anything(out, s, 2, atom);

    SETSYMBOL(atom+0, rrw);
    outlet_anything(out, s, 2, atom);

    /* this is not really necessary, it can be triggered by the rw message */
    SETSYMBOL(atom+0, proc);
    outlet_anything(out, s, 1, atom);

}


/* unregister a packet and send it to an outlet */
void
pdp_pass_if_valid(t_outlet *outlet, int *packet)
{
    if (-1 != *packet){
	pdp_packet_mark_unused(*packet);
	outlet_pdp(outlet, *packet);
	*packet = -1;
    }
}

void
pdp_replace_if_valid(int *dpacket, int *spacket)
{
    if (-1 != *spacket){
	pdp_packet_mark_unused(*dpacket);
	*dpacket = *spacket;
	*spacket = -1;
    }
    
}


int
pdp_packet_copy_ro_or_drop(int *dpacket, int spacket)
{
    int drop = 0;
    if (*dpacket == -1) *dpacket = pdp_packet_copy_ro(spacket);
    else {
	/* send a notification there is a dropped packet */
	pdp_control_notify_drop(spacket);
	drop = 1;
    }
    return drop;
}


int
pdp_packet_copy_rw_or_drop(int *dpacket, int spacket)
{
    int drop = 0;
    if (*dpacket == -1) *dpacket = pdp_packet_copy_rw(spacket);
    else {
	/* send a notification there is a dropped packet */
	pdp_control_notify_drop(spacket);
	drop = 1;
    }
    return drop;
}






#ifdef __cplusplus
}
#endif