private int AssembleTcpMessage() { if (tcpSendingQueueInThread.Count <= 0) { return(0); } int length = 0; while (length < NetworkConstants.MAX_PACKET_SIZE && tcpSendingQueueInThread.Count > 0) { ClientTcpMessage message = tcpSendingQueueInThread.Peek(); Byte[] data = message.Encode(); int dataLength = data.Length; DebugUtils.Assert(dataLength <= NetworkConstants.MAX_PACKET_SIZE, string.Format("The length {0} of the tcp client message {1} exceeds the limitation!", dataLength, message.protocalCode)); if (length + dataLength < NetworkConstants.MAX_PACKET_SIZE) { //DebugUtils.Log( DebugUtils.Type.SocketNet, "Assemble protocol = " + message.protocolCol ); OnRequestSuccess += message.OnRequestSuccess; OnRequestFail += message.OnRequestFailed; Buffer.BlockCopy(data, 0, tcpSendBuffer, length, dataLength); length += dataLength; tcpSendingQueueInThread.Dequeue(); } else { break; } } return(length); }
public static void StartWaiting(ServerType type, ClientTcpMessage msg, int resendTimes) { NetworkWaitingItem item = new NetworkWaitingItem(); item.message = msg; item.timer = 0; item.resendTimes = resendTimes; item.state = NetworkItemState.BeginToWait; waitingMessages[type].Add(msg.sequence, item); DebugUtils.Log(DebugUtils.Type.NetAlert, String.Format("sending protocol {0} with seq {1}", msg.protocalCode, msg.sequence)); //Debug.Log( string.Format( "StartWaiting: {0} resendTimes:{1} waitingBackMsgQueue.Count:{2}", msg.protocalCode, resendTimes, waitingBackMsgQueue.Count ) ); }
public void SendRequest(MsgCode protocolCode, byte[] data, ClientType type = ClientType.Tcp, Action onRequestSuccess = null, Action onResquestFailed = null, int resendTimes = 1) { DebugUtils.Log(DebugUtils.Type.Protocol, "try to send protocol " + protocolCode); if (notifySendThreadClosed) { DebugUtils.Log(DebugUtils.Type.Protocol, "Network message thread has been shutdown when trying to send protocol " + protocolCode); return; } if (type == ClientType.Tcp) { ClientTcpMessage message = new ClientTcpMessage((int)protocolCode, data, tcpSequence++); if (onRequestSuccess != null) { message.OnRequestSuccess += onRequestSuccess; } if (onResquestFailed != null) { message.OnRequestFailed += onResquestFailed; } tcpSendingQueue.Enqueue(message); NetworkAlert.StartWaiting(serverType, message, resendTimes); } else if (type == ClientType.Udp) { ClientUdpMessage[] messages = ClientUdpMessage.CreateUdpMessages((int)protocolCode, ref udpSequence, udpAck, data); int j = 0; for ( ; j < messages.Length; j++) { udpSendingQueue.Enqueue(messages[j]); } if (onRequestSuccess != null) { messages[j].OnRequestSuccess += onRequestSuccess; } if (onResquestFailed != null) { messages[j].OnRequestFailed += onResquestFailed; } } else { DebugUtils.LogError(DebugUtils.Type.AsyncSocket, string.Format("There is no such client type {0} to send request!", type)); } }