diff options
author | chai <chaifix@163.com> | 2018-08-11 21:43:14 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-11 21:43:14 +0800 |
commit | 8389e1ab28952b29b99fe62abe822ec61ee61c94 (patch) | |
tree | df95581a1b965bfb2c1ab3be3b7cd3f76d84df57 | |
parent | 3949f8f90ddd0aa2bc98017b4d94692993e66d6c (diff) |
+network
-rw-r--r-- | NetWork/AsynClient.cs | 250 |
1 files changed, 250 insertions, 0 deletions
diff --git a/NetWork/AsynClient.cs b/NetWork/AsynClient.cs index e69de29..20efcc7 100644 --- a/NetWork/AsynClient.cs +++ b/NetWork/AsynClient.cs @@ -0,0 +1,250 @@ +using System; +using System.Net; +using System.Net.Sockets; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +/* +public class Messageinfo +{ + public MsgHeader header; // 消息头 + public byte[] databuffer; // 数据本体 +} + +*/ + +namespace Assets.Scripts.NetWork +{ + // 发送出去的data buffer + public class SendBufferInfo + { + public UInt32 seqId; + public byte[] buffer; + public int length; + } + + public class AsynClient + { + public const int BufferSzie = 65500; + private byte[] socketCacheBuffer = new byte[BufferSzie]; + + private int mPort; + private string mServerAddress; + + private volatile Socket mClient = null; + private volatile bool isConnectionEstablished = false; + // thread lock + private volatile object lockThis = new object(); + + //private vp_Timer.Handle timerHandle = new vp_Timer.Handle(); + + private const int SendReceiveTimeOut = 3000; + + public delegate void OnCoonectCallBack(); + public delegate void OnConnectFail(Exception e); + public delegate void OnDisconnectCallBack(); + public delegate void OnSendCallBack(SendBufferInfo bufferInfo, bool isCompleted); + public delegate void OnReceiveCallBack(byte[] buffer, int length); + + public OnCoonectCallBack connectCallBack = null; + public OnConnectFail connectFailCallBack = null; + public OnDisconnectCallBack disConnectCallBack = null; + public OnSendCallBack sendCallBack = null; + public OnReceiveCallBack receiveCallBack = null; + + // socket + public void StartClient(int port, string serverAddress, int timeOutMS) + { + lock(lockThis) + { + mPort = port; + mServerAddress = serverAddress; + + try + { + IPAddress[] ipAddresses = Dns.GetHostAddresses(mServerAddress); + if(ipAddresses.Length == 0) + { + return; + } + IPAddress ipAddress = ipAddresses[0]; + IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, mPort); + + mClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + mClient.BeginConnect(remoteEndPoint, new AsyncCallback(ConnectCallback), null); + + // + } + catch(Exception e) + { + CloseClient(); + } + } + } + + private void CloseClient() + { + try + { + if(!isConnectionEstablished) + { + + mClient.Close(); + mClient = null; + if (disConnectCallBack != null) + disConnectCallBack(); + return; + } + isConnectionEstablished = false; + if (mClient != null) + mClient.Shutdown(SocketShutdown.Both); + if(mClient != null) + { + mClient.Close(); + mClient = null; + } + if (disConnectCallBack != null) + disConnectCallBack(); + } + catch(Exception e) + { + if (disConnectCallBack != null) + disConnectCallBack(); + } + } + + // 用来设置连接超时 + private void timer_connection_tick() + { + //lock (lockThis) + //{ + // if(null == mClient || !mClient.Connected) + // { + // timerHandler.Cancel(); + // if (null != mCLient) + // CloseClient(); + // } + //} + } + + private void ConnectCallback(IAsyncResult ar) + { + lock (lockThis) + { + try + { + if(null != mClient && null != ar) + { + mClient.EndConnect(ar); + mClient.ReceiveTimeout = SendReceiveTimeOut; + mClient.SendTimeout = SendReceiveTimeOut; + + isConnectionEstablished = true; + + if (connectCallBack != null) + connectCallBack(); + + // 接收线程 + Thread receiveThread = new Thread(new ThreadStart(AsyncRecive)); + receiveThread.Start(); + } + else + { + } + } + catch(Exception e) + { + if (connectFailCallBack != null) + connectFailCallBack(e); + CloseClient(); + } + } + } + + private void AsyncRecive() + { + try + { + while (true) + { + lock (lockThis) + { + if (!isConnectionEstablished) + break; + if (null == mClient || !mClient.Connected) + break; + if (mClient.Available > 0) + { + int bytesRead = mClient.Receive(socketCacheBuffer, 0, socketCacheBuffer.Length, SocketFlags.None); + if (bytesRead > 0) + { + if (receiveCallBack != null) + { + receiveCallBack(socketCacheBuffer, bytesRead); + } + else + { + } + } + else + { + // 服务器主动断线 + CloseClient(); + break; + } + } + } + Thread.Sleep(10); + } + } + catch(Exception e) + { + + } + } + + public void Send(SendBufferInfo sendBuffer) + { + lock (lockThis) + { + if(sendBuffer.length <= 0 || sendBuffer.length > sendBuffer.buffer.Length) + { + sendCallBack(sendBuffer, true); + return; + } + + try + { + if(!isConnectionEstablished) + { + sendCallBack(sendBuffer, false); + return; + } + + //NetWorkData.totalSendBytes += sendBuffer.length; // 统计发送的字节 + mClient.Send(sendBuffer.buffer, 0, sendBuffer.length, SocketFlags.None); + sendCallBack(sendBuffer, true); + + } + catch(Exception e) + { + sendCallBack(sendBuffer, false); + CloseClient(); + } + } + } + + public void CloseNetWorkConnect() + { + lock (lockThis) + { + CloseClient(); + } + } + + + } +} + |