Exemplo n.º 1
0
        void client_DataReceived(Client sender, ReceiveBuffer e)
        {
            Packet pkt = DataPacket.DataParser(e.BufStream, e.pkt);

            UpdateCompLogMsg("Received data: " + pkt.ToString());
            switch (pkt.type)
            {
            case PktType.AUTH:
                if (pkt.response == PKT_ACK)
                {
                    UpdateStatusMessage("Success login.", pkt.guid);
                    guid = pkt.guid;
                    UpdateCompLogMsg(pkt.guid);
                }
                else
                {
                    UpdateStatusMessage("Failed login.", pkt.errMsg);
                }
                break;

            case PktType.PASSENGER:
                if (pkt.response == PKT_ACK)
                {
                    UpdateStatusMessage("Success passenger count.", pkt.accessId.ToString());
                    accessId = pkt.accessId;
                    UpdateCompLogMsg(pkt.accessId.ToString());
                }
                else
                {
                    UpdateStatusMessage("Failed passenger count.", pkt.errMsg);
                }
                break;

            case PktType.ORDER:
                if (pkt.response == PKT_ACK)
                {
                    UpdateStatusMessage("Success receive order.", pkt.order.orderId);
                    UpdateCompLogMsg(pkt.order.orderId);
                }
                else
                {
                    UpdateStatusMessage("Failed receive order.", pkt.errMsg);
                }
                break;

            case PktType.ONCE:
                if (pkt.response == PKT_ACK)
                {
                    UpdateStatusMessage("Success receive order.", pkt.order.orderId);
                    UpdateCompLogMsg(pkt.order.orderId);
                }
                else
                {
                    UpdateStatusMessage("Failed receive order.", pkt.errMsg);
                }
                break;
            }
        }
Exemplo n.º 2
0
        public void receiveCallBack(IAsyncResult ar)
        {
            try
            {
                int rec = socket.EndReceive(ar);

                if (rec == 0)
                {
                    if (OnDisconnect != null)
                    {
                        OnDisconnect(this);
                        return;
                    }
                }

                if (rec != headBuf.Length)
                {
                    throw new Exception();
                }
            }
            catch (SocketException se)
            {
                switch (se.SocketErrorCode)
                {
                case SocketError.ConnectionAborted:
                case SocketError.ConnectionReset:
                    if (OnDisconnect != null)
                    {
                        OnDisconnect(this);
                        return;
                    }
                    break;
                }
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (NullReferenceException)
            {
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            buffer = new ReceiveBuffer(headBuf);
            socket.BeginReceive(buffer.Buffer, 0, buffer.Buffer.Length, SocketFlags.None, receivePacketCallBack, null);
        }