Пример #1
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);
        }
Пример #2
0
        private void InitPlayerId()          //初始化客户端Player的ID,为每个Actor的专属ID,由ActorManager统一管理
        {
            var cmd = GCPlayerCmd.CreateBuilder();

            cmd.Result = string.Format("Init {0}", Id);

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

            ServerBundle.ReturnBundle(bundle);

            agent.SendBytes(bytes);
        }
Пример #3
0
        private void BroadcastToAll(GCPlayerCmd.Builder cmd)  //广播消息
        {
            var          parr = players;
            ServerBundle bundle;
            var          bytes = ServerBundle.sendImmediateError(cmd, 0, 0, out bundle);

            ServerBundle.ReturnBundle(bundle);

            foreach (var p in parr)
            {
                p.GetAgent().SendBytes(bytes);
            }
        }
Пример #4
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();
            }
        }