Beispiel #1
0
 private static void resoveSCException(SCException exception)
 {
     // 有些exception不弹窗,不报警
     if (exception.ErrCode == ErrorCode.RoomNotExist)
     {
         return;
     }
     Debug.LogError("error:errorCode = " + exception.ErrCode + ",errorMsg = " + exception.ErrMsg + ",csOpcode:" + exception.CsOpcode + ",scOpcode:" + exception.ScOpcode);
     WarnDialog.showWarnDialog(/*Message.getText("dataError")+*/ exception.ErrMsg, delegate() {
         //				ConnectServer();
     });
 }
Beispiel #2
0
    /**
     * 不要在主线程用,会阻塞主线程
     * 如果需要可以在主线程调用的同步网络io,可以考虑用协程做
     **/
    public static byte[] SendMessageSync(int opcode, byte[] data)
    {
        if (!IsConnected && !IsConnecting)
        {
            needConnectOnce = true;
        }
        try
        {
            int        id     = Interlocked.Increment(ref idIndex);
            ByteBuffer buffer = ByteBuffer.Allocate(512);
            buffer.WriteInt(data.Length);
            buffer.WriteInt(opcode);
            buffer.WriteInt(id);
            buffer.WriteBytes(data);
            byte[] sendData = buffer.ToArray();
            clientSocket.Send(sendData);
            SyncObject syncObject = new SyncObject();
            syncObjects.Add(id, syncObject);
//			Debug.Log("send success,size = "+data.Length);
            Monitor.Enter(syncObject);
            if (!Monitor.Wait(syncObject, 1000))
            {
                syncObjects.Remove(id);
            }
            Monitor.Exit(syncObject);
            if (syncObject.Opcode == (int)BaseOpcode.SCException)
            {
                SCException exception = SCException.Deserialize(syncObject.Data);
//				Debug.LogError("error:errorCode = "+exception.ErrCode+",errorMsg = "+exception.ErrMsg);
                // TODO 弹出窗口,抛出异常
                throw new Exception(exception.ErrCode + ":" + exception.ErrMsg);
            }
            return(syncObject.Data);
        }
        catch (Exception e)
        {
//			IsConnected = false;
//			clientSocket.Shutdown(SocketShutdown.Both);
//			clientSocket.Close();
            Debug.Log("send fail:" + e);
            throw new Exception(e.Message);
        }
    }
Beispiel #3
0
 private static void doReceivePacket(int opcode, int id, byte[] data)
 {
     if (opcode == (int)MiGongOpcode.SCSendEatBean)
     {
         SCSendEatBean sc = SCSendEatBean.Deserialize(data);
         Debug.Log("opcode:MiGongOpcode.SCEatBean" + sc.Beans.Count);
         foreach (PBEatBeanInfo bean in sc.Beans)
         {
             Debug.Log("" + bean.UserId + "," + bean.BeanPos); // 谁吃的
         }
     }
     if (dic.ContainsKey(id))
     {
         if (opcode == (int)BaseOpcode.SCException)
         {
             SCException exception = SCException.Deserialize(data);
             resoveSCException(exception);
         }
         else
         {
             ActionForReceive action = dic[id];
             if (action != null)
             {
                 AsyncObject o = new AsyncObject();
                 o.Opcode = opcode;
                 o.Data   = data;
                 o.action = action;
                 lock (invokeQueue)
                 {
                     invokeQueue.Enqueue(o);
                 }
             }
         }
         dic.Remove(id);
     }
     else if (syncObjects.ContainsKey(id))
     {
         if (opcode == (int)BaseOpcode.SCException)
         {
             SCException exception = SCException.Deserialize(data);
             resoveSCException(exception);
         }
         else
         {
         }
         SyncObject syncObject = syncObjects[id];
         Monitor.Enter(syncObject);
         syncObject.Data   = data;
         syncObject.Opcode = opcode;
         syncObjects.Remove(id);
         Monitor.Pulse(syncObject);
         Monitor.Exit(syncObject);
     }
     else
     {
         // 推送消息
         if (opcode == (int)BaseOpcode.SCException)
         {
             SCException exception = SCException.Deserialize(data);
             resoveSCException(exception);
         }
         else
         {
             if (serverSendData.ContainsKey(opcode))
             {
                 ActionForReceive action = serverSendData[opcode];
                 AsyncObject      o      = new AsyncObject();
                 o.Opcode = opcode;
                 o.Data   = data;
                 o.action = action;
                 lock (invokeQueue)
                 {
                     invokeQueue.Enqueue(o);
                 }
             }
             //                          Debug.Log("opcode = "+opcode+",");
         }
     }
 }