private void ThreadFunction_Recv() { try { while (_thread_running && isConnected) { var bufraw = _inner_socket.Receive(ref _server_endpoint); if (bufraw.Length > 4) { byte[] buf = Base.MemoryPool.Alloc(bufraw.Length - 4); //new byte[bufraw.Length - 4]; Array.Copy(bufraw, 4, buf, 0, bufraw.Length - 4); XOREncrypt.Decrypt(buf, bufraw.Length - 4); MsgStream msg = MsgStream.Create(buf, bufraw.Length - 4); if (msg.IsCustomCmd) { if (msg.CustomType == CustomMsgType.ping) { //服务器返回了 证明连接成功了 _IsConnected = true; TimeSpan ts = DateTime.Now - mLastPingTime;//mLastPingTimes[idx]; mPing = (int)(ts.TotalMilliseconds); mLastPingTime = DateTime.Now; // this.LastSendPingTimestamp = Utils.GetTimestampSecondsInt(); msg.Dispose(); continue; } } //服务器返回了 证明连接成功了 _IsConnected = true; _recvQueue.Enqueue(msg); } else { //<=4 } } } catch (SocketException e) { Debug.Log("UdpSocket:" + e.Message + " " + e.ErrorCode + " " + e.NativeErrorCode + " " + e.SocketErrorCode); } catch (Exception e) { Debug.Log("UdpSocket:" + e.Message); } this.Disconnected(); while (_recvQueue.Empty() == false) { var x = _recvQueue.Dequeue(); if (x != null) { x.Dispose(); } } Debug.Log("[NetWork]:UdpSocket Recv Thread Close"); }
private void ThreadFunction_Recv() { try { _inner_socket.NoDelay = true; while (_thread_running && isConnected) { // Thread.Sleep(1);//能减少socket error 10054 出现的 概率 byte[] buffer = _buffer_head; int c = 0; bool will_break = false; while (c < 4) { int l = _inner_socket.Receive(buffer, c, 4 - c, SocketFlags.None); // _inner_tcp_stream.Read(buffer, c, 4 - c); if (l > 0) { c += l; } else if (l == 0) { this.Disconnected(); Debug.Log("socket read faild 4 server disconnected"); will_break = true; break; } else { this.Disconnected(); Debug.Log("socket read faild 1"); will_break = true; break; } } if (will_break) { break; } if (!isConnected || !_thread_running) { break; } int len = (int)System.BitConverter.ToInt32(buffer, 0); if (len < MAX_VALID_BUFFER_LEN) { //byte[] buf = new byte[len]; byte[] buf = Base.MemoryPool.Alloc(len); try { int read_len = 0; while (read_len < len) { int l = _inner_socket.Receive(buf, read_len, len - read_len, SocketFlags.None);// _inner_tcp_stream.Read(buf, read_len, len - read_len); if (l > 0) { read_len += l; } else if (l == 0) { /* * //https://msdn.microsoft.com/zh-cn/library/8s4y8aff(v=vs.90).aspx * 如果当前使用的是面向连接的 Socket,那么 Receive 方法将会读取所有可用的数据,直到达到缓冲区的大小为止。如果远程主机使用 Shutdown 方法关闭了 Socket 连接,并且所有可用数据均已收到,则 Receive 方法将立即完成并返回零字节。*/ break; } else { break; } } if (read_len != len) { this.Disconnected(); Debug.Log("socket read faild 2"); break; } XOREncrypt.Decrypt(buf, len); byte by = buf[0]; if (by == 1) // 占位符 { MsgStream msg = MsgStream.Create(buf, len); if (msg.IsCustomCmd) { if (msg.CustomType == CustomMsgType.ping) { TimeSpan ts = DateTime.Now - mLastPingTime;//mLastPingTimes[idx]; mPing = (int)(ts.TotalMilliseconds); mLastPingTime = DateTime.Now; // this.LastSendPingTimestamp = Utils.GetTimestampSecondsInt(); msg.Dispose(); continue; } } _recvQueue.Enqueue(msg); } else { //maybe dicconnected by server or net error Debug.LogError("****************** unexp bytes headdrer=" + (int)by + " len=" + len + " read_len=" + read_len); this.Disconnected(); } } catch (Exception e) { //不回收 让他GC掉好了 } } else { this.Disconnected(); Debug.LogError("TcpSocket error send buffer overfollow 0x2 "); } } } catch (SocketException e) { Debug.Log("TcpSocket:" + e.Message + " " + e.ErrorCode + " " + e.NativeErrorCode + " " + e.SocketErrorCode); } catch (Exception e) { Debug.Log("TcpSocket:" + e.Message); } this.Disconnected(); while (_recvQueue.Empty() == false) { var x = _recvQueue.Dequeue(); if (x != null) { x.Dispose(); } } Debug.Log("[NetWork]:Socket Recv Thread Close"); }
private static byte[] _buffer_send = new byte[4096]; // 4kb buffer cache ,avoid memory-alloc private void ThreadFunction_Send() { MsgStream ms = null; try { _inner_socket.NoDelay = true; while (_thread_running && isConnected) { Thread.Sleep(1); if (ms != null) { ms.Dispose(); ms = null; } while (_sendQueue.Empty() == false && isConnected && _thread_running) { ms = _sendQueue.Dequeue(); if (ms == null) { continue; } //减小 缓冲区 异常 的概率 //TODO 考虑改为 异步IO bool ok = false; using (ms) { byte[] buffer = null; buffer = ms.stream.buffer; XOREncrypt.Encrypt(buffer, buffer.Length); int len = ms.stream.Length; if (len < MAX_VALID_BUFFER_LEN) { try { byte[] buf = System.BitConverter.GetBytes(len); len = buf.Length + len; buf.CopyTo(_buffer_send, 0); //write data len buffer.CopyTo(_buffer_send, buf.Length); //write data // Debug.LogError("send cus " + ms.Msg + "len " + send.Length); // Debug.LogWarning("send buffer len=" + len); int send_len = 0; while (send_len < len) { int l = _inner_socket.Send(_buffer_send, send_len, len - send_len, SocketFlags.None); if (l >= 0) { send_len += l; } else { break; } } if (send_len == len) { ok = true; } } catch (Exception e) { ok = false; } } } ms = null; if (!ok) { Debug.LogError("TcpSocket error send buffer overfollow 0x1 "); this.Disconnected(); break; } } } this.Disconnected(); } catch (Exception e) { Debug.Log(e); this.Disconnected(); } if (ms != null) { ms.Dispose(); ms = null; } while (_sendQueue.Empty() == false) { var x = _sendQueue.Dequeue(); if (x != null) { x.Dispose(); } } Debug.Log("[NetWork]:Socket Send Thread Close"); }