示例#1
0
//		const int output_max_len = 2000;
//		static byte[] output_buffer = new byte[output_max_len];
        public static void SendByProtoBuf(PacketDistributed packet)
        {
            WebClient client = null;

            try
            {
                int    opcode = (int)packet.getMessageID();
                byte[] data   = packet.ToByteArray();
                Debug.Log("send packet opcode[" + packet.getMessageID() + "] data.Length=" + data.Length);
                byte[] output;
                if (ClientConfigure.useMrdCompress)
                {
                    output = data;
                }
                else
                {
                    output = data;
                }
                //------------尝试接入消息包压缩库结束-----------
                client = new WebClient();
                client.Headers.Set("opcode", "" + opcode);
                client.Headers.Set(KEY_GAME_SESSION, sessionId);
                Debug.Log("sessionId = " + sessionId);
                client.UploadDataCompleted += new UploadDataCompletedEventHandler(uploadComplate);

                client.UploadDataAsync(uri, output);

                Debug.Log("Send opcode = " + client.Headers.ToString());
            }
            catch (WebException we) {            //超时
                Debug.LogError(we.ToString());
                if (client != null)
                {
                    client.CancelAsync();
                }
                NetworkSender.Instance().sendFinish(false);
            }
            catch (SocketException e)
            {
                Debug.LogError(e.ToString());
                NetworkSender.Instance().sendFinish(false);
            }
        }