ClientConnection.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:7k
源码类别:

J2ME

开发平台:

Java

  1. package cardserver;
  2. import javax.microedition.io.SocketConnection;
  3. /**
  4.  * <p>Title: CardServer</p>
  5.  *
  6.  * <p>Description: lizhenpeng</p>
  7.  *
  8.  * <p>Copyright: Copyright (c) 2005</p>
  9.  *
  10.  * <p>Company: LP&P</p>
  11.  *
  12.  * @author lipeng
  13.  * @version 1.0
  14.  */
  15. import java.io.*;
  16. import javax.microedition.io.*;
  17. import java.util.*;
  18. public class ClientConnection
  19.   implements Runnable
  20. {
  21.   public SocketConnection socket;
  22.   boolean isRunning;
  23.   InputStream is;
  24.   public OutputStream os;
  25.   public ClientConnection Oppo;
  26.   DataHead recvDataHead=new DataHead();
  27.   DataHead sendDataHead=new DataHead();
  28.   byte[] recvBuffer;
  29.   byte[] sendBuffer;
  30.   byte[] recvHeadBuffer=new byte[4];
  31.   public String userName;
  32.   Card [] totalCard = new Card[55];
  33.   public ClientConnection(SocketConnection sock)
  34.   {
  35.     isRunning=true;
  36.     socket=sock;
  37.     try
  38.     {
  39.       is=socket.openInputStream();
  40.       os=socket.openOutputStream();
  41.     } catch(Exception e)
  42.     {
  43.     }
  44.   }
  45.   public void run()
  46.   {
  47.     while(isRunning)
  48.     {
  49.       try
  50.       {
  51.         is.read(recvHeadBuffer,0,4);
  52.         recvDataHead.FillData(recvHeadBuffer,0);
  53.         if(recvDataHead.size!=0)
  54.         {
  55.           // socket.get
  56.           recvBuffer=new byte[recvDataHead.size];
  57.           is.read(recvBuffer,0,recvDataHead.size);
  58.           //处理数据
  59.           ProcessData();
  60.         } else
  61.         {
  62.           //处理信息
  63.           ProcessData();
  64.         }
  65.       } catch(Exception e)
  66.       {
  67.         System.out.println(e);
  68.         isRunning = false;
  69.         break;
  70.       }
  71.     }
  72.     MainForm.inst.userList.removeElement(this);
  73.   }
  74.   public void ProcessData()
  75.   {
  76.     switch(recvDataHead.command)
  77.     {
  78.       //发送本次产生的唯一的ID
  79.       case NetProtocol.LOGIN:
  80.       {
  81.         try
  82.         {
  83.           userName=new String(this.recvBuffer);
  84.           int port;
  85.           int size=MainForm.inst.userList.size();
  86.           byte[] buffer=new byte[32*(size-1)];
  87.           int place=0;
  88.           for(int i=0;i<size;i++)
  89.           {
  90.             ClientConnection client=(ClientConnection)MainForm.inst.userList.
  91.               elementAt(i);
  92.             if(client!=this)
  93.             {
  94.               byte[] nameBuffer=client.userName.getBytes();
  95.               System.arraycopy(nameBuffer,0,buffer,place,nameBuffer.length);
  96.               place+=32;
  97.             }
  98.           }
  99.           this.sendDataHead.command=NetProtocol.HASLOGIN;
  100.           sendDataHead.size=(char)buffer.length;
  101.           this.sendBuffer=sendDataHead.getBytes();
  102.           os.write(sendBuffer,0,sendBuffer.length);
  103.           os.write(buffer,0,buffer.length);
  104.           os.flush();
  105.         } catch(Exception e)
  106.         {
  107.           System.out.println(e);
  108.         }
  109.       }
  110.       break;
  111.       case NetProtocol.REQUEST_STARTGAME:
  112.       {
  113.         String name=new String(this.recvBuffer);
  114.         int size=MainForm.inst.userList.size();
  115.         for(int i=0;i<size;i++)
  116.         {
  117.           ClientConnection client=(ClientConnection)MainForm.inst.userList.
  118.             elementAt(i);
  119.           if(client.userName.compareTo(name)==0)
  120.           {
  121.             this.Oppo = client;
  122.             client.Oppo = this;
  123.             mixCard();
  124.             try
  125.             {
  126.               byte[] buffer1=new byte[4*54/2];
  127.               byte[] buffer2=new byte[4*54/2];
  128.               int place=0;
  129.               for(i=1;i<28;i++)
  130.               {
  131.                 System.arraycopy(totalCard[i].getBytes(),0,buffer1,place,4);
  132.                 place+=4;
  133.               }
  134.               place=0;
  135.               for(i=28;i<54;i++)
  136.               {
  137.                 System.arraycopy(totalCard[i].getBytes(),0,buffer2,place,4);
  138.                 place+=4;
  139.               }
  140.               sendDataHead.command=NetProtocol.STARTGAME_FIRST;
  141.               sendDataHead.size=(char)(buffer1.length+buffer2.length);
  142.               sendBuffer=sendDataHead.getBytes();
  143.               os.write(sendBuffer,0,sendBuffer.length);
  144.               os.write(buffer1,0,buffer1.length);
  145.               os.write(buffer2,0,buffer2.length);
  146.               Oppo.sendDataHead.command = NetProtocol.STARTGAME_LAST;
  147.               Oppo.sendDataHead.size = (char)(buffer2.length+buffer1.length);
  148.               Oppo.sendBuffer = Oppo.sendDataHead.getBytes();
  149.               Oppo.os.write(Oppo.sendBuffer,0,Oppo.sendBuffer.length);
  150.               Oppo.os.write(buffer2,0,buffer2.length);
  151.               Oppo.os.write(buffer1,0,buffer1.length);
  152.               //改变用户状态为游戏中
  153.               //通知开始
  154.               //发牌
  155.               //先后顺序
  156.             }
  157.             catch(Exception e)
  158.             {
  159.               System.out.print(e);
  160.             }
  161.             break;
  162.           }
  163.         }
  164.       }
  165.       break;
  166.       case NetProtocol.POST_CARD:
  167.       {
  168.         try
  169.         {
  170.           this.Oppo.sendDataHead.command=NetProtocol.OPPO_POSTCARD;
  171.           this.Oppo.sendDataHead.size=(char)recvBuffer.length;
  172.           this.Oppo.sendBuffer=Oppo.sendDataHead.getBytes();
  173.           Oppo.os.write(Oppo.sendBuffer,0,Oppo.sendBuffer.length);
  174.           this.Oppo.os.write(this.recvBuffer,0,this.recvBuffer.length);
  175.         }
  176.         catch(Exception e)
  177.         {
  178.           System.out.print(e);
  179.         }
  180.       }
  181.       break;
  182.       case NetProtocol.REJECT_CARD:
  183.       {
  184.         try
  185.         {
  186.           this.Oppo.sendDataHead.command=NetProtocol.OPPO_REJECT;
  187.           this.Oppo.sendDataHead.size=0;
  188.           this.Oppo.sendBuffer=Oppo.sendDataHead.getBytes();
  189.           Oppo.os.write(Oppo.sendBuffer,0,Oppo.sendBuffer.length);
  190.         }
  191.         catch(Exception e)
  192.         {
  193.           System.out.print(e);
  194.         }
  195.       }
  196.       break;
  197.     }
  198.   }
  199.   void mixCard()
  200.   {
  201.     initCard();
  202.     Random rand=new Random();
  203.     for(int i=1;i<totalCard.length;i++)
  204.     {
  205.       int order = Math.abs(rand.nextInt())%(totalCard.length-1)+1;
  206.       Card temp=totalCard[i];
  207.       totalCard[i]=totalCard[order];
  208.       totalCard[order]=temp;
  209.     }
  210.   }
  211.   /*
  212.     id 为1-54
  213.    kind 为1-5
  214.    1为红心
  215.    2为黑桃
  216.    3为方块
  217.    4位梅花
  218.    5为王
  219.    num为 1-13
  220.   */
  221.   void initCard()
  222.   {
  223.     for(int i=0;i<totalCard.length;i++)
  224.     {
  225.       totalCard[i]=new Card();
  226.       totalCard[i].id = (byte)(i+1);
  227.     }
  228.     for(int i=1;i<14;i++)
  229.     {
  230.       totalCard[i].kind=1;
  231.       totalCard[i].num=(byte)i;
  232.     }
  233.     for(int i=14;i<27;i++)
  234.     {
  235.       totalCard[i].kind=2;
  236.       totalCard[i].num=(byte)(i-13);
  237.     }
  238.     for(int i=27;i<40;i++)
  239.     {
  240.       totalCard[i].kind=3;
  241.       totalCard[i].num=(byte)(i-26);
  242.     }
  243.     for(int i=40;i<53;i++)
  244.     {
  245.       totalCard[i].kind=4;
  246.       totalCard[i].num=(byte)(i-39);
  247.     }
  248.     totalCard[53].kind = 5;
  249.     totalCard[53].num = 0;
  250.     totalCard[54].kind = 5;
  251.     totalCard[54].num = 1;
  252.   }
  253. }