Exemplo n.º 1
0
        public void Send(MessageType type, int x = 0, int y = 0)   //发送信息
        {
            int length = 16;

            byte[] head = new byte[length];
            StreamHead.WriteHead(head, type, x, y);

            lock (messages)   //线程锁
            {
                messages.Add(head);
            }
        }
Exemplo n.º 2
0
        void receiver()    //接收端
        {
            while (true)
            {
                MessageType type;
                int         x, y;

                byte[] getLength = new byte[4];
                int    alllength;
                try
                {
                    alllength = socket.Receive(getLength, 4, SocketFlags.None);  //接受数据
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    goto End;
                }
                alllength = BitConverter.ToInt32(getLength, 0);
                byte[] head = new byte[alllength];
                byte[] temp = new byte[alllength];
                Array.Copy(getLength, head, 4);

                try
                {
                    int sub = alllength - 4;
                    int length;
                    while (true)
                    {
                        int l = (temp.Length < sub) ? temp.Length : sub;
                        length = socket.Receive(temp, l, SocketFlags.None);
                        if (length <= 0)
                        {
                            goto End;
                        }
                        if (length == sub)
                        {
                            Array.Copy(temp, 0, head, head.Length - sub, length);
                            break;
                        }
                        else // if (length < sub)
                        {
                            Array.Copy(temp, 0, head, head.Length - sub, length);
                            sub -= length;
                        }
                    }
                    StreamHead.Read(head, out type, out x, out y);

                    if (type == MessageType.Reset)
                    {
                        MainWindow.Piece self = random.Next(0, 2) == 0 ? MainWindow.Piece.Black : MainWindow.Piece.White;
                        MainWindow.Piece now  = MainWindow.Piece.Black;

                        // 先设置棋子 再请求刷新 否则可能不一致
                        Owner.RefreshText(self, now);
                        Owner.Reset();

                        Send(MessageType.Piece, (int)MainWindow.Reserve(self), (int)now);
                    }
                    if (type == MessageType.Piece)
                    {
                        Owner.RefreshText(self, now);
                        Owner.Reset();
                    }
                    if (type == MessageType.Set)
                    {
                        Owner.Set(new Point(x, y), false);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    goto End;
                }
            }

End:
            new Thread(abort).Start();
        }