Exemplo n.º 1
0
        public void SendUDPPacket(MsgBuffer mb)
        {
            var send = false;

            lock (msgBuffers)
            {
                msgBuffers.Enqueue(mb);
                if (msgBuffers.Count == 1)
                {
                    send = true;
                }
            }
            if (send)
            {
                try
                {
                    udpClient.BeginSend(mb.buffer, mb.buffer.Length, mb.remoteEnd, OnSend, null);
                }
                catch (Exception exp)
                {
                    LogHelper.Log("UDP", exp.ToString());
                    DequeueMsg();
                }
            }
        }
Exemplo n.º 2
0
        public void SendPacket(IBuilderLite retpb)
        {
            if (udpClient == null)
            {
                return;
            }
            if (IsClose)
            {
                return;
            }

            //LogHelper.Log("UDP", "SendPacket: "+retpb.ToString());

            ServerBundle bundle;
            var          bytes = ServerBundle.sendImmediateError(retpb, 0, 0, out bundle);

            ServerBundle.ReturnBundle(bundle);

            var mb = new MsgBuffer()
            {
                position = 0, buffer = bytes, bundle = bundle, remoteEnd = remoteEnd
            };

            socketServer.SendUDPPacket(mb);
        }
Exemplo n.º 3
0
        public void SendBytes(byte[] bytes)          //发送数据
        {
            mSendPacketCount     += 1;
            mSendPacketSizeCount += (ulong)bytes.Length;

            var mb = new MsgBuffer()
            {
                position = 0, buffer = bytes, bundle = null
            };
            var send = false;

            lock (msgBuffer)
            {
                msgBuffer.Add(mb);
                if (msgBuffer.Count == 1)
                {
                    send = true;
                }
            }
            if (send)
            {
                try
                {
                    mSocket.BeginSend(mb.buffer, mb.position, mb.Size, SocketFlags.None, OnSend, null);
                }
                catch (Exception exception)
                {
                    Debug.LogError(exception.Message);
                    Close();
                }
            }
        }
Exemplo n.º 4
0
        private void SendThread()
        {
            while (!IsClose && !callCloseYet)
            {
                //var tn = Thread.CurrentThread.ManagedThreadId;
                //Debug.LogError("ThreadN: "+tn);
                //var st = Util.GetTimeNow();

                signal.WaitOne();
                if (IsClose)
                {
                    break;
                }
                MsgBuffer mb = null;
                lock (msgBuffer)
                {
                    if (msgBuffer.Count > 0)
                    {
                        mb = msgBuffer.Dequeue();
                    }
                }
                if (mb != null)
                {
                    try
                    {
                        client.GetStream().Write(mb.buffer, mb.position, mb.Size);

                        /*
                         * ml.queueInLoop(() =>
                         * {
                         *  Bundle.ReturnBundle(mb.bundle);
                         * });
                         */
                    }
                    catch (Exception exception)
                    {
                        callCloseYet = true;
                        ml.queueInLoop(() => {
                            Debug.LogError(exception.ToString());
                            Close();
                        });
                    }
                }

                lock (msgBuffer)
                {
                    if (msgBuffer.Count <= 0)
                    {
                        signal.Reset();
                    }
                }
                //var et = Util.GetTimeNow();
                //Debug.LogError("DiffTime: "+(et-st));
            }
        }
Exemplo n.º 5
0
 public void Send(byte[] data, Bundle bundle)
 {
     lock (msgBuffer)
     {
         var mb = new MsgBuffer()
         {
             position = 0, buffer = data, bundle = null
         };
         Bundle.ReturnBundle(bundle);
         msgBuffer.Enqueue(mb);
     }
     signal.Set();
 }
