Skip to content

Commit f44f1db

Browse files
committed
SocketExtra拆分为SocketBase,SocketClient,SocketServer
增加byte[]简单的读写类,ByteInArg.cs, Speech增加语音识别功能,增加GUI测试显示
1 parent 876e1cc commit f44f1db

12 files changed

Lines changed: 410 additions & 199 deletions

SApiClient/Assets/Core.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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.IO;
2+
3+
namespace Stardust
4+
{
5+
public class ByteInArg
6+
{
7+
private readonly MemoryStream m_stream = new MemoryStream();
8+
private readonly BinaryWriter m_writer;
9+
10+
public ByteInArg()
11+
{
12+
m_writer = new BinaryWriter(m_stream);
13+
}
14+
15+
public void Write(bool value)
16+
{
17+
m_writer.Write(value);
18+
}
19+
20+
public void Write(byte value)
21+
{
22+
m_writer.Write(value);
23+
}
24+
25+
public void Write(char ch)
26+
{
27+
m_writer.Write(ch);
28+
}
29+
30+
public void Write(int value)
31+
{
32+
m_writer.Write(value);
33+
}
34+
35+
public void Write(string value)
36+
{
37+
m_writer.Write(value);
38+
}
39+
40+
public byte[] GetBuffer()
41+
{
42+
return m_stream.GetBuffer();
43+
}
44+
}
45+
46+
public class ByteOutArg
47+
{
48+
private readonly BinaryReader m_reader;
49+
50+
public ByteOutArg(byte[] buffer)
51+
{
52+
var stream = new MemoryStream(buffer);
53+
m_reader = new BinaryReader(stream);
54+
}
55+
56+
public bool ReadBoolean()
57+
{
58+
return m_reader.ReadBoolean();
59+
}
60+
61+
public byte ReadByte()
62+
{
63+
return m_reader.ReadByte();
64+
}
65+
66+
public int ReadInt32()
67+
{
68+
return m_reader.ReadInt32();
69+
}
70+
71+
public string ReadString()
72+
{
73+
return m_reader.ReadString();
74+
}
75+
}
76+
}

