예제 #1
0
        private void handleNewClient(TcpClient client)
        {
            try {
                Console.WriteLine("new client!");
                clients.Add(client, new List <NETMSG>());
                BufferedStream  ns = new BufferedStream(client.GetStream());
                BinaryFormatter bf = new BinaryFormatter();

                //innitial sync
                SendClient(client, new NETMSG(NETMSG.MSG_TYPES.SERVER_OK, null, "welcome"));
                NETMSG r = ReceiveClient(client);

                if (NETMSG.MSG_TYPES.CLIENT_OK.Equals(r.Type))
                {
                    //client will request game
                    r = ReceiveClient(client);
                    if (NETMSG.MSG_TYPES.CLIENT_REQUEST_SYNC.Equals(r.Type))
                    {
                        //send it to client
                        SendClient(client, new NETMSG(NETMSG.MSG_TYPES.SERVER_GAME, objToBytes(this.game)));

                        NETMSG m = ReceiveClient(client);
                        ProcessClientMessage(client, m);

                        if (NETMSG.MSG_TYPES.CLIENT_READY.Equals(ReceiveClient(client).Type))
                        {
                            //handleClient
                            handleClientMainLoop(client);
                        }
                    }
                }
            } catch (Exception e) {
                Console.WriteLine(e.Message + "\n" + e.StackTrace);
            }
        }
예제 #2
0
        private void handleClientMainLoop(TcpClient client)
        {
            NETMSG defaultMSG = new NETMSG(NETMSG.MSG_TYPES.SERVER_OK, null);
            NETMSG toSend;

            Console.WriteLine("entering main loop for client " + client);

            while (client.Connected && clients.ContainsKey(client))
            {
                NETMSG m = ReceiveClient(client);
                ProcessClientMessage(client, m);

                if (clients.ContainsKey(client))
                {
                    if (clients[client].Count > 0)
                    {
                        //send to client then remove from queue
                        toSend = clients[client][0];
                        //Console.WriteLine( "TOSEND:" + toSend.Type.ToString());
                        SendClient(client, toSend);
                        clients[client].RemoveAt(0);
                    }
                    else
                    {
                        SendClient(client, defaultMSG);
                    }
                }
                else
                {
                    break;
                }
            }
        }
예제 #3
0
 public void FullBroadCast(NETMSG msg)
 {
     foreach (TcpClient client in clients.Keys)
     {
         clients[client].Add(msg);
         //SendClient( client, msg );
     }
 }
예제 #4
0
 public void BroadCastExceptForClient(TcpClient client, NETMSG msg)
 {
     foreach (TcpClient c in clients.Keys)
     {
         if (c != client)
         {
             clients[client].Add(msg);
             //SendClient( c, msg );
         }
     }
     Console.WriteLine("partial bc done");
 }
예제 #5
0
        public void SendClient(TcpClient client, NETMSG msg)
        {
            BinaryFormatter bf  = new BinaryFormatter();
            MemoryStream    mem = new MemoryStream();

            //Console.WriteLine( "SENDING" );

            bf.Serialize(mem, msg);  //ns
            byte[] b   = mem.ToArray();
            byte[] len = BitConverter.GetBytes(b.Length);
            client.GetStream().Write(len, 0, 4);
            client.GetStream().Write(b, 0, b.Length);
        }
예제 #6
0
        public void ProcessClientMessage(TcpClient client, NETMSG msg)
        {
            switch (msg.Type)
            {
            case NETMSG.MSG_TYPES.CLIENT_REQUEST_SYNC:
                SendClient(client, new NETMSG(NETMSG.MSG_TYPES.SERVER_GAME, objToBytes(this.game)));
                break;

            case NETMSG.MSG_TYPES.CLIENT_DISCONNECT:
                this.clients.Remove(client);
                Console.WriteLine("client disconnected");
                game.Disconnect((uint)NETMSG.bytesToObj(msg.Payload));
                FullBroadCast(new NETMSG(NETMSG.MSG_TYPES.PLAYER_DISCONNECTED, msg.Payload));
                break;

            case NETMSG.MSG_TYPES.CLIENT_OK:
                //for sync purposes
                break;

            case NETMSG.MSG_TYPES.CLIENT_READY:
                //for sync purposes
                break;

            case NETMSG.MSG_TYPES.CLIENT_REQUEST_UID:
                CardUtils.Player p = game.createPlayer();
                game.AddPlayer(p);
                SendClient(client, new NETMSG(NETMSG.MSG_TYPES.SERVER_PLAYER_UID, objToBytes(p)));
                FullBroadCast(new NETMSG(NETMSG.MSG_TYPES.PLAYER_CONNECTED, objToBytes(p)));
                break;

            case NETMSG.MSG_TYPES.PLAYER_PASS:
                game.Pass(((CardUtils.Player)NETMSG.bytesToObj(msg.Payload)).ID);
                BroadCastExceptForClient(client, msg);
                break;

            case NETMSG.MSG_TYPES.PLAYER_PICKS:
                Console.WriteLine("player picked: " + (uint)NETMSG.bytesToObj(msg.Payload));

                game.PickCard((uint)NETMSG.bytesToObj(msg.Payload));
                BroadCastExceptForClient(client, msg);
                break;

            case NETMSG.MSG_TYPES.PLAYER_BETS:
                game.Bet(((BET)NETMSG.bytesToObj(msg.Payload)).PlayerID, ((BET)NETMSG.bytesToObj(msg.Payload)).betTOAdd);
                BroadCastExceptForClient(client, msg);
                break;

            default:
                break;
            }
        }
