private IEnumerator BeginSendPacket <T>(int network_message, T packet, bool timeout_message, byte[] msg_index_bytes) where T : global::ProtoBuf.IExtensible { // 每一条消息的的下标 string msg_id = BitConverter.ToString(msg_index_bytes); lock (_msg_index_map) { _msg_index_map.Add(msg_id, packet); } NetLog.Log("发送消息 : {0} 消息下标 :{1}", network_message, msg_id); DoBeginSendPacket <T>(network_message, packet, msg_index_bytes); yield return(CoroutineConst.GetWaitForSeconds(NetworkConst.REQ_TIME_OUT)); // 等待超时时间之后,检测超时情况 LogManager.Error("超时:[{0}]", network_message); // 移除相关的回调信息 lock (_msg_index_map) { bool result = RemoveMsgIndex(msg_id); if (result && timeout_message)// 然后提示超时 { NetworkEventSystem.Instance.RaiseEvent(E_NetModule.req_timeout, null); } } }
private void EndSendPacket(IAsyncResult ar) { try { lock (_socket) { _socket.EndSend(ar); } } catch (Exception ex) { NetLog.Error(ex.Message); } }
/// <summary> /// 序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public static byte[] Serialize <T>(T t) { NetLog.Log("send:[{0}]", t); using (MemoryStream ms = new MemoryStream()) { //Serializer.Serialize<T>(ms, t); byte[] pBuffer = ms.ToArray(); return(pBuffer); } }
// 接受包数据 public void BeginReceivePacket() { try { lock (_socket) { _socket.BeginReceive(_receive_buffer, 0, _socket.ReceiveBufferSize, SocketFlags.None, EndReceivePacket, null); } } catch (Exception ex) { NetLog.Error(ex.StackTrace); } }
// 连接成功,开始接受数据 private void OnSocketConnected(Object param) { NetLog.Log("连接成功,开始接受数据"); if (_receive_buffer == null) { _receive_buffer = new byte[_socket.ReceiveBufferSize]; } _is_keep_alive = true; BeginReceivePacket(); }
/// <summary> /// 当无法接收到心跳包的时候尝试重新连接服务器 /// </summary> public IEnumerator BeginTryConnect() { LogManager.Log("尝试连接"); //yield return null; while (_socket == null || !_socket.Connected || !_is_keep_alive) { NetLog.Log("开始连接"); CloseConnection(); yield return(StartCoroutineManager.Start(BeginConnection())); } /*LogManager.Log("目前没有检测心跳"); * while (_is_keep_alive) * { * NetLog.Log("检测_is_keep_alive:[{0}]", _is_keep_alive); * _is_keep_alive = false; * yield return CoroutineConst.GetWaitForSeconds(NetworkConst.KEEP_ALIVE_TIME_OUT); * } * * NetworkEventSystem.Instance.RaiseEvent(E_NetModule.socket_disconnected, null);*/ }
/// <summary> /// /// </summary> /// <param name="ar"></param> public void EndReceivePacket(IAsyncResult ar) { int bytes_read = -1; try { if (IsConncted) { lock (_socket) { bytes_read = _socket.EndReceive(ar); } } if (bytes_read == -1) { CloseConnection(); return; } } catch (ObjectDisposedException) { NetLog.Error("Receive Closed"); } catch (Exception ex) { NetLog.Error("{0}\n{1}\n{2}", ex.Message, ex.StackTrace, ex.Source); } NetLog.Assert(bytes_read < NetworkConst.MAX_BUFF_SIZE, "接收到的包太长了"); // Begin Read // int position = 0; // MsgID : 4 | PacketLength : 4 |PACKET : dynamic while (position < bytes_read) { int buffer_size = NetworkHelper.ReadBufferSize(_receive_buffer, ref position /*position + HEAD_SIZE * 0*/); int msg_id = NetworkHelper.ReadMsgId(_receive_buffer, ref position); int msg_data_size = NetworkHelper.ReadPbLength(_receive_buffer, ref position); // TODO 确认心跳线程 // KEEP_ALIVE_SYNC // 保证整个Buffer长度 if (position + msg_data_size > bytes_read) { LogManager.Error("Error receive packet, packet is too long : " + buffer_size); break; } // 接包; IExtensible rsp_packet = UnPackTool.UnPack1(ref position, msg_data_size, _receive_buffer, msg_id); if (rsp_packet == null) { continue; } // 确认某一个消息已经Req_Finish RaiseMessage(); // position += msg_data_size; } Array.Clear(_receive_buffer, 0, _socket.ReceiveBufferSize); BeginReceivePacket(); }
private void OnRequestFinish(Object param) { NetLog.Log("OnRequestFinish"); }
// 断开连接,请求重新连接 private void OnSocketDisconnected(Object param) { NetLog.Log("断开连接,请求重新连接"); StartCoroutineManager.Start(BeginTryConnect()); }
public static void Serialize1 <T>(MemoryStream ms, T t) { NetLog.Log("send:[{0}]", t); //Serializer.Serialize<T>(ms, t); }