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); } } }
/// <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); } }
// 连接成功,开始接受数据 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);*/ }
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); }