public void RunTimer() { StateObjClass StateObj = new StateObjClass(); StateObj.TimerCanceled = false; StateObj.SomeValue = 1; System.Threading.TimerCallback TimerDelegate = new System.Threading.TimerCallback(TimerTask); System.Threading.Timer TimerItem = new System.Threading.Timer(TimerDelegate, StateObj, 0, 3000); // Save a reference for Dispose. StateObj.TimerReference = TimerItem; CPacket msg = CPacket.create(); JsonObjectCollection collection = new JsonObjectCollection(); collection.Add(new JsonStringValue("PROTOCOL_ID", "PING")); msg.push(collection.ToString()); this.send(msg); if (SocketExtensions.IsConnected(this.socket)) { Console.WriteLine("true"); } else { StateObj.TimerReference.Change(Timeout.Infinite, Timeout.Infinite); //Change Console.WriteLine("false(("); } }
/// <summary> /// 새로운 클라이언트가 접속 성공 했을 때 호출됩니다. /// AcceptAsync의 콜백 매소드에서 호출되며 여러 스레드에서 동시에 호출될 수 있기 때문에 공유자원에 접근할 때는 주의해야 합니다. /// </summary> /// <param name="client_socket"></param> void on_new_client(Socket client_socket, object token) { // 플에서 하나 꺼내와 사용한다. SocketAsyncEventArgs receive_args = this.receive_event_args_pool.Pop(); SocketAsyncEventArgs send_args = this.send_event_args_pool.Pop(); // UserToken은 매번 새로 생성하여 깨끗한 인스턴스로 넣어준다. CUserToken user_token = new CUserToken(this.logic_entry); user_token.on_session_closed += this.on_session_closed; receive_args.UserToken = user_token; send_args.UserToken = user_token; this.usermanager.add(user_token); user_token.on_connected(); if (this.session_created_callback != null) { this.session_created_callback(user_token); } begin_receive(client_socket, receive_args, send_args); CPacket msg = CPacket.create((short)CUserToken.SYS_START_HEARTBEAT); byte send_interval = 5; msg.push(send_interval); user_token.send(msg); }
public void start_keepalive() { System.Threading.Timer keepalive = new System.Threading.Timer((object e) => { CPacket msg = CPacket.create(0); msg.push(0); send(msg); }, null, 0, 3000); }
public void close() { // 중복 수행을 막는다. if (Interlocked.CompareExchange(ref this.is_closed, 1, 0) == 1) { return; } if (this.current_state == State.Closed) { // already closed. return; } this.current_state = State.Closed; this.socket.Close(); this.socket = null; this.send_event_args.UserToken = null; this.receive_event_args.UserToken = null; this.sending_list.Clear(); this.message_resolver.clear_buffer(); if (this.peer != null) { CPacket msg = CPacket.create((short)-1); if (this.dispatcher != null) { this.dispatcher.on_message(this, new ArraySegment <byte>(msg.buffer, 0, msg.position)); } else { on_message(msg); } } }
public void start_keepalive() { System.Threading.Timer keepalive = null; keepalive = new System.Threading.Timer((object e) => { CPacket msg = CPacket.create(); JsonObjectCollection collection = new JsonObjectCollection(); collection.Add(new JsonStringValue("PROTOCOL_ID", "PING")); msg.push(collection.ToString()); this.send(msg); if (SocketExtensions.IsConnected(this.socket)) { Console.WriteLine("true"); } else { keepalive.Change(Timeout.Infinite, Timeout.Infinite); Console.WriteLine("false"); } }, null, 0, 3000); }
/// <summary> /// 종료코드를 전송하여 상대방이 먼저 끊도록 한다. /// </summary> void byebye() { CPacket bye = CPacket.create(SYS_CLOSE_REQ); send(bye); }
void send() { CPacket msg = CPacket.create((short)CUserToken.SYS_UPDATE_HEARTBEAT); this.server.send(msg); }