Esempio n. 1
0
    /// <summary>
    /// 发送数据
    /// </summary>
    /// <param name="main"></param>
    /// <param name="sub"></param>
    /// <param name="buffer"></param>
    /// <param name="wSize"></param>
    /// <param name="callback"></param>
    /// <param name="param"></param>
    public void SendScoketData(int main, int sub, byte[] buffer = null, int wSize       = 0,
                               Action <Hashtable> callback      = null, Hashtable param = null)
    {
        MyDebug.SocketLog("Send-------------------------------------------------------------------- --main: " +
                          main + "-- Sub:" + sub);
        if (null == buffer)
        {
            wSize = 0;
        }
        MyDebug.SocketLog("wSize:" + wSize);
        var headSize = Marshal.SizeOf(typeof(TCP_Head));
        var SendSize = wSize + headSize;
        var head     = new TCP_Head();

        head.CommandInfo.wMainCmdID = (ushort)main;
        head.CommandInfo.wSubCmdID  = (ushort)sub;
        head.TCPInfo.wPacketSize    = (ushort)SendSize;
        head.TCPInfo.cbDataKind     = 0x05;
        var SendBuffer = new byte[NetUtil.SOCKET_TCP_BUFFER];
        var Headbuffer = NetUtil.StructToBytes(head);

        MyDebug.SocketLog("Headbuffer.Length:" + Headbuffer.Length);
        MyDebug.SocketLog("SendBuffer.Length:" + SendBuffer.Length);
        MyDebug.SocketLog("headSize:" + headSize);
        Array.Copy(Headbuffer, SendBuffer, headSize);
        if (null != buffer)
        {
            Array.Copy(buffer, 0, SendBuffer, headSize, wSize);
        }
        //加密
        SendSize = NetUtil.EncryptBuffer(SendBuffer, SendSize);
        SendMessage(SendBuffer, SendSize);
    }
Esempio n. 2
0
        private void handleNetMsg(byte[] msgBytes)
        {
            int      nHeadSize   = Marshal.SizeOf(typeof(TCP_Head));
            TCP_Head tcpHead     = (TCP_Head)NetUtil.BytesToStruct <TCP_Head>(msgBytes, nHeadSize);
            int      nBufferSize = msgBytes.Length - nHeadSize;

            byte[] buffer = new byte[nBufferSize];
            Array.Copy(msgBytes, nHeadSize, buffer, 0, nBufferSize);

            LogUtil.LogInfo(string.Format(">>>>>>>>>>    收到客户端数据长度:{0}   主命令:{1}  子命令:{2}", msgBytes.Length, tcpHead.Cmd.Main_ID, tcpHead.Cmd.Sub_ID));
            NetMessageCenter.Ins.DispatchMsg(this, (int)tcpHead.Cmd.Main_ID, (int)tcpHead.Cmd.Sub_ID, buffer);
        }
Esempio n. 3
0
    private void ProcessNetEvent(byte[] msgBytes)
    {
        int      nHeadSize   = Marshal.SizeOf(typeof(TCP_Head));
        TCP_Head tcpHead     = (TCP_Head)NetDefine.BytesToStru <TCP_Head>(msgBytes);
        int      nBufferSize = msgBytes.Length - nHeadSize;

        byte[] buffer = new byte[nBufferSize];
        Array.Copy(msgBytes, nHeadSize, buffer, 0, nBufferSize);


        Debug.Log(string.Format("<color=yellow>收到服务器数据长度:{0}   主命令:{1}  子命令:{2} </color>", msgBytes.Length, tcpHead.Cmd.Main_ID, tcpHead.Cmd.Sub_ID));
        GApp.NetMgr.AddNetMessage(tcpHead.Cmd.Main_ID, tcpHead.Cmd.Sub_ID, buffer);
    }
Esempio n. 4
0
        public void SendAsync(ushort main_id, ushort sub_id, byte[] data = null, Action <Hashtable> callback = null, Hashtable hashtable = null)
        {
            TCP_Head tcpHead = new TCP_Head();

            tcpHead.Cmd.Main_ID = main_id;
            tcpHead.Cmd.Sub_ID  = sub_id;
            int nHeadSize = Marshal.SizeOf(tcpHead);

            int nSendSize = nHeadSize;

            if (data != null)
            {
                nSendSize += data.Length;
            }
            tcpHead.Info.Buffer_Size = (ushort)nSendSize;

            byte[] SendBuffer = new byte[nSendSize];
            var    Headbuffer = NetUtil.StructToBytes(tcpHead);

            Array.Copy(Headbuffer, SendBuffer, nHeadSize);
            if (data != null)
            {
                Array.Copy(data, 0, SendBuffer, nHeadSize, data.Length);
            }

            lock (socket)
            {
                socket.BeginSend(SendBuffer, 0, nSendSize, SocketFlags.None, (ar) =>
                {
                    try
                    {
                        socket.EndSend(ar);
                        callback?.Invoke(hashtable);
                        LogUtil.LogInfo(string.Format("<<<<<<<<<<    向客户端发送数据,长度 :{0}   主命令:{1}   子命令:{2}", SendBuffer.Length,
                                                      main_id, sub_id));
                    }
                    catch (SocketException e)
                    {
                        Console.WriteLine("Send失败:" + e.Message);
                        Close();
                    }
                }, socket);
            }
        }
Esempio n. 5
0
    public void SendAsync(ushort main_id, ushort sub_id, byte[] data = null, Action <Hashtable> callback = null, Hashtable hashtable = null)
    {
        TCP_Head tcpHead = new TCP_Head();

        tcpHead.Cmd.Main_ID = main_id;
        tcpHead.Cmd.Sub_ID  = sub_id;
        int nHeadSize = Marshal.SizeOf(tcpHead);

        int nSendSize = nHeadSize;

        if (data != null)
        {
            nSendSize += data.Length;
        }
        tcpHead.Info.Buffer_Size = (ushort)nSendSize;

        byte[] SendBuffer = new byte[nSendSize];
        var    Headbuffer = NetDefine.StruToBytes(tcpHead);

        Array.Copy(Headbuffer, SendBuffer, nHeadSize);
        if (data != null)
        {
            Array.Copy(data, 0, SendBuffer, nHeadSize, data.Length);
        }

        socket.BeginSend(SendBuffer, 0, nSendSize, SocketFlags.None, (ar) =>
        {
            try
            {
                socket.EndSend(ar);
                if (callback != null)
                {
                    callback(hashtable);
                }
            }
            catch (SocketException se)
            {
                ReConn(se.ErrorCode);
            }
        }, socket);
    }