예제 #1
0
        private void Callback_Connect(IAsyncResult result)
        {
            try {
                _socket = (Socket)result.AsyncState;
                _socket.EndConnect(result);
                _socket.ReceiveBufferSize = Gamnet.Buffer.BUFFER_SIZE;
                _socket.SendBufferSize    = Gamnet.Buffer.BUFFER_SIZE;
                //_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 10000);
                //_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000);
                _timer.Stop();

                Receive();

                _state           = ConnectionState.Connected;
                _disconnectState = DisconnectState.Invalid;

                Send_Connect_Req();

                _timer           = new System.Timers.Timer();
                _timer.Interval  = heartbeat_time;
                _timer.AutoReset = true;
                _timer.Elapsed  += delegate {
                    Send_HeartBeat_Req();
                };
                _timer.Start();
            }
            catch (System.Exception e) {
                Debug.LogError("[Session.Callback_Connect] exception:" + e.ToString());
                Error(new Gamnet.Exception(ErrorCode.ConnectFailError, e.ToString()));
                Disconnect();
            }
        }
예제 #2
0
 public void OnDisconnect(object sender, EventArgs e)
 {
     if (State.GetType() != typeof(DisconnectState))
     {
         State = new DisconnectState(this);
         State.Execute();
     }
 }
예제 #3
0
        public TimeoutMonitor SendMsg(object msg, bool handOverRelility = false)
        {
            if (null == _endpoint)
            {
                Debug.LogError("[Session.Reconnect] invalid destination address");
                return(null);
            }
            if (true == handOverRelility)
            {
                if (Application.internetReachability != _networkReachability)
                {
                    _networkReachability = Application.internetReachability;
                    Disconnect();
                    _disconnectState = DisconnectState.Handover;
                }
            }
            try
            {
                Reconnect();

                System.IO.MemoryStream ms   = new System.IO.MemoryStream();
                System.Type            type = msg.GetType();
                type.GetMethod("Store").Invoke(msg, new object[] { ms });
                System.Reflection.FieldInfo fi = type.GetField("MSG_ID");

                uint   msgID        = (uint)(int)fi.GetValue(msg);
                int    dataLength   = (int)(type.GetMethod("Size").Invoke(msg, null));
                ushort packetLength = (ushort)(Packet.HEADER_SIZE + dataLength);

                if (packetLength > Gamnet.Buffer.BUFFER_SIZE)
                {
                    throw new System.Exception(string.Format("Overflow the send buffer max size : {0}", packetLength));
                }

                Gamnet.Packet packet = new Gamnet.Packet();
                packet.length   = packetLength;
                packet.msg_seq  = ++_send_seq;
                packet.msg_id   = msgID;
                packet.reliable = handOverRelility;
                packet.Append(ms);

                lock (this)
                {
                    SendMsg(packet);
                    if (0 == _send_seq % 30 && ConnectionState.Connected == _state)
                    {
                        Send_HeartBeat_Req();
                    }
                }
            }
            catch (System.Exception e)
            {
                Debug.LogError("[Session.SendMsg] exception:" + e.ToString());
                Error(new Gamnet.Exception(ErrorCode.SendMsgFailError, e.ToString()));
                return(null);
            }
            return(_timeout_monitor);
        }
예제 #4
0
 public void Pause()
 {
     Debug.Log("[Session.Pause]");
     Disconnect();
     _disconnectState = DisconnectState.Pause;
 }
 /// <summary>
 /// Initializes a new instance.
 /// </summary>
 /// <param name="state"></param>
 public UnhandledDisconnectException(DisconnectState state)
     : base(state.ToString())
 {
     State = state;
 }