SApiClient/Assets/Core/ByteInArg.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.
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
4+
using System.Net.Sockets;
5+
6+
namespace Stardust
7+
{
8+
public interface INetComponent
9+
{
10+
bool NetReciveMsg(byte[] recivebuffer, int netID);
11+
}
12+
13+
public class SocketClient : SocketBase
14+
{
15+
public SocketClient(INetComponent netComponent) : base(netComponent)
16+
{
17+
}
18+
19+
public void Connect(string ip, int port)
20+
{
21+
Socket.BeginConnect(ip, port, OnConnectCallBack, Socket);
22+
}
23+
24+
private void OnConnectCallBack(IAsyncResult ar)
25+
{
26+
if (Socket != null && Socket.Connected)
27+
{
28+
StartListen(Socket);
29+
}
30+
else
31+
{
32+
Error = "Connect faild";
33+
}
34+
}
35+
36+
public bool SendMsg(byte[] buffer)
37+
{
38+
return SendMsg(buffer, Socket);
39+
}
40+
}
41+
public class SocketServer : SocketBase
42+
{
43+
public SocketServer(INetComponent netComponent) : base(netComponent)
44+
{
45+
}
46+
47+
public void Bind(string ip, int port)
48+
{
49+
var ipadr = IPAddress.Parse(ip);
50+
var ipend = new IPEndPoint(ipadr, port);
51+
Socket.Bind(ipend);
52+
Socket.Listen(0);
53+
Socket.BeginAccept(OnClientConnecte, Socket);
54+
}
55+
56+
private readonly Dictionary<int, Socket> m_clients = new Dictionary<int, Socket>();
57+
58+
public delegate void OnConnecteEvent(int cid);
59+
60+
public OnConnecteEvent OnConnecte;
61+
62+
private void OnClientConnecte(IAsyncResult ar)
63+
{
64+
var socket = (Socket)ar.AsyncState;
65+
var client = socket.EndAccept(ar);
66+
var id = StartListen(client);
67+
m_clients.Add(id, client);
68+
if (OnConnecte != null)
69+
OnConnecte(id);
70+
}
71+
72+
public bool SendMsg(byte[] buffer, int cid)
73+
{
74+
var client = m_clients[cid];
75+
return SendMsg(buffer, client);
76+
}
77+
78+
protected override void OnDisconnect()
79+
{
80+
base.OnDisconnect();
81+
OnConnecte = null;
82+
}
83+
}
84+
85+
public abstract class SocketBase
86+
{
87+
private INetComponent m_net;
88+
protected Socket Socket { get; private set; }
89+
90+
protected SocketBase(INetComponent netComponent)
91+
{
92+
m_net = netComponent;
93+
Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
94+
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
95+
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
96+
}
97+
98+
private int m_idIndex;
99+
100+
protected int StartListen(Socket socket)
101+
{
102+
var res = m_idIndex;
103+
m_idIndex++;
104+
var state = new StateObject { WorkSocket = socket, NetID = m_idIndex };
105+
if (socket != null && socket.Connected)
106+
socket.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, ReceiveCallback, state);
107+
return res;
108+
}
109+
110+
private void ReceiveCallback(IAsyncResult ar)
111+
{
112+
try
113+
{
114+
var state = (StateObject)ar.AsyncState;
115+
var socket = state.WorkSocket;
116+
if (!socket.Connected)
117+
return;
118+
var bytesRead = socket.EndReceive(ar);
119+
if (bytesRead > 0)
120+
{
121+
var len = state.Result == null ? 0 : state.Result.Length;
122+
var res = new byte[len + bytesRead];
123+
if (state.Result != null)
124+
Array.Copy(state.Result, 0, res, 0, len);
125+
Array.Copy(state.Buffer, 0, res, len, bytesRead);
126+
state.Result = res;
127+
if (bytesRead <= StateObject.BufferSize)
128+
{
129+
m_net.NetReciveMsg(state.Result, state.NetID);
130+
state.Result = null;
131+
}
132+
if (socket.Connected)
133+
socket.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, ReceiveCallback, state);
134+
}
135+
else
136+
{
137+
Disconnect("Socket Disconnect:bytesRead <= 0");
138+
}
139+
}
140+
catch (Exception e)
141+
{
142+
Disconnect("Socket Disconnect:" + e.Message);
143+
Error = e.Message;
144+
}
145+
}
146+
147+
public string DisConnectReason { get; private set; }
148+
149+
public void Disconnect(string msg)
150+
{
151+
OnDisconnect();
152+
DisConnectReason = msg;
153+
if (Socket != null && Socket.Connected)
154+
{
155+
try
156+
{
157+
Socket.Shutdown(SocketShutdown.Both);
158+
Socket.Close();
159+
}
160+
catch (Exception)
161+
{
162+
Socket = null;
163+
}
164+
}
165+
}
166+
167+
protected virtual void OnDisconnect()
168+
{
169+
}
170+
171+
public void Destroy()
172+
{
173+
Disconnect("Socket Disconnect:Destroy");
174+
Socket = null;
175+
m_net = null;
176+
}
177+
178+
protected bool SendMsg(byte[] buffer, Socket socket)
179+
{
180+
try
181+
{
182+
return socket != null && socket.Send(buffer) > 0;
183+
}
184+
catch (Exception e)
185+
{
186+
Error = e.Message;
187+
return false;
188+
}
189+
}
190+
191+
public string Error { get; protected set; }
192+
193+
public bool Connected
194+
{
195+
get
196+
{
197+
if (Socket == null)
198+
return false;
199+
return Socket.Connected;
200+
}
201+
}
202+
203+
private class StateObject
204+
{
205+
public Socket WorkSocket;
206+
207+
public const int BufferSize = 1024;
208+
public readonly byte[] Buffer = new byte[BufferSize];
209+
public byte[] Result;
210+
211+
public int NetID;
212+
}
213+
}
214+
}
File renamed without changes.

0 commit comments

Comments
 (0)