예제 #7
0
        public NETMSG ReceiveClient(TcpClient client)
        {
            BinaryFormatter Bf = new BinaryFormatter();

            try {
                int          br  = 0;;
                byte[]       b   = new byte[256];
                MemoryStream mem = new MemoryStream();
                int          len;
                byte[]       lenb = new byte[4];

                //read length (client always sends length first as 4bytes)
                client.GetStream().Read(lenb, 0, 4);
                len = BitConverter.ToInt32(lenb, 0);

                int toread = len;
                int read   = 0;

                do
                {
                    if (toread < 256)
                    {
                        br = client.GetStream().Read(b, 0, toread);
                    }
                    else
                    {
                        br = client.GetStream().Read(b, 0, 256);
                    }
                    mem.Write(b, 0, br);
                    read += br;
                } while (br == 256);

                mem.Position = 0;
                NETMSG msg = (NETMSG)Bf.Deserialize(mem);
                //Console.WriteLine( "server received: " + msg.Type.ToString() );
                return(msg);
            } catch (Exception e) {
                return(new NETMSG(NETMSG.MSG_TYPES.CLIENT_ERROR, objToBytes(e), e.Message));
            }
        }
예제 #8
0
        public void ProcessClientMessage(TcpClient client, NETMSG msg)
        {
            switch (msg.Type)
            {
            case NETMSG.MSG_TYPES.CLIENT_REQUEST_SYNC:
                SendClient(client, new NETMSG(NETMSG.MSG_TYPES.SERVER_GAME, objToBytes(this.game)));
                break;

            case NETMSG.MSG_TYPES.CLIENT_DISCONNECT:
                this.clients.Remove(client);
                printl("client disconnected");
                game.Disconnect((uint)NETMSG.bytesToObj(msg.Payload));
                FullBroadCast(new NETMSG(NETMSG.MSG_TYPES.PLAYER_DISCONNECTED, msg.Payload));
                break;

            case NETMSG.MSG_TYPES.CLIENT_OK:
                //for sync purposes
                break;

            case NETMSG.MSG_TYPES.CLIENT_READY:
                //for sync purposes
                break;

            case NETMSG.MSG_TYPES.CLIENT_REQUEST_UID:
                CardUtils.Player p = game.createPlayer();
                game.AddPlayer(p);

                if (game.Players.Count == 1)
                {
                    printl(p.ID + " is the first so he starts.");
                    game.PlayingPlayer = game.Players[0];
                    clients[client].Add(new NETMSG(NETMSG.MSG_TYPES.PLAYER_YOURTURN, null));
                }
                SendClient(client, new NETMSG(NETMSG.MSG_TYPES.SERVER_PLAYER_UID, objToBytes(p)));

                FullBroadCast(new NETMSG(NETMSG.MSG_TYPES.PLAYER_CONNECTED, objToBytes(p)));
                break;

            case NETMSG.MSG_TYPES.PLAYER_PASS:
                uint passingID = (uint)NETMSG.bytesToObj(msg.Payload);
                printl(passingID + " passed.");
                game.Pass(passingID);
                FullBroadCast(msg);

                break;

            case NETMSG.MSG_TYPES.PLAYER_PICKS:
                printl("player picked: " + (uint)NETMSG.bytesToObj(msg.Payload));
                uint           pid    = (uint)NETMSG.bytesToObj(msg.Payload);
                CardUtils.Card picked = game.PickCard(pid);

                FullBroadCast(
                    new NETMSG(
                        NETMSG.MSG_TYPES.PLAYER_PICKS,
                        objToBytes(
                            new PICK(
                                pid,
                                picked
                                )
                            )
                        )
                    );
                break;

            case NETMSG.MSG_TYPES.PLAYER_BETS:
                game.Bet(((BET)NETMSG.bytesToObj(msg.Payload)).PlayerID, ((BET)NETMSG.bytesToObj(msg.Payload)).betTOAdd);

                FullBroadCast(msg);
                //BroadCastExceptForClient(client, msg);
                break;

            default:
                break;
            }
        }