Exemplo n.º 6
0
        void OnSend(IAsyncResult result)
        {
            int num = 0;

            try
            {
                num = mSocket.EndSend(result);
            } catch (Exception exception)
            {
                num = 0;
                Close();
                Debug.LogError(exception.ToString());
                return;
            }
            lock (msgBuffer)
            {
                if (mSocket != null && mSocket.Connected)
                {
                    var       mb         = msgBuffer [0];
                    MsgBuffer nextBuffer = null;
                    if (mb.Size <= num)
                    {
                        msgBuffer.RemoveAt(0);
                        if (msgBuffer.Count > 0)
                        {
                            nextBuffer = msgBuffer [0];
                        }
                    }
                    else if (mb.Size > num)
                    {
                        mb.position += num;
                        nextBuffer   = mb;
                    }
                    if (nextBuffer != null)
                    {
                        try
                        {
                            mSocket.BeginSend(nextBuffer.buffer, nextBuffer.position, nextBuffer.Size, SocketFlags.None, OnSend, null);
                        } catch (Exception exception)
                        {
                            Debug.LogError(exception.ToString());
                            Close();
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        public void SendBytes(byte[] bytes)
        {
            if (udpClient == null)
            {
                return;
            }
            if (IsClose)
            {
                return;
            }
            var mb = new MsgBuffer()
            {
                position = 0, buffer = bytes, bundle = null, remoteEnd = remoteEnd
            };

            socketServer.SendUDPPacket(mb);
        }
Exemplo n.º 8
0
        private void OnSend(IAsyncResult result)
        {
            bool error = false;

            try
            {
                udpClient.EndSend(result);
            }
            catch (Exception exp)
            {
                LogHelper.Log("Error", exp.ToString());
                DequeueMsg();
                error = true;
            }

            if (udpClient != null)
            {
                MsgBuffer nextBuffer = null;
                lock (msgBuffers)
                {
                    if (!error)
                    {
                        msgBuffers.Dequeue();
                    }
                    if (msgBuffers.Count > 0)
                    {
                        nextBuffer = msgBuffers.Peek();
                    }
                }

                if (nextBuffer != null)
                {
                    try
                    {
                        udpClient.BeginSend(nextBuffer.buffer, nextBuffer.buffer.Length, nextBuffer.remoteEnd, OnSend, null);
                    }
                    catch (Exception exp)
                    {
                        LogHelper.Log("UDP", exp.ToString());
                        DequeueMsg();
                    }
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 内部Actor将Agent要发送的消息推送给客户端
        /// SendPacket 应该以SendBuff行驶发送
        /// 同一个Socket的Write Read只能加入一次 epoll
        /// Read在初始化的时候加入
        /// Write在每次要写入的时候加入
        /// </summary>
        public void SendPacket(IBuilderLite retpb, byte flowId, byte errorCode)          //序列化Protobuff后发送数据
        {
            if (isClose)
            {
                return;
            }

            var          proto  = retpb as GCPlayerCmd.Builder;
            var          result = proto.Result;
            ServerBundle bundle;
            var          bytes = ServerBundle.sendImmediateError(retpb, flowId, errorCode, out bundle);

            //Debug.Log ("SendBytes: " + bytes.Length);
            mSendPacketCount     += 1;
            mSendPacketSizeCount += (ulong)bytes.Length;
            LogHelper.LogSendPacket(string.Format("actor={0} result={1} size={2}", id, result, bytes.Length));

            var mb = new MsgBuffer()
            {
                position = 0, buffer = bytes, bundle = bundle
            };
            var send = false;

            lock (msgBuffer)
            {
                msgBuffer.Add(mb);
                if (msgBuffer.Count == 1)                   //当msgBuffer为空并新加入一个消息时,我们才开始发送,不然在OnSend的回调中一直在发送buff,两边同时发送会有问题,
                {
                    send = true;
                }
            }
            if (send)
            {
                try
                {
                    mSocket.BeginSend(mb.buffer, mb.position, mb.Size, SocketFlags.None, OnSend, null);
                }
                catch (Exception exception)
                {
                    Debug.LogError(exception.Message);
                    Close();
                }
            }
        }
Exemplo n.º 10
0
 public void Send(byte[] data)
 {
     lock (msgBuffer)
     {
         var mb = new MsgBuffer()
         {
             position = 0, buffer = data
         };
         msgBuffer.Add(mb);
         if (msgBuffer.Count == 1)
         {
             try
             {
                 var asyncRet = mSocket.BeginSend(mb.buffer, mb.position, mb.Size, SocketFlags.None, OnSend, null);
                 ThreadPool.QueueUserWorkItem(SendTimeOut, asyncRet);
             } catch (Exception exception)
             {
                 Debug.LogError(exception.ToString());
                 Close();
             }
         }
     }
 }
Exemplo n.º 11
0
        private void OnSend(IAsyncResult result)
        {
            int num = 0;              //发送的字节数

            try
            {
                num = mSocket.EndSend(result);
            }
            catch (Exception exception)
            {
                num = 0;
                Close();
                Debug.LogError(exception.Message);
                return;
            }

            if (mSocket != null && mSocket.Connected)
            {
                MsgBuffer mb = null;
                lock (msgReader)
                {
                    mb = msgBuffer[0];
                }
                MsgBuffer nextBuffer = null;
                if (mb.Size == num)                  //发送成功
                {
                    lock (msgBuffer)
                    {
                        msgBuffer.RemoveAt(0);
                        if (msgBuffer.Count > 0)
                        {
                            nextBuffer = msgBuffer[0];
                        }
                    }
                    ServerBundle.ReturnBundle(mb.bundle);
                }
                else if (mb.Size > num)                  //发送了一部分,还有一部分没发
                {
                    mb.position += num;
                    nextBuffer   = msgBuffer[0];
                }
                else                  //出错了
                {
                    ServerBundle.ReturnBundle(mb.bundle);
                    lock (msgBuffer)
                    {
                        msgBuffer.RemoveAt(0);
                        if (msgBuffer.Count > 0)
                        {
                            nextBuffer = msgBuffer[0];
                        }
                    }
                }

                if (nextBuffer != null)
                {
                    try
                    {
                        mSocket.BeginSend(nextBuffer.buffer, nextBuffer.position, nextBuffer.Size, SocketFlags.None,
                                          new AsyncCallback(OnSend), null);                 //这里一直同步调用BeginSend和OnSend,直至nextBuffer == null,也就是所有消息全部发送完成,msgBuffer.Count == 0
                    }
                    catch (Exception exception)
                    {
                        Debug.LogError(exception.Message);
                        Close();
                    }
                }
            }
            else
            {
                Close();
            }
        }