Skip to content

Commit 68978cc

Browse files
committed
init
init
1 parent eef9fe0 commit 68978cc

37 files changed

Lines changed: 895 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ $RECYCLE.BIN/
4545
Network Trash Folder
4646
Temporary Items
4747
.apdisk
48+
Unity3d使用SAPI语音在Windows上进行文本发音.docx

SApiClient/Assets/Sapi.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Sockets;
4+
5+
public class SocketExtra
6+
{
7+
/// <summary>
8+
/// 是否需要策略文件
9+
/// </summary>
10+
public SocketExtra(INetComponent netComponent)
11+
{
12+
m_net = netComponent;
13+
m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
14+
m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
15+
m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
16+
}
17+
18+
private Socket m_socket;
19+
private INetComponent m_net;
20+
21+
22+
public string Error { get; private set; }
23+
24+
public void Connect(string ip, int port)
25+
{
26+
m_socket.BeginConnect(ip, port, OnConnectCallBack, m_socket);
27+
}
28+
29+
private void OnConnectCallBack(IAsyncResult ar)
30+
{
31+
if (m_socket != null && m_socket.Connected)
32+
{
33+
StartListen(m_socket);
34+
}
35+
else
36+
{
37+
Error = "Connect faild";
38+
}
39+
}
40+
41+
private void StartListen(Socket socket)
42+
{
43+
var state = new StateObject { WorkSocket = socket };
44+
if (socket != null && socket.Connected)
45+
socket.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, ReceiveCallback, state);
46+
}
47+
48+
private void ReceiveCallback(IAsyncResult ar)
49+
{
50+
try
51+
{
52+
var state = (StateObject)ar.AsyncState;
53+
var client = state.WorkSocket;
54+
if (!client.Connected)
55+
return;
56+
var bytesRead = client.EndReceive(ar);
57+
if (bytesRead > 0)
58+
{
59+
var len = state.Result == null ? 0 : state.Result.Length;
60+
var res = new byte[len + bytesRead];
61+
if (state.Result != null)
62+
Array.Copy(state.Result, 0, res, 0, len);
63+
Array.Copy(state.Buffer, 0, res, len, bytesRead);
64+
state.Result = res;
65+
if (bytesRead <= StateObject.BufferSize)
66+
{
67+
m_net.NetReciveMsg(state.Result);
68+
state.Result = null;
69+
}
70+
if (client.Connected)
71+
client.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, ReceiveCallback, state);
72+
}
73+
else
74+
{
75+
Disconnect("Socket Disconnect:bytesRead <= 0");
76+
}
77+
}
78+
catch (Exception e)
79+
{
80+
Disconnect("Socket Disconnect:" + e.Message);
81+
Error = e.Message;
82+
}
83+
}
84+
85+
public void Disconnect(string msg)
86+
{
87+
DisConnectReason = msg;
88+
if (m_socket != null && m_socket.Connected)
89+
{
90+
try
91+
{
92+
m_socket.Shutdown(SocketShutdown.Both);
93+
m_socket.Close();
94+
}
95+
catch (Exception)
96+
{
97+
m_socket = null;
98+
}
99+
}
100+
}
101+
102+
public void Bind(string ip, int port)
103+
{
104+
var ipadr = IPAddress.Parse(ip);
105+
var ipend = new IPEndPoint(ipadr, port);
106+
m_socket.Bind(ipend);
107+
m_socket.Listen(0);
108+
m_socket.BeginAccept(OnClientConnecte, m_socket);
109+
110+
}
111+
112+
private void OnClientConnecte(IAsyncResult ar)
113+
{
114+
var socket = (Socket)ar.AsyncState;
115+
var client = socket.EndAccept(ar);
116+
Console.WriteLine("连接成功");
117+
StartListen(client);
118+
}
119+
120+
public void Destroy()
121+
{
122+
Disconnect("Socket Disconnect:Destroy");
123+
m_socket = null;
124+
m_net = null;
125+
}
126+
127+
public string DisConnectReason { get; private set; }
128+
129+
public bool SendMsg(byte[] buffer)
130+
{
131+
try
132+
{
133+
return m_socket != null && m_socket.Send(buffer) > 0;
134+
}
135+
catch (Exception e)
136+
{
137+
Error = e.Message;
138+
m_socket = null;
139+
return false;
140+
}
141+
}
142+
143+
public bool Connected
144+
{
145+
get
146+
{
147+
if (m_socket == null)
148+
return false;
149+
return m_socket.Connected;
150+
}
151+
}
152+
153+
public class StateObject
154+
{
155+
public Socket WorkSocket;
156+
157+
public const int BufferSize = 1024;
158+
public byte[] Buffer = new byte[BufferSize];
159+
public byte[] Result;
160+
}
161+
162+
public interface INetComponent
163+
{
164+
bool NetReciveMsg(byte[] recivebuffer);
165+
bool NetSendMsg(byte[] sendbuffer);
166+
}
167+
}

SApiClient/Assets/Sapi/SocketExtra.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SApiClient/Assets/Sapi/Speecher.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Collections;
2+
using System.Diagnostics;
3+
using System.Text;
4+
using UnityEngine;
5+
6+
public class Speecher : MonoBehaviour, SocketExtra.INetComponent
7+
{
8+
private SocketExtra m_socket;
9+
private Process m_process;
10+
11+
protected void Awake()
12+
{
13+
Ins = this;
14+
m_process = new Process
15+
{
16+
StartInfo = new ProcessStartInfo
17+
{
18+
FileName = "speech.exe",
19+
CreateNoWindow = true,
20+
WindowStyle = ProcessWindowStyle.Hidden
21+
},
22+
};
23+
m_process.Start();
24+
}
25+
26+
/***测试代码,可删除Start***/
27+
protected IEnumerator Start()
28+
{
29+
yield return StartCoroutine(Connect());
30+
Speak("test");
31+
}
32+
/***测试代码,可删除End***/
33+
34+
public IEnumerator Connect()
35+
{
36+
m_socket = new SocketExtra(this);
37+
m_socket.Connect("127.0.0.1", 9903);
38+
while (!m_socket.Connected)
39+
{
40+
yield return 1;
41+
}
42+
}
43+
44+
protected void OnDestroy()
45+
{
46+
if (m_process != null && !m_process.HasExited)
47+
m_process.Kill();
48+
m_process = null;
49+
}
50+
51+
public static Speecher Ins;
52+
53+
public static void Speak(string str)
54+
{
55+
#if UNITY_EDITOR||UNITY_STANDALONE_WIN
56+
Ins.Speech(str);
57+
#endif
58+
}
59+
60+
public void Speech(string str)
61+
{
62+
if (m_socket.Connected)
63+
{
64+
var bytes = Encoding.Default.GetBytes(str);
65+
m_socket.SendMsg(bytes);
66+
}
67+
}
68+
69+
public bool NetReciveMsg(byte[] recivebuffer)
70+
{
71+
return true;
72+
}
73+
74+
public bool NetSendMsg(byte[] sendbuffer)
75+
{
76+
return true;
77+
}
78+
}

SApiClient/Assets/Sapi/Speecher.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SApiClient/Assets/Scene.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SApiClient/Assets/Scene/test.unity

12.8 KB
Binary file not shown.

SApiClient/Assets/Scene/test.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
4.04 KB
Binary file not shown.

0 commit comments

Comments
 (0)