Example #1
0
    protected void SendHubPacket <PT>(PT packet)
        where PT : class
    {
        PacketCore _PacketHub = new PacketCore();

        _PacketHub.Name = typeof(PT).Name;
        _PacketHub.Data = JsonConvert.SerializeObject(packet);
        Debug.LogFormat("(Send) [{0}] [{1}] : [{2}]", hub.Name, _PacketHub.Name, _PacketHub.Data);
        hub.Call("SendPacket", _PacketHub.Name, _PacketHub.Data);
    }
Example #2
0
    void JsonAsyncInternal <SendT, RecvT>(System.Uri uri, string packet_namespace, SendT packet, Action <SendT, RecvT> callback, bool use_crc)
        where SendT : class
        where RecvT : class
    {
        if (uri == null)
        {
            throw new System.Exception("Please call CS_Client.InitServer");
        }

        PacketCore _PacketCore = new PacketCore();

        _PacketCore.Name = typeof(SendT).FullName;

        if (_PacketCore.Name.StartsWith(packet_namespace) == false)
        {
            throw new System.Exception(string.Format("Packet namesapce fault({0}) : {1}", packet_namespace, _PacketCore.Name));
        }

        _PacketCore.Data = JsonConvert.SerializeObject(packet, Formatting.None, NetworkCore.PacketUtil.json_ops);

        HTTPRequest request = new HTTPRequest(uri, HTTPMethods.Post, new Handler <SendT, RecvT>(this, callback, packet).handle);

        foreach (var header in m_Headers)
        {
            request.AddHeader(header.Key, header.Value);
        }
        request.RawData = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(_PacketCore, Formatting.None, NetworkCore.PacketUtil.json_ops));

        Debug.Log(string.Format("[Packet:{0},{2}] {1}", _PacketCore.Name, _PacketCore.Data, SendIndex));
        if (PostCallback != null && callback != null)
        {
            Networking.Instance.Block();
        }

        if (use_crc == true && SendIndex > 0)
        {
            uint checksum = Octodiff.Core.Adler32RollingChecksum.CalculateChecksum(System.Text.Encoding.UTF8.GetBytes(_PacketCore.Name));
            checksum = Octodiff.Core.Adler32RollingChecksum.Add(checksum, System.Text.Encoding.UTF8.GetBytes(_PacketCore.Data));
            checksum = Octodiff.Core.Adler32RollingChecksum.Add(checksum, BitConverter.GetBytes(AccountIdx));
            checksum = Octodiff.Core.Adler32RollingChecksum.Add(checksum, BitConverter.GetBytes(AccessToken));
            checksum = Octodiff.Core.Adler32RollingChecksum.Add(checksum, BitConverter.GetBytes(SendIndex));
            checksum = Octodiff.Core.Adler32RollingChecksum.Add(checksum, BitConverter.GetBytes(checksum));
            request.AddHeader("Checksum", checksum.ToString());
            ++SendIndex;
        }

        request.Send();
    }
Example #3
0
    void OnReceivePacket(Hub hub, MethodCallMessage message)
    {
        PacketCore packet = new PacketCore();

        packet.Data = ((IDictionary)message.Arguments[0])["Data"].ToString();
        packet.Name = ((IDictionary)message.Arguments[0])["Name"].ToString();

        Debug.LogFormat("(Recv) [{0}] [{1}] : [{2}]", hub.Name, packet.Name, packet.Data);

        IHubHandler handler;

        if (m_handlers.TryGetValue(packet.Name, out handler) == true)
        {
            handler.Handler(packet);
        }
    }
Example #4
0
        public void handle(HTTPRequest request, HTTPResponse response)
        {
            if (request.State != HTTPRequestStates.Finished)
            {
                //                 if (request.Exception is System.Net.Sockets.SocketException)
                //                 {
                //                     System.Net.Sockets.SocketException socket_exception = request.Exception as System.Net.Sockets.SocketException;
                //                     client.handle_error(Name, socket_exception.SocketErrorCode.ToString(), "Network Error");
                //                 }
                //                 else
                {
                    Debug.LogWarningFormat("Error in {0} : [ServerError:{1}] {2}", Name, request.State, request.Exception != null ? request.Exception.Message : "Network Error");

                    client.handle_error(Name, request, null);
                }
                return;
            }

            if (response.IsSuccess == false)
            {
//                 switch(response.StatusCode)
//                 {
//                     case 500:
//                         return;
//                 }
                Debug.LogWarningFormat("Error in {0} : [ServerError:{1}] {2}", Name, response.StatusCode, response.Message);
                client.handle_error(Name, request, response);
                return;
            }

            PacketCore packet_core = JsonConvert.DeserializeObject <PacketCore>(response.DataAsText, NetworkCore.PacketUtil.json_ops);

            if (packet_core.Name != typeof(RecvT).FullName)
            {
                if (client.HandleCommon(request, send_packet, packet_core) == true)
                {
                    return;
                }
                else
                {
                    if (client.PostCallback != null)
                    {
                        client.PostCallback();
                    }
                    throw new System.Exception(string.Format("packet name({0}) != {1}", packet_core.Name, typeof(RecvT).FullName));
                }
            }

            if (packet_core.PrePackets != null && packet_core.PrePackets.Count > 0)
            {
                foreach (var pre_packet in packet_core.PrePackets)
                {
                    Debug.LogFormat("[PacketPre:{0}] {1}", pre_packet.Name, pre_packet.Data);
                    if (client.HandlePre(request, send_packet, pre_packet) == false)
                    {
                        Debug.LogErrorFormat("can't find pre packet handler : {0}", pre_packet.Name);
                    }
                }
            }

            if (packet_core.Data.Length > 1000)
            {
                Debug.Log(string.Format("[Packet:{0}] {1}", typeof(RecvT).FullName, packet_core.Data.Substring(0, 1000) + "...."));
            }
            else
            {
                Debug.Log(string.Format("[Packet:{0}] {1}", typeof(RecvT).FullName, packet_core.Data));
            }
            if (callback != null)
            {
                if (client.PostCallback != null)
                {
                    client.PostCallback();
                }
                callback(send_packet, JsonConvert.DeserializeObject <RecvT>(packet_core.Data, NetworkCore.PacketUtil.json_ops));
            }

            if (packet_core.PostPackets != null && packet_core.PostPackets.Count > 0)
            {
                foreach (var post_packet in packet_core.PostPackets)
                {
                    if (client.HandlePost(request, send_packet, post_packet) == false)
                    {
                        Debug.LogErrorFormat("can't find post packet handler : {0}", post_packet.Name);
                    }
                    else
                    {
                        Debug.LogFormat("[PacketPost:{0}] {1}", post_packet.Name, post_packet.Data);
                    }
                }
            }
        }
Example #5
0
 public void Handler(PacketCore packet)
 {
     callback(JsonConvert.DeserializeObject <RecvT>(packet.Data));
 }