示例#1
0
        void HandleConnect(IAsyncResult result)
        {
            Debug.Log("[{0}]Connected to server {1}", SessionId, m_endPoint.ToString());
            object[] param = (object[])result.AsyncState;
            Socket   sock  = (Socket)param[0];
            //IPEndPoint endpoint = (IPEndPoint)param[1];

            ServerConnected body = new ServerConnected();

            body.success = false;

            try
            {
                sock.EndConnect(result);

                if (sock.Connected)
                {
                    body.endpoint = (IPEndPoint)sock.RemoteEndPoint;
                    body.success  = true;
                    BeginReceive();
                }
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
                RestartSocket();
            }
            finally
            {
                connecting = false;

                Protocol proto = new Protocol(body);
                PostProto(proto);
            }
        }
        public Protocol GetMessage(int sessionId)
        {
            Protocol proto = allSessions[sessionId].GetMessage();

            if (proto != null)
            {
                if (proto.GetID() == ProtoIDReserve.CMD_SERVER_CONNECTED)
                {
                    ServerConnected body = (ServerConnected)proto.GetBody();
                    if (body.success)
                    {
                        Debug.Log("[{0}]CONNECTED", sessionId);
                    }
                    else
                    {
                        Debug.Log("[{0}]CONNECT FAILED", sessionId);
                    }
                }
                else if (proto.GetID() == ProtoIDReserve.CMD_SERVER_DISCONNECTED)
                {
                    Debug.Log("[{0}]DISCONNECTED", sessionId);
                }
                else
                {
                    Debug.Log("[{0}][RECV]{1}({2} bytes) {3}", sessionId, proto.GetID().ToString(), proto.GetBodySize().ToString(), DateTime.Now.ToString("HH:mm:ss.fff"));
                }
            }
            else
            {
                Debug.Log("[{0}]No Message", sessionId);
            }

            return(proto);
        }