static void StartMulticastConnector() { IPAddress localIPAddr = IPAddress.Any; IPEndPoint mcastEP = new IPEndPoint(mcastAddress, mcastPort); AsyncDatagramConnector connector = new AsyncDatagramConnector(); connector.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8))); // Set the local IP address used by the listener and the sender to // exchange multicast messages. connector.DefaultLocalEndPoint = new IPEndPoint(localIPAddr, 0); // Define a MulticastOption object specifying the multicast group // address and the local IP address. // The multicast group address is the same as the address used by the listener. MulticastOption mcastOption = new MulticastOption(mcastAddress, localIPAddr); connector.SessionConfig.MulticastOption = mcastOption; // Call Connect() to force binding to the local IP address, // and get the associated multicast session. IoSession session = connector.Connect(mcastEP).Await().Session; // Send multicast packets to the multicast endpoint. session.Write("hello 1", mcastEP); session.Write("hello 2", mcastEP); session.Write("hello 3", mcastEP); }
public void Broadcast(String message) { if (session != null) { session.Write("BROADCAST " + message); } }
public override void MessageReceived(IoSession session, Object message) { String theMessage = (String)message; String[] result = theMessage.Split(new Char[] { ' ' }, 2); String theCommand = result[0]; String user = session.GetAttribute <String>("user"); if (String.Equals("QUIT", theCommand, StringComparison.OrdinalIgnoreCase)) { session.Write("QUIT OK"); session.Close(true); } else if (String.Equals("LOGIN", theCommand, StringComparison.OrdinalIgnoreCase)) { if (user != null) { session.Write("LOGIN ERROR user " + user + " already logged in."); return; } if (result.Length == 2) { user = result[1]; } else { session.Write("LOGIN ERROR invalid login command."); return; } // check if the username is already used if (users.ContainsKey(user)) { session.Write("LOGIN ERROR the name " + user + " is already used."); return; } sessions[session] = true; session.SetAttribute("user", user); // Allow all users users[user] = true; session.Write("LOGIN OK"); Broadcast("The user " + user + " has joined the chat session."); } else if (String.Equals("BROADCAST", theCommand, StringComparison.OrdinalIgnoreCase)) { if (result.Length == 2) { Broadcast(user + ": " + result[1]); } } else { Console.WriteLine("Unhandled command: " + theCommand); } }
public T invoke <T> (int timeoutInMs, string mappingUrl, params object[] paramList) { makeConnectionInCallerThread(15000); if (ioSession == null || !ioSession.Connected) { return(default(T)); } CallCommand callCmd = new CallCommand(); callCmd.procedureUri = mappingUrl; callCmd.args = paramList; callCmd.options.Add("Cookie", cookieManager.getCookieForSendToServer()); CallResultFuture asyncResult = new CallResultFuture(); metaHolder.requestPool.Add(callCmd.requestId, asyncResult); asyncResult.resultType = typeof(T); ioSession.Write(callCmd); lock (asyncResult.monitorLock) { //Monitor.Wait (asyncResult.monitorLock, timeoutInMs); commented for test Monitor.Wait(asyncResult.monitorLock); } if (!asyncResult.done) { throw new TimeoutException("{timeoutInMs:" + timeoutInMs + '}'); } if (!asyncResult.isExceptionResult) { return((T)asyncResult.result); } if (asyncResult.isExceptionResult) { throw new Exception((string)asyncResult.result); } return(default(T)); }
public void SessionCreated(IoSession session) { LOG.Info("[SESSION-CREATE (" + Config.Call_Sign + ")]"); //Setup session var ariesSession = new AriesSession(session); session.SetAttribute("s", ariesSession); _Sessions.Add(ariesSession); foreach (var interceptor in _SessionInterceptors) { try{ interceptor.SessionCreated(ariesSession); }catch (Exception ex) { LOG.Error(ex); } } if (TimeoutIfNoAuth) { ariesSession.TimeoutIfNoAuth(20000); } //Ask for session info session.Write(new RequestClientSession()); }
public virtual void RunAsync() { if (!session.Connected) { throw new SessionOpenException(session.RemoteEndPoint as IPEndPoint); } if (hasRunCount <= MAX_RUN_COUNT) { var commandMessage = BuildMessage(); if (producer.Produce(ID, this, BLOCK_UNTIL_TIMEOUT_AFTER_SECONDS)) { hasRunCount++; try { session.Write(commandMessage); } catch (Exception ex) { AbstractAsyncCommand command = null; producer.TryRemoveThoseWaitToTimeout(commandMessage.Header.MessageID, out command); Log.Error(string.Format(ExceptionMessage.COMMAND_RUN_ERROR, session.GetAttribute <string>(KeyName.SESSION_IDENTIFIER)), ex); this.RaiseCallback(this, new CommandEventArgs <DuplexMessage>(DuplexMessage.CreateAckMessage(commandMessage, ErrorCode.FireCommandError))); } } } else { Log.Warn( string.Format(ExceptionMessage.TOO_MUCH_RETRY, session.GetAttribute <string>(KeyName.SESSION_IDENTIFIER))); } }
public void Write(params object[] packets) { if (Session != null) { Session.Write(packets); } }
static void Main(string[] args) { IoAcceptor acceptor = new LoopbackAcceptor(); LoopbackEndPoint lep = new LoopbackEndPoint(8080); // Set up server acceptor.Handler = new TennisPlayer(); acceptor.Bind(lep); // Connect to the server. LoopbackConnector connector = new LoopbackConnector(); connector.Handler = new TennisPlayer(); IConnectFuture future = connector.Connect(lep); future.Await(); IoSession session = future.Session; // Send the first ping message session.Write(new TennisBall(10)); // Wait until the match ends. session.CloseFuture.Await(); acceptor.Unbind(); }
public void TestUnbindResume() { Bind(true); IoConnector connector = NewConnector(); IoSession session = null; IConnectFuture future = connector.Connect(CreateEndPoint(port)); future.Await(); session = future.Session; Assert.IsTrue(session.Connected); Assert.IsTrue(session.Write(IoBuffer.Allocate(1)).Await().Written); // Wait for the server side session to be created. Thread.Sleep(500); ICollection <IoSession> managedSession = acceptor.ManagedSessions.Values; Assert.AreEqual(1, managedSession.Count); acceptor.Unbind(); // Wait for the client side sessions to close. Thread.Sleep(500); //Assert.AreEqual(0, managedSession.Count); foreach (IoSession element in managedSession) { Assert.IsFalse(element.Connected); } // Rebind Bind(true); // Check again the connection future = connector.Connect(CreateEndPoint(port)); future.Await(); session = future.Session; Assert.IsTrue(session.Connected); Assert.IsTrue(session.Write(IoBuffer.Allocate(1)).Await().Written); // Wait for the server side session to be created. Thread.Sleep(500); managedSession = acceptor.ManagedSessions.Values; Assert.AreEqual(1, managedSession.Count); }
public void Write(params object[] messages) { //TODO: Frame this more efficiently foreach (var message in messages) { IoSession.Write(message); } }
protected virtual void ReceivePoststarMessage(IoSession session, PostStarMessage starMessage) { Console.WriteLine(String.Format("{0}", starMessage.text)); PostStarMessage reStarMessage = new PostStarMessage(starMessage.getSender()); reStarMessage.text = "잘 받았어요."; session.Write(reStarMessage); }
private void HandlePlayAction(string[] input, IoSession session) { if (_gstate.Equals(GameState.Playing) && _pstate.Equals(PlayerState.PlayerTurn)) { if (input.Length == 2) { this.PlayCardNumber(input, session); } else if (input.Length != 3) { Console.WriteLine("Not enought argument, use /help to have further informations"); } else { string color = input[1]; int value = this.getValue(input[2]); if (value.Equals(0)) { bool res = int.TryParse(input[2], out value); if (!res) { Console.WriteLine("Second argument is not a number, run /help"); return; } } if (!color.Equals("spike") && !color.Equals("spade") && !color.Equals("club") && !color.Equals("heart")) { Console.WriteLine("Color is not good, run /help"); } else if (value < 7 || value > 14) { Console.WriteLine("Value is not good, run with /help"); } else { ACard card = _hand.FindCard(value, color); if (card == null) { Console.WriteLine("Cannot find the card in your hand, use /hand"); } else { _hand.DeleteCard(card); session.Write("0011 " + JsonConvert.SerializeObject(card)); _pstate = PlayerState.OtherTurn; } } } } else { Console.WriteLine("Not playing or not your turn"); } }
public bool SendMessage(IPacket packet) { if (session == null) { return(false); } var message = packet.Build(); session.Write(message).Await(); return(true); }
private void HandlePassAction(IoSession session) { if (_gstate.Equals(GameState.BindingPhase) && _pstate.Equals(PlayerState.PlayerTurn)) { session.Write("0012"); _pstate = PlayerState.OtherTurn; } else { Console.WriteLine("Actually not in Binding Phase or not your turn"); } }
public virtual void AddPlayer(IPlayer player, IoSession session) { IGame tmp; session.Write("0002"); if ((tmp = FreeGame) == null) { Console.WriteLine("New game created !"); tmp = new CoincheGame(); games.Add(tmp); } tmp.AddPlayer(player); players.Add(new handlerPlayer(session, tmp)); }
public void Return(DuplexMessage resultMessage) { try { if (session != null) { session.Write(resultMessage); } } catch (Exception ex) { Log.Error(ErrorCode.SysError, ex); } }
/// <summary> /// If the message is 'quit', we exit by closing the session. Otherwise, /// we return the current date. /// </summary> public override void MessageReceived(IoSession session, Object message) { String str = message.ToString(); // "Quit" ? let's get out ... if (str.Trim().Equals("quit", StringComparison.OrdinalIgnoreCase)) { session.Close(true); return; } // Send the current date back to the client session.Write(DateTime.Now.ToString()); Console.WriteLine("Message written..."); }
public override void MessageReceived(IoSession session, Object message) { IoBuffer rb = message as IoBuffer; if (rb == null) { return; } // Write the received data back to remote peer IoBuffer wb = IoBuffer.Allocate(rb.Remaining); wb.Put(rb); wb.Flip(); session.Write(wb); }
private void buttonConnect_Click(object sender, EventArgs e) { String server = textBoxServer.Text; if (String.IsNullOrEmpty(server)) { return; } if (checkBoxSSL.Checked) { if (!connector.FilterChain.Contains("ssl")) { connector.FilterChain.AddFirst("ssl", new SslFilter("TempCert", null)); } } else if (connector.FilterChain.Contains("ssl")) { connector.FilterChain.Remove("ssl"); } IPEndPoint ep; String[] parts = server.Trim().Split(':'); if (parts.Length > 0) { ep = new IPEndPoint(IPAddress.Parse(parts[0]), Int32.Parse(parts[1])); } else { ep = new IPEndPoint(IPAddress.Loopback, Int32.Parse(parts[0])); } IConnectFuture future = connector.Connect(ep).Await(); if (future.Connected) { session = future.Session; session.Write("LOGIN " + textBoxUser.Text); } else { MessageBox.Show("Could not connect to " + server); } }
private void HandleBidAction(string[] input, IoSession session) { if (_gstate == GameState.BindingPhase && _pstate == PlayerState.PlayerTurn) { if (input.Length != 3) { Console.WriteLine("Not enought argument, run /help"); } else { string color = input[1]; int bid = 0; bool res = int.TryParse(input[2], out bid); if (!res) { Console.WriteLine("Second argument is not a number, run /help"); } else if (!color.Equals("spike") && !color.Equals("spade") && !color.Equals("club") && !color.Equals("heart")) { Console.WriteLine("Color is not good, run /help"); } else if (bid < _lastBid || bid < 80 || bid > 160) { Console.WriteLine("Bad bid amount, run /help"); } else { _lastBid = bid; _color = color; string toSend = "0010 " + color + " " + bid.ToString(); session.Write(toSend); _pstate = PlayerState.OtherTurn; } } } else { Console.WriteLine("Not playing or not your turn"); } }
/// <summary> /// 메시지 수신 Handler /// </summary> /// <param name="session"></param> /// <param name="message"></param> public override void MessageReceived(IoSession session, Object message) { try { Message baseMessage = (Message)message; Console.WriteLine(String.Format("{0} : ", baseMessage.getSender().nickName)); Type msgType = message.GetType(); if (msgType == typeof(String)) { Console.WriteLine(message); } else if (msgType == typeof(PostStarMessage)) { PostStarMessage starMessage = (PostStarMessage)message; Console.WriteLine(String.Format("{0}", starMessage.text)); PostStarMessage reStarMessage = new PostStarMessage(baseMessage.getSender()); reStarMessage.text = "잘 받았어요."; session.Write(reStarMessage); } else if (msgType == typeof(CardMessage)) { CardMessage cardMessage = (CardMessage)message; Console.WriteLine(String.Format("CARD TITLE : {0}", cardMessage.cardTitle)); Console.WriteLine(String.Format("CARD BODY : {0}", cardMessage.cardBody)); } else { throw new Exception("알 수 없는 메시지 형식입니다."); } } catch (Exception ex) { throw ex; } finally { session.Close(false); } }
public override void MessageReceived(IoSession session, Object message) { Console.WriteLine("Player-" + id + ": RCVD " + message); TennisBall ball = (TennisBall)message; // Stroke: TTL decreases and PING/PONG state changes. ball = ball.Stroke(); if (ball.TTL > 0) { // If the ball is still alive, pass it back to peer. session.Write(ball); } else { // If the ball is dead, this player loses. Console.WriteLine("Player-" + id + ": LOSE"); session.Close(true); } }
private void PlayCardNumber(string[] input, IoSession session) { int value = 0; bool res = int.TryParse(input[1], out value); if (!res) { Console.WriteLine("Second argument is not a number, run /help"); } else if (value > _hand.GetSize()) { Console.WriteLine("You don't have enought card in your hand"); } else { ACard card = _hand.FindCard(value); session.Write("0011 " + JsonConvert.SerializeObject(card)); _hand.DeleteCard(value); _pstate = PlayerState.OtherTurn; } }
public void SessionCreated(IoSession session) { LOG.Info("[SESSION-CREATE]"); //Setup session var ariesSession = new AriesSession(session); session.SetAttribute("s", ariesSession); _Sessions.Add(ariesSession); foreach (var interceptor in _SessionInterceptors) { try{ interceptor.SessionCreated(ariesSession); }catch (Exception ex) { LOG.Error(ex); } } //Ask for session info session.Write(new RequestClientSession()); }
/// <summary> /// Writes the given buffer. /// </summary> public void Write(IoBuffer buf) { if (CanRead) { if (_closed) { return; } lock (_syncRoot) { if (_buf.HasRemaining) { _buf.Compact().Put(buf).Flip(); } else { _buf.Clear().Put(buf).Flip(); Monitor.PulseAll(_syncRoot); } } } else if (CanWrite) { if (!_session.Connected) { throw new IOException("The session has been closed."); } _lastWriteFuture = _session.Write(buf); } else { throw new NotSupportedException(); } }
public override void SessionOpened(IoSession session) { session.Write(message); }
public override void MessageReceived(IoSession session, Object message) { IoBuffer rb = message as IoBuffer; if (rb == null) return; // Write the received data back to remote peer IoBuffer wb = IoBuffer.Allocate(rb.Remaining); wb.Put(rb); wb.Flip(); session.Write(wb); }
private void buttonConnect_Click(object sender, EventArgs e) { String server = textBoxServer.Text; if (String.IsNullOrEmpty(server)) return; if (checkBoxSSL.Checked) { if (!connector.FilterChain.Contains("ssl")) connector.FilterChain.AddFirst("ssl", new SslFilter("TempCert", null)); } else if (connector.FilterChain.Contains("ssl")) { connector.FilterChain.Remove("ssl"); } IPEndPoint ep; String[] parts = server.Trim().Split(':'); if (parts.Length > 0) { ep = new IPEndPoint(IPAddress.Parse(parts[0]), Int32.Parse(parts[1])); } else { ep = new IPEndPoint(IPAddress.Loopback, Int32.Parse(parts[0])); } IConnectFuture future = connector.Connect(ep).Await(); if (future.Connected) { session = future.Session; session.Write("LOGIN " + textBoxUser.Text); } else { MessageBox.Show("Could not connect to " + server); } }
public void SessionOpen(IoSession session) { Console.WriteLine("Connected to the server"); Console.WriteLine("Game is coinche"); session.Write("0001 Hello World"); }
private void Write(IoSession session, String s) { session.Write(IoBuffer.Wrap(Encoding.ASCII.GetBytes(s))); }
public void Announce(PacketOutStream packet) { session.Write(packet.getPackets()); packet.Dispose(); }
private void DoFlush(IoSession session, WriteOp op) { if (op != null && !op.IsExpired) { try { long cnt = flushCounter.AtomicIncrementAndGet(); long now = new DateTime().CurrentTimeMillis(); if (now - lastLogTime.ReadFullFence() > 60000) { lastLogTime.WriteFullFence(now); string log = String.Format("opQueueSize: {0}, session: {1}, couner: {2}", opQueue.Count, session.RemoteEndPoint.ToString(), cnt); client.Log.Info(log); } var writeFuture = session.Write(op.Command); writeFuture.Complete += (s, e) => { if (e.Future.Done) { flushing.AtomicExchange(false); flushingOp.AtomicExchange(null); } else { if (!IsClosed) { Thread.Sleep(client.Config.EndpointSessionWriteRetryDealyInMills); DoFlush(e.Future.Session, op); } } }; } catch (Exception ex) { client.Log.Error(ex); flushing.AtomicExchange(false); flushingOp.AtomicExchange(null); } } else { flushing.AtomicExchange(false); flushingOp.AtomicExchange(null); } }
public void TestSendLargeFile() { Assert.AreEqual(FILE_SIZE, file.Length, "Test file not as big as specified"); CountdownEvent countdown = new CountdownEvent(1); Boolean[] success = { false }; Exception[] exception = { null }; Int32 port = 12345; IoAcceptor acceptor = CreateAcceptor(); IoConnector connector = CreateConnector(); try { acceptor.ExceptionCaught += (s, e) => { exception[0] = e.Exception; e.Session.Close(true); }; Int32 index = 0; acceptor.MessageReceived += (s, e) => { IoBuffer buffer = (IoBuffer)e.Message; while (buffer.HasRemaining) { int x = buffer.GetInt32(); if (x != index) { throw new Exception(String.Format("Integer at {0} was {1} but should have been {0}", index, x)); } index++; } if (index > FILE_SIZE / 4) { throw new Exception("Read too much data"); } if (index == FILE_SIZE / 4) { success[0] = true; e.Session.Close(true); } }; acceptor.Bind(CreateEndPoint(port)); connector.ExceptionCaught += (s, e) => { exception[0] = e.Exception; e.Session.Close(true); }; connector.SessionClosed += (s, e) => countdown.Signal(); IConnectFuture future = connector.Connect(CreateEndPoint(port)); future.Await(); IoSession session = future.Session; session.Write(file); countdown.Wait(); if (exception[0] != null) { throw exception[0]; } Assert.IsTrue(success[0], "Did not complete file transfer successfully"); Assert.AreEqual(1, session.WrittenMessages, "Written messages should be 1 (we wrote one file)"); Assert.AreEqual(FILE_SIZE, session.WrittenBytes, "Written bytes should match file size"); } finally { try { connector.Dispose(); } finally { acceptor.Dispose(); } } }