-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtClient.cs
163 lines (147 loc) · 6.03 KB
/
ButtClient.cs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ButtClient
{
/// <summary>
/// A framework-independent library for controlling your sex toys (.NET Standard 2.0 compliant).<br/>
/// Requires the standalone receiver application <a href='https://github.com/MLTorches/ButtServer/releases/latest'>ButtServer</a>.
/// </summary>
///
/// <remarks>Implementation-wise, this a transparent wrapper around the <a href='https://github.com/MLTorches/BasicButtManager/releases/latest'>BasicButtManager</a> library,
/// via the ButtServer proxy.<br/>
/// The public-facing interface is identical to that of BasicButtManager besides the class name.</remarks>
public class ButtClient
{
private readonly Socket socket;
private readonly string clientName;
/// <summary>
/// Initialize this ButtClient and connect it to the ButtServer.
/// </summary>
/// <param name="clientName">Identify this client to the server.</param>
public ButtClient(String clientName)
{
this.clientName = clientName;
IPHostEntry ipHostInfo = Dns.GetHostEntryAsync("localhost").Result;
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint ipEndPt = new IPEndPoint(ipAddress, 42069);
socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipEndPt);
Send("Connected");
}
/// <summary>
/// Send the specified payload with the client's name prefixed.
/// </summary>
/// <param name="payload">The raw message to be sent.</param>
private void Send(String payload)
{
payload = clientName + ' ' + payload + "\n";
byte[] msg = Encoding.ASCII.GetBytes(payload);
socket.Send(msg);
}
/// <summary>
/// Directly control your toys with the provided parameters.
/// </summary>
///
/// <param name="intensity">Intensity for vibrators, oscillators, rotators, pressurizers, etc.</param>
/// <param name="position">Position of linear actuators, if omitted will take the value of intensity.</param>
/// <param name="oscillate">If true, let the linear actuators move automatically at a set speed (based on intensity). Only applicable if position not given.</param>
///
/// <remarks>It is recommended in most cases to use one of the provided convenient functions instead, most notably Set().</remarks>
public void Control(float intensity, float position = -1f, bool oscillate = false)
{
Send("Control" + ' ' + intensity + ' ' + position + ' ' + oscillate);
}
/// <summary>
/// Vibrate, oscillate, stroke, etc. all connected devices according to the one given intensity value.
/// </summary>
///
/// <param name="intensity">The intensity to be applied to all connected toys.</param>
public void Set(float intensity)
{
Send("Set" + ' ' + intensity);
}
/// <summary>
/// Set all toys to the same constant speed AND location.
/// </summary>
///
/// <param name="actionValue">The intensity or location to be set to all connected toys.</param>
public void Press(float actionValue)
{
Send("Press" + ' ' + actionValue);
}
/// <summary>
/// Fade the intensity of all toys to a certain point.
/// </summary>
///
/// <param name="targetIntensity">The target intensity, can be higher or lower than the current intensity.</param>
/// <param name="smoothness">Between 0f and 1f, the higher the smoother the transition.</param>
/// <remarks>Does not support multiple fades at one time.</remarks>
public void Fade(float targetIntensity, float smoothness = 1f)
{
Send("Fade" + ' ' + targetIntensity + ' ' + smoothness);
}
/// <summary>
/// Fade all toys in from zero intensity to max power.
/// </summary>
///
/// <remarks>Does not support multiple fades at one time.</remarks>
public void FadeIn()
{
Send("FadeIn");
}
/// <summary>
/// Fade all toys out from max intensity to zero power.
/// </summary>
///
/// <remarks>Does not support multiple fades at one time.</remarks>
public void FadeOut()
{
Send("FadeOut");
}
/// <summary>
/// Press for a single buzz/squeeze/stroke/spin.
/// </summary>
///
/// <param name="reboundSpeed">How long the action is held before being released, from 0f to 1f.</param>
/// <remarks>Client must make sure to not pulse too often too quickly (~ once per second is ideal).</remarks>
public void Pulse(float reboundSpeed)
{
Send("Pulse" + ' ' + reboundSpeed);
}
/// <summary>
/// Push all strokers down, buzz vibrators for a bit, then push strokers back up.<br/>
/// </summary>
///
/// <param name="reboundSpeed">How long the action is held before being released, from 0f to 1f.</param>
/// <remarks>Basically the slow version of Pulse().</remarks>
public void Hold(float reboundSpeed)
{
Send("Hold" + ' ' + reboundSpeed);
}
/// <summary>
/// Stop all connected toys.
/// </summary>
public void Stop()
{
Send("Stop");
}
/// <summary>
/// Stop all connected toys and close server connection.
/// </summary>
public void Exit()
{
Send("Exit");
}
/// <summary>
/// Disconnect this client from the ButtServer.
/// </summary>
public void Disconnect()
{
Send("Disconnected");
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
}