Ejemplo n.º 1
0
 public void ReceiveMessageFromClient(TcpClient client)
 {
     using (NetworkStream stream = client.GetStream())
         using (BinaryReader reader = new BinaryReader(stream))
         {
             while (!stop)
             {
                 if (stream.DataAvailable)
                 {
                     string           message     = reader.ReadString();
                     CommandEventArgs commandArgs = new CommandEventArgs(); //make sure tanslate from jason
                     OnMessageToServer?.Invoke(client, commandArgs);
                 }
                 else
                 {
                     Thread.Sleep(10);
                 }
             }
         }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Send a message to the environment
        /// </summary>
        /// <param name="request"></param>
        public IResponse SendToApi(IRequest request)
        {
            var messageJson = JsonConvert.SerializeObject(request);

            Logger.LogInformation($"<< {Id} {request.GetMessageName()}");
            Logger.LogDebug($"<< {Id} {messageJson}");
            IResponse response = new Response()
            {
                _requestInstanceId = request.GetRequestInstanceId(),
                _responseSuccess   = false
            };

            if (Status == TerminalStatus.Guest)
            {
                if (request.IsMessageType <AccountCreateAccountRequest>() ||
                    request.IsMessageType <AccountLoginAccountRequest>())
                {
                    response = OnMessageToServer?.Invoke(this, request);
                    if (response.IsSuccess())
                    {
                        Guid?accountId;
                        accountId = response.GetMessageAsType <AccountLoginAccountResponse>()?.Account?.Id;
                        accountId = (accountId == null) ? response.GetMessageAsType <AccountCreateAccountResponse>()?.CreatedAccountEvent?.Account.Id : accountId;
                        Authenticate(accountId.Value);
                    }
                }
            }
            else
            {
                if (request.IsMessageType <AccountLoginCharacterRequest>())
                {
                    var playCharacterRequest = request.GetMessageAsType <AccountLoginCharacterRequest>();
                    response = new AccountLoginCharacterResponse()
                    {
                        _requestInstanceId = request.GetRequestInstanceId(),
                        _responseSuccess   = true
                    };
                    try
                    {
                        Attach(playCharacterRequest.ItemId);
                    }
                    catch (Exception e)
                    {
                        response.SetSuccess(false).SetMessage("Can't attach to character. " + e.Message);
                    }
                }
                else if (request.IsMessageType <AccountLogoutAccountRequest>())
                {
                    response = OnMessageToServer?.Invoke(this, request);
                    if (response.IsSuccess())
                    {
                        Guest();
                    }
                }
                else
                {
                    response = OnMessageToServer?.Invoke(this, request);
                }
            }
            return(response);
        }
Ejemplo n.º 3
0
        //make sure to have listener.accept turned on already in imageserver, or here?
        public void AcceptClient(TcpClient client)
        {
            using (NetworkStream stream = client.GetStream())
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    while (!stop)
                    {
                        if (stream.DataAvailable)
                        {
                            string           message     = reader.ReadString();
                            CommandEventArgs commandArgs = new CommandEventArgs(); //make sure tanslate from jason
                            OnMessageToServer?.Invoke(this, commandArgs);
                        }
                        else
                        {
                            Thread.Sleep(10);
                        }
                    }
                }

            /**try
             * {
             *  // ShutdownEvent is a ManualResetEvent signaled by
             *  // Client when its time to close the socket.
             *  while (!ShutdownEvent.WaitOne(0))
             *  {
             *      try
             *      {
             *          // We could use the ReadTimeout property and let Read()
             *          // block.  However, if no data is received prior to the
             *          // timeout period expiring, an IOException occurs.
             *          // While this can be handled, it leads to problems when
             *          // debugging if we are wanting to break when exceptions
             *          // are thrown (unless we explicitly ignore IOException,
             *          // which I always forget to do).
             *          if (stream.DataAvailable)
             *          {
             *              // Give up the remaining time slice.
             *              Thread.Sleep(1);
             *          }
             *          else if (stream.Read(_data, 0, _data.Length) > 0)
             *          {
             *              // Raise the DataReceived event w/ data...
             *          }
             *          else
             *          {
             *              // The connection has closed gracefully, so stop the
             *              // thread.
             *              ShutdownEvent.Set();
             *          }
             *      }
             *      catch (IOException ex)
             *      {
             *          // Handle the exception...
             *      }
             *  }
             * }
             * catch (Exception ex)
             * {
             *  // Handle the exception...
             * }**/
            //Task.Run(() => ReceiveMessageFromClient(client));
        }