Beispiel #1
0
        private byte[] AssembleUdpMessage(ref long seq)
        {
            if (udpSendingQueueInThread.Count <= 0)
            {
                return(null);
            }

            ClientUdpMessage message = udpSendingQueueInThread.Dequeue();

            seq = message.sequence;
            return(message.Encode());
        }
Beispiel #2
0
        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));
            }
        }
        public static ClientUdpMessage[] CreateUdpMessages(int protocalCode, ref long sequence, long ack, byte[] data)
        {
            int length = data.Length;
            int num    = (length - 1) / NetworkConstants.MAX_SEGMENT_SIZE;

            if (num >= 0)
            {
                num += 1;
                ClientUdpMessage[] messages = new ClientUdpMessage[num];
                if (num == 1)
                {
                    messages[0] = new ClientUdpMessage(protocalCode, sequence, ack, sequence++, data);
                }
                else
                {
                    int  totalSize   = 0;
                    long endSequence = sequence + num;
                    for (int j = 0; j < num; j++)
                    {
                        if (j < num - 1)
                        {
                            byte[] d = new byte[NetworkConstants.MAX_SEGMENT_SIZE];
                            Buffer.BlockCopy(data, totalSize, d, 0, NetworkConstants.MAX_SEGMENT_SIZE);
                            messages[j] = new ClientUdpMessage(protocalCode, sequence++, ack, endSequence, d);
                            totalSize  += NetworkConstants.MAX_SEGMENT_SIZE;
                        }
                        else
                        {
                            DebugUtils.Assert(sequence == endSequence, string.Format("the ending sequence {0} isn't right! the num = {1}", endSequence, num));
                            int    len = length - totalSize;
                            byte[] d   = new byte[len];
                            Buffer.BlockCopy(data, totalSize, d, 0, len);
                            messages[j] = new ClientUdpMessage(protocalCode, sequence++, ack, endSequence, d);
                        }
                    }
                }

                return(messages);
            }
            else
            {
                return(null);
            }